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