adjust the code
[vfc/nfvo/wfengine.git] / common-util / src / main / java / org / openo / baseservice / encrypt / cbb / impl / AesCipher.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.baseservice.encrypt.cbb.impl;
18
19 import java.security.NoSuchAlgorithmException;
20 import java.security.spec.InvalidKeySpecException;
21 import java.security.spec.KeySpec;
22
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;
30
31 import org.openo.baseservice.encrypt.cbb.inf.AbstractCipher;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * CipherManager implement<br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version SDNO 0.5 31-May-2016
42  */
43 public class AesCipher implements AbstractCipher {
44
45     private static final Logger LOG = LoggerFactory.getLogger(AesCipher.class);
46     
47     private static final byte[] DEFAULT_IV = {2, 1, 4, 8, 0, 3, 2, 0, 7, 9, 2, 8, 5, 11, 6, 1};
48
49     private static final IvParameterSpec IV_PARAMETER_SPEC = new IvParameterSpec(DEFAULT_IV);
50
51     protected final SecretKey secretKey;
52
53     /**
54      * Constructor<br/>
55      * <p>
56      * Creates default key.
57      * </p>
58      * 
59      * @since SDNO 0.5
60      */
61     public AesCipher() {
62         super();
63         secretKey = createSecretKey("default");
64     }
65
66     /**
67      * Constructor<br/>
68      * <p>
69      * </p>
70      * 
71      * @since SDNO 0.5
72      * @param ckey: key.
73      */
74     public AesCipher(final String key) {
75         super();
76         secretKey = createSecretKey(key);
77     }
78
79     private SecretKey createSecretKey(final String key) {
80         SecretKey secretKey = null;
81         try {
82             final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
83             final KeySpec keySpec = new PBEKeySpec(key.toCharArray(), DEFAULT_IV, 30000, 128);
84
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);
91         }
92         return null;
93     }
94
95     @Override
96     public String encrypt(final String plain) {
97         try {
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);
104             return null;
105         }
106     }
107
108     @Override
109     public String decrypt(final String encrypted) {
110
111         if(encrypted == null || encrypted.length() == 0) {
112             return null;
113         }
114
115         if(secretKey == null) {
116             return null;
117         }
118
119         try {
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);
127             return null;
128         }
129     }
130
131 }