2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.util;
24 import java.security.GeneralSecurityException;
25 import java.security.NoSuchAlgorithmException;
27 import javax.crypto.Cipher;
28 import javax.crypto.spec.SecretKeySpec;
30 // import com.att.util.common.RttpBasicAuth;
32 //Need to add BPM error handler
35 * CryptoUtils adapted from RTTP client.
38 public class CryptoUtils {
40 public static final String AES = "AES";
43 * encrypt a value and generate a keyfile
44 * if the keyfile is not found then a new one is created
45 * @throws GeneralSecurityException
48 public static String encrypt(String value, String keyString) throws GeneralSecurityException
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);
59 * @throws GeneralSecurityException
62 public static String decrypt(String message, String keyString) throws GeneralSecurityException
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);
71 private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException
73 byte [] key = hexStringToByteArray(keyString);
74 SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);
79 private static String byteArrayToHexString(byte[] b){
80 StringBuffer sb = new StringBuffer(b.length * 2);
81 for (int i = 0; i < b.length; i++){
86 sb.append(Integer.toHexString(v));
88 return sb.toString().toUpperCase();
91 private static byte[] hexStringToByteArray(String s) {
92 byte[] b = new byte[s.length() / 2];
93 for (int i = 0; i < b.length; i++){
95 int v = Integer.parseInt(s.substring(index, index + 2), 16);
104 * Call Rttp utility jar to encrypt pwd
105 * @param clearPassword
107 * @throws GeneralSecurityException
110 public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {
112 return RttpBasicAuth.encrypt(clearPassword);
113 } catch (Exception e) {
114 // wrap generic exception
115 throw new GeneralSecurityException(e);