c926903cf324f8bccb6e2c4cbf62b4bdf3e66d9b
[so.git] / common / src / main / java / org / openecomp / mso / utils / 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.utils;
22
23
24
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.security.NoSuchAlgorithmException;
28
29 import javax.crypto.Cipher;
30 import javax.crypto.spec.SecretKeySpec;
31
32
33 /**
34  * CryptoUtils adapted from RTTP client.
35  * 
36  */
37 public final class CryptoUtils {
38
39     public static final String AES = "AES";
40
41     /**
42      * encrypt a value and generate a keyfile
43      * if the keyfile is not found then a new one is created
44      * 
45      * @throws GeneralSecurityException
46      */
47     public static String encrypt (String value, String keyString) throws GeneralSecurityException {
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      * 
58      * @throws GeneralSecurityException
59      */
60     public static String decrypt (String message, String keyString) throws GeneralSecurityException {
61         SecretKeySpec sks = getSecretKeySpec (keyString);
62         Cipher cipher = Cipher.getInstance (CryptoUtils.AES);
63         cipher.init (Cipher.DECRYPT_MODE, sks);
64         byte[] decrypted = cipher.doFinal (hexStringToByteArray (message));
65         return new String (decrypted);
66     }
67
68     private static SecretKeySpec getSecretKeySpec (String keyString) throws NoSuchAlgorithmException {
69         byte[] key = hexStringToByteArray (keyString);
70         SecretKeySpec sks = new SecretKeySpec (key, CryptoUtils.AES);
71         return sks;
72     }
73
74     public static String byteArrayToHexString (byte[] b) {
75         StringBuffer sb = new StringBuffer (b.length * 2);
76         for (int i = 0; i < b.length; i++) {
77             int v = b[i] & 0xff;
78             if (v < 16) {
79                 sb.append ('0');
80             }
81             sb.append (Integer.toHexString (v));
82         }
83         return sb.toString ().toUpperCase ();
84     }
85
86     private static byte[] hexStringToByteArray (String s) {
87         byte[] b = new byte[s.length () / 2];
88         for (int i = 0; i < b.length; i++) {
89             int index = i * 2;
90             int v = Integer.parseInt (s.substring (index, index + 2), 16);
91             b[i] = (byte) v;
92         }
93         return b;
94     }
95 }