2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openo.baseservice.encrypt.cbb.impl;
19 import java.security.NoSuchAlgorithmException;
20 import java.security.spec.InvalidKeySpecException;
21 import java.security.spec.KeySpec;
23 import javax.crypto.Cipher;
24 import javax.crypto.SecretKey;
25 import javax.crypto.SecretKeyFactory;
26 import javax.crypto.spec.IvParameterSpec;
27 import javax.crypto.spec.PBEKeySpec;
28 import javax.crypto.spec.SecretKeySpec;
29 import javax.xml.bind.DatatypeConverter;
31 import org.openo.baseservice.encrypt.cbb.inf.AbstractCipher;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * CipherManager implement<br/>
41 * @version 31-May-2016
43 public class AesCipher implements AbstractCipher {
45 private static final Logger LOG = LoggerFactory.getLogger(AesCipher.class);
47 private static final byte[] DEFAULT_IV = {2, 1, 4, 8, 0, 3, 2, 0, 7, 9, 2, 8, 5, 11, 6, 1};
49 private static final IvParameterSpec IV_PARAMETER_SPEC = new IvParameterSpec(DEFAULT_IV);
51 protected final SecretKey secretKey;
56 * Creates default key.
63 secretKey = createSecretKey("default");
75 public AesCipher(final String key) {
77 secretKey = createSecretKey(key);
80 private SecretKey createSecretKey(final String key) {
81 SecretKey secretKey = null;
83 final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
84 final KeySpec keySpec = new PBEKeySpec(key.toCharArray(), DEFAULT_IV, 30000, 128);
86 secretKey = keyFactory.generateSecret(keySpec);
87 return new SecretKeySpec(secretKey.getEncoded(), "AES");
88 } catch(InvalidKeySpecException e) {
89 LOG.error("Invalid KeySpec ", e);
90 } catch(NoSuchAlgorithmException e) {
91 LOG.error("Algorithm do not support: ", e);
97 public String encrypt(final String plain) {
99 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
100 cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV_PARAMETER_SPEC);
101 final byte[] encryptToBytes = cipher.doFinal(plain.getBytes());
102 return DatatypeConverter.printBase64Binary(encryptToBytes);
103 } catch(final Exception e) {
104 LOG.error("Encrypt the plain error:", e);
110 public String decrypt(final String encrypted) {
112 if(encrypted == null || encrypted.length() == 0) {
116 if(secretKey == null) {
121 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
122 cipher.init(Cipher.DECRYPT_MODE, secretKey, IV_PARAMETER_SPEC);
123 final byte[] tempBytes = DatatypeConverter.parseBase64Binary(encrypted);
124 final byte[] decrypTobytes = cipher.doFinal(tempBytes);
125 return new String(decrypTobytes);
126 } catch(final Exception e) {
127 LOG.error("decrypt the plain error:", e);