workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / common-util / src / main / java / org / openo / baseservice / encrypt / cbb / impl / AesCipher.java
1 /*
2  * Copyright 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   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  
60      */
61     public AesCipher() {
62         super();
63         secretKey = createSecretKey("default");
64     }
65
66     /**
67      * 
68      * Constructor<br/>
69      * <p>
70      * </p>
71      * Creates key.
72      * @param key
73      * @since   
74      */
75     public AesCipher(final String key) {
76         super();
77         secretKey = createSecretKey(key);
78     }
79
80     private SecretKey createSecretKey(final String key) {
81         SecretKey secretKey = null;
82         try {
83             final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
84             final KeySpec keySpec = new PBEKeySpec(key.toCharArray(), DEFAULT_IV, 30000, 128);
85
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);
92         }
93         return null;
94     }
95
96     @Override
97     public String encrypt(final String plain) {
98         try {
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);
105             return null;
106         }
107     }
108
109     @Override
110     public String decrypt(final String encrypted) {
111
112         if(encrypted == null || encrypted.length() == 0) {
113             return null;
114         }
115
116         if(secretKey == null) {
117             return null;
118         }
119
120         try {
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);
128             return null;
129         }
130     }
131
132 }