2 * Copyright (c) 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 SDNO 0.5 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");
74 public AesCipher(final String key) {
76 secretKey = createSecretKey(key);
79 private SecretKey createSecretKey(final String key) {
80 SecretKey secretKey = null;
82 final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
83 final KeySpec keySpec = new PBEKeySpec(key.toCharArray(), DEFAULT_IV, 30000, 128);
85 secretKey = keyFactory.generateSecret(keySpec);
86 return new SecretKeySpec(secretKey.getEncoded(), "AES");
87 } catch(InvalidKeySpecException e) {
88 LOG.error("Invalid KeySpec ", e);
89 } catch(NoSuchAlgorithmException e) {
90 LOG.error("Algorithm do not support: ", e);
96 public String encrypt(final String plain) {
98 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
99 cipher.init(Cipher.ENCRYPT_MODE, secretKey, IV_PARAMETER_SPEC);
100 final byte[] encryptToBytes = cipher.doFinal(plain.getBytes());
101 return DatatypeConverter.printBase64Binary(encryptToBytes);
102 } catch(final Exception e) {
103 LOG.error("Encrypt the plain error:", e);
109 public String decrypt(final String encrypted) {
111 if(encrypted == null || encrypted.length() == 0) {
115 if(secretKey == null) {
120 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
121 cipher.init(Cipher.DECRYPT_MODE, secretKey, IV_PARAMETER_SPEC);
122 final byte[] tempBytes = DatatypeConverter.parseBase64Binary(encrypted);
123 final byte[] decrypTobytes = cipher.doFinal(tempBytes);
124 return new String(decrypTobytes);
125 } catch(final Exception e) {
126 LOG.error("decrypt the plain error:", e);