2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.bpmn.common.util;
\r
24 import java.security.GeneralSecurityException;
\r
25 import java.security.NoSuchAlgorithmException;
\r
27 import javax.crypto.Cipher;
\r
28 import javax.crypto.spec.SecretKeySpec;
\r
30 //Need to add BPM error handler
\r
33 * CryptoUtils adapted from RTTP client.
\r
36 public class CryptoUtils {
\r
38 public static final String AES = "AES";
\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 * @throws IOException
\r
46 public static String encrypt(String value, String keyString) throws GeneralSecurityException
\r
48 SecretKeySpec sks = getSecretKeySpec(keyString);
\r
49 Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
\r
50 cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
\r
51 byte[] encrypted = cipher.doFinal(value.getBytes());
\r
52 return byteArrayToHexString(encrypted);
\r
57 * @throws GeneralSecurityException
\r
58 * @throws IOException
\r
60 public static String decrypt(String message, String keyString) throws GeneralSecurityException
\r
62 SecretKeySpec sks = getSecretKeySpec(keyString);
\r
63 Cipher cipher = Cipher.getInstance(CryptoUtils.AES);
\r
64 cipher.init(Cipher.DECRYPT_MODE, sks);
\r
65 byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));
\r
66 return new String(decrypted);
\r
69 private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException
\r
71 byte [] key = hexStringToByteArray(keyString);
\r
72 SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);
\r
77 private static String byteArrayToHexString(byte[] b){
\r
78 StringBuffer sb = new StringBuffer(b.length * 2);
\r
79 for (int i = 0; i < b.length; i++){
\r
80 int v = b[i] & 0xff;
\r
84 sb.append(Integer.toHexString(v));
\r
86 return sb.toString().toUpperCase();
\r
89 private static byte[] hexStringToByteArray(String s) {
\r
90 byte[] b = new byte[s.length() / 2];
\r
91 for (int i = 0; i < b.length; i++){
\r
93 int v = Integer.parseInt(s.substring(index, index + 2), 16);
\r
102 * Call Rttp utility jar to encrypt pwd
\r
103 * @param clearPassword
\r
105 * @throws GeneralSecurityException
\r
106 * @throws Exception
\r
108 public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {
\r
110 return RttpBasicAuth.encrypt(clearPassword);
\r
111 } catch (Exception e) {
\r
112 // wrap generic exception
\r
113 throw new GeneralSecurityException(e);
\r