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.common.util;
24 import java.security.GeneralSecurityException;
25 import java.security.NoSuchAlgorithmException;
27 import javax.crypto.Cipher;
28 import javax.crypto.spec.SecretKeySpec;
30 //Need to add BPM error handler
33 * CryptoUtils adapted from RTTP client.
36 public class CryptoUtils {
38 public static final String AES = "AES";
41 * encrypt a value and generate a keyfile
42 * if the keyfile is not found then a new one is created
43 * @throws GeneralSecurityException
46 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);
57 * @throws GeneralSecurityException
60 public static String decrypt(String message, String keyString) throws GeneralSecurityException
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);
69 private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException
71 byte [] key = hexStringToByteArray(keyString);
72 SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);
77 private static String byteArrayToHexString(byte[] b){
78 StringBuffer sb = new StringBuffer(b.length * 2);
79 for (int i = 0; i < b.length; i++){
84 sb.append(Integer.toHexString(v));
86 return sb.toString().toUpperCase();
89 private static byte[] hexStringToByteArray(String s) {
90 byte[] b = new byte[s.length() / 2];
91 for (int i = 0; i < b.length; i++){
93 int v = Integer.parseInt(s.substring(index, index + 2), 16);
102 * Call Rttp utility jar to encrypt pwd
103 * @param clearPassword
105 * @throws GeneralSecurityException
108 public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {
110 return RttpBasicAuth.encrypt(clearPassword);
111 } catch (Exception e) {
112 // wrap generic exception
113 throw new GeneralSecurityException(e);