2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.main;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.UnsupportedEncodingException;
27 import java.security.InvalidKeyException;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.SecureRandom;
30 import java.util.Scanner;
32 import javax.crypto.BadPaddingException;
33 import javax.crypto.Cipher;
34 import javax.crypto.IllegalBlockSizeException;
35 import javax.crypto.NoSuchPaddingException;
36 import javax.crypto.spec.IvParameterSpec;
37 import javax.crypto.spec.SecretKeySpec;
39 import org.apache.commons.codec.binary.Base64;
40 import org.apache.commons.lang3.ArrayUtils;
41 import org.onap.music.eelf.logging.EELFLoggerDelegate;
43 public class CipherUtil {
49 private static String keyString = null;
51 private static final String ALGORITHM = "AES";
52 private static final String ALGORYTHM_DETAILS = ALGORITHM + "/CBC/PKCS5PADDING";
53 private static final int BLOCK_SIZE = 128;
54 @SuppressWarnings("unused")
55 private static SecretKeySpec secretKeySpec;
56 private static IvParameterSpec ivspec;
57 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CipherUtil.class);
59 * @deprecated Please use {@link #encryptPKC(String)} to encrypt the text.
61 * Encrypts the text using the specified secret key.
66 * Key to use for encryption
67 * @return encrypted version of plain text.
69 * if any encryption step fails
73 public static String encrypt(String plainText, String secretKey) {
74 String encryptedString = null;
76 byte[] encryptText = plainText.getBytes("UTF-8");
77 byte[] rawKey = Base64.decodeBase64(secretKey);
78 SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES");
79 Cipher cipher = Cipher.getInstance("AES");
80 cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
81 encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText));
82 } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException
83 | NoSuchPaddingException | UnsupportedEncodingException ex) {
85 return encryptedString;
89 * @deprecated Please use {@link #encryptPKC(String)} to encrypt the text.
90 * Encrypts the text using the secret key in key.properties file.
94 * @return Encrypted Text
96 * if any decryption step fails
99 public static String encrypt(String plainText) {
100 return CipherUtil.encrypt(plainText, keyString);
104 * Encrypts the text using a secret key.
108 * @return Encrypted Text
110 * if any decryption step fails
112 public static String encryptPKC(String plainText) {
113 return CipherUtil.encryptPKC(plainText, keyString);
118 * @deprecated Please use {@link #decryptPKC(String)} to Decryption the text.
120 * Decrypts the text using the specified secret key.
122 * @param encryptedText
125 * Key to use for decryption
126 * @return plain text version of encrypted text
128 * if any decryption step fails
132 public static String decrypt(String encryptedText, String secretKey) {
133 String encryptedString = null;
135 byte[] rawKey = Base64.decodeBase64(secretKey);
136 SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES");
137 byte[] encryptText = Base64.decodeBase64(encryptedText.getBytes("UTF-8"));
138 Cipher cipher = Cipher.getInstance("AES");
139 cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
140 encryptedString = new String(cipher.doFinal(encryptText));
141 } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException
142 | NoSuchPaddingException | UnsupportedEncodingException ex) {
144 return encryptedString;
147 private static SecretKeySpec getSecretKeySpec() {
148 byte[] key = Base64.decodeBase64(keyString);
149 return new SecretKeySpec(key, ALGORITHM);
152 private static SecretKeySpec getSecretKeySpec(String keyString) {
153 byte[] key = Base64.decodeBase64(keyString);
154 return new SecretKeySpec(key, ALGORITHM);
158 * Encrypt the text using the secret key in key.properties file
161 * @return The encrypted string
162 * @throws BadPaddingException
164 * In case of issue with the encryption
166 public static String encryptPKC(String value, String skey) {
167 Cipher cipher = null;
168 byte[] iv = null, finalByte = null;
171 cipher = Cipher.getInstance(ALGORYTHM_DETAILS, "SunJCE");
173 SecureRandom r = SecureRandom.getInstance("SHA1PRNG");
174 iv = new byte[BLOCK_SIZE / 8];
176 ivspec = new IvParameterSpec(iv);
177 cipher.init(Cipher.ENCRYPT_MODE, getSecretKeySpec(skey), ivspec);
178 finalByte = cipher.doFinal(value.getBytes());
180 } catch (Exception ex) {
183 return Base64.encodeBase64String(ArrayUtils.addAll(iv, finalByte));
187 * Decrypts the text using the secret key in key.properties file.
190 * The encrypted string that must be decrypted using the ecomp
192 * @return The String decrypted
194 * if any decryption step fails
196 public static String decryptPKC(String message, String skey) {
197 byte[] encryptedMessage = Base64.decodeBase64(message);
199 byte[] decrypted = null;
201 cipher = Cipher.getInstance(ALGORYTHM_DETAILS, "SunJCE");
202 ivspec = new IvParameterSpec(ArrayUtils.subarray(encryptedMessage, 0, BLOCK_SIZE / 8));
203 byte[] realData = ArrayUtils.subarray(encryptedMessage, BLOCK_SIZE / 8, encryptedMessage.length);
204 cipher.init(Cipher.DECRYPT_MODE, getSecretKeySpec(skey), ivspec);
205 decrypted = cipher.doFinal(realData);
207 } catch (Exception ex) {
212 return new String(decrypted);
216 * @deprecated Please use {@link #decryptPKC(String)} to Decrypt the text.
218 * Decrypts the text using the secret key in key.properties file.
220 * @param encryptedText
222 * @return Decrypted text
224 * if any decryption step fails
227 public static String decrypt(String encryptedText) {
228 return CipherUtil.decrypt(encryptedText, keyString);
233 * Decrypts the text using the secret key in key.properties file.
235 * @param encryptedText
237 * @return Decrypted text
239 * if any decryption step fails
241 public static String decryptPKC(String encryptedText) {
242 return CipherUtil.decryptPKC(encryptedText, keyString);
246 public static void readAndSetKeyString() {
248 Scanner in = new Scanner(new FileReader("/opt/app/music/etc/properties.txt"));
249 StringBuilder sb = new StringBuilder();
250 while(in.hasNext()) {
251 sb.append(in.next());
254 keyString = sb.toString();
255 } catch (FileNotFoundException e) {
256 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
260 /*public static void main(String[] args) {
262 System.out.println("Encrypted password: "+encryptPKC("cassandra"));
264 System.out.println("Decrypted password: "+decryptPKC("dDhqAp5/RwZbl9yRSZg15fN7Qul9eiE/JFkKemtTib0="));
265 System.out.println("Decrypted password: "+decryptPKC("I/dOtD/YYzBStbtOYhKuUUyPHSW2G9ZzdSyB8bJp4vk="));
266 System.out.println("Decrypted password: "+decryptPKC("g7zJqg74dLsH/fyL7I75b4eySy3pbMS2xVqkrB5lDl8="));