[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / 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.common.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 //Need to add BPM error handler
31
32 /**
33  * CryptoUtils adapted from RTTP client.
34  *
35  */
36 public class CryptoUtils {
37
38     public static final String AES = "AES";
39
40     /**
41     * encrypt a value and generate a keyfile
42      * if the keyfile is not found then a new one is created
43     * @throws GeneralSecurityException
44      * @throws IOException
45      */
46     public static String encrypt(String value, String keyString) throws GeneralSecurityException
47     {
48         SecretKeySpec sks = getSecretKeySpec(keyString);
49         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
50         cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
51         byte[] encrypted = cipher.doFinal(value.getBytes());
52         return byteArrayToHexString(encrypted);
53     }
54
55     /**
56     * decrypt a value
57      * @throws GeneralSecurityException
58      * @throws IOException
59      */
60     public static String decrypt(String message, String keyString) throws GeneralSecurityException
61     {
62         SecretKeySpec sks = getSecretKeySpec(keyString);
63         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
64         cipher.init(Cipher.DECRYPT_MODE, sks);
65         byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
66         return new String(decrypted);
67     }
68
69     private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException
70     {
71         byte [] key = hexStringToByteArray(keyString);
72         SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);
73         return sks;
74     }
75
76
77     private static String byteArrayToHexString(byte[] b){
78         StringBuffer sb = new StringBuffer(b.length * 2);
79         for (int i = 0; i < b.length; i++){
80             int v = b[i] & 0xff;
81             if (v < 16) {
82                 sb.append('0');
83             }
84             sb.append(Integer.toHexString(v));
85     }
86         return sb.toString().toUpperCase();
87     }
88
89     private static byte[] hexStringToByteArray(String s) {
90         byte[] b = new byte[s.length() / 2];
91         for (int i = 0; i < b.length; i++){
92             int index = i * 2;
93             int v = Integer.parseInt(s.substring(index, index + 2), 16);
94             b[i] = (byte)v;
95         }
96         return b;
97     }
98
99     /**
100      * Not Used...
101      *
102     * Call Rttp utility jar to encrypt pwd
103     * @param clearPassword
104     * @return
105     * @throws GeneralSecurityException
106      * @throws Exception
107     *
108     public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {
109         try {
110             return RttpBasicAuth.encrypt(clearPassword);
111         } catch (Exception e) {
112             // wrap generic exception
113             throw new GeneralSecurityException(e);
114         }
115     }
116     */
117 }
118