Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOGammaBPMN / src / main / java / org / openecomp / mso / bpmn / util / CryptoUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.util;
22
23
24 import java.security.GeneralSecurityException;
25 import java.security.NoSuchAlgorithmException;
26
27 import javax.crypto.Cipher;
28 import javax.crypto.spec.SecretKeySpec;
29
30 // import com.att.util.common.RttpBasicAuth;  
31
32 //Need to add BPM error handler
33
34 /**
35  * CryptoUtils adapted from RTTP client.
36  * 
37  */
38 public class CryptoUtils {
39
40     public static final String AES = "AES";
41
42     /**
43     * encrypt a value and generate a keyfile 
44      * if the keyfile is not found then a new one is created
45     * @throws GeneralSecurityException 
46      * @throws IOException 
47      */
48     public static String encrypt(String value, String keyString) throws GeneralSecurityException
49     {
50         SecretKeySpec sks = getSecretKeySpec(keyString);
51         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
52         cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
53         byte[] encrypted = cipher.doFinal(value.getBytes());
54         return byteArrayToHexString(encrypted);
55     }
56
57     /**
58     * decrypt a value  
59      * @throws GeneralSecurityException 
60      * @throws IOException 
61      */
62     public static String decrypt(String message, String keyString) throws GeneralSecurityException
63     {
64         SecretKeySpec sks = getSecretKeySpec(keyString);
65         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
66         cipher.init(Cipher.DECRYPT_MODE, sks);
67         byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
68         return new String(decrypted);
69     }
70
71     private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException
72     {
73         byte [] key = hexStringToByteArray(keyString);
74         SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);
75         return sks;
76     }
77
78
79     private static String byteArrayToHexString(byte[] b){
80         StringBuffer sb = new StringBuffer(b.length * 2);
81         for (int i = 0; i < b.length; i++){
82             int v = b[i] & 0xff;
83             if (v < 16) {
84                 sb.append('0');
85             }
86             sb.append(Integer.toHexString(v));
87     }
88         return sb.toString().toUpperCase();
89     }
90
91     private static byte[] hexStringToByteArray(String s) {
92         byte[] b = new byte[s.length() / 2];
93         for (int i = 0; i < b.length; i++){
94             int index = i * 2;
95             int v = Integer.parseInt(s.substring(index, index + 2), 16);
96             b[i] = (byte)v;
97         }
98         return b;
99     }
100
101     /**
102      * Not Used... 
103      * 
104     * Call Rttp utility jar to encrypt pwd
105     * @param clearPassword
106     * @return
107     * @throws GeneralSecurityException 
108      * @throws Exception
109     *
110     public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {
111         try {
112             return RttpBasicAuth.encrypt(clearPassword);
113         } catch (Exception e) {
114             // wrap generic exception
115             throw new GeneralSecurityException(e);
116         }
117     }
118     */
119 }
120