2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.ccadaptor;
26 import java.security.Provider;
27 import java.security.Provider.Service;
28 import java.security.Security;
29 import javax.crypto.Cipher;
30 import org.jasypt.contrib.org.apache.commons.codec_1_3.binary.Base64;
31 import org.jasypt.util.text.BasicTextEncryptor;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class is used to encapsulate the encryption and decryption support in one place and to provide a utility to
37 * encrypt and decrypt data.
39 public class EncryptionTool {
42 * The prefix we insert onto any data we encrypt so that we can tell if it is encrpyted later and therefore decrypt
45 @SuppressWarnings("nls")
46 public static final String ENCRYPTED_VALUE_PREFIX = "enc:";
48 * This lock object is used ONLY if the singleton has not been set up.
50 private static final Object lock = new Object();
52 * The salt is used to initialize the PBE (password Based Encrpytion) algorithm.
54 private static final byte[] DEFAULT_SALT = {
55 (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
58 * The iteration count used to initialize the PBE algorithm and to generate the key spec
60 private static final int ITERATION_COUNT = 20;
62 * The logger for this class.
64 private static final Logger LOG = LoggerFactory.getLogger(EncryptionTool.class);
66 * The algorithm to encrypt and decrpyt data is "Password (or passphrase) Based Encryption with Message Digest #5
67 * and the Data Encryption Standard", i.e., PBEWithMD5AndDES.
69 @SuppressWarnings("nls")
70 private static final String SECURITY_ALGORITHM = "PBEWITHMD5AND256BITAES";// "PBEWithMD5AndDES";
72 * The instance of the encryption utility object
74 private static EncryptionTool instance = null;
76 * The secret passphrase (PBE) that we use to perform encryption and decryption. The algorithm we are using is a
79 private static char[] secret = {
80 'C', '_', 'z', 'l', '!', 'K', '!', '4', '?', 'O', 'z', 'E', 'K', 'E', '>', 'U', 'R', '/', '%', 'Y', '\\', 'f',
81 'b', '"', 'e', 'n', '{', '"', 'l', 'U', 'F', '+', 'E', '\'', 'R', 'T', 'p', '1', 'V', '4', 'l', 'a', '9', 'w',
82 'v', '5', 'Z', '#', 'i', 'V', '"', 'd', 'l', '!', 'L', 'M', 'g', 'L', 'Q', '{', 'v', 'v', 'K', 'V'
85 * The decryption cipher object
87 private Cipher decryptCipher = null;
90 * The encryption cipher object
92 private Cipher encryptCipher = null;
94 private BasicTextEncryptor encryptor;
97 * Get an instance of the EncryptionTool
99 * @return The encryption tool to be used
101 public static final EncryptionTool getInstance() {
102 if (instance == null) {
103 synchronized (lock) {
104 if (instance == null) {
105 instance = new EncryptionTool();
113 * Create the EncryptionTool instance
115 @SuppressWarnings("nls")
116 private EncryptionTool() {
117 // encryptor = new BasicTextEncryptor();
118 // encryptor.setPassword(secret.toString());
119 String out = "Found the following security algorithms:";
120 for (Provider p : Security.getProviders()) {
121 for (Service s : p.getServices()) {
122 String algo = s.getAlgorithm();
124 String.format("%n -Algorithm [ %s ] in provider [ %s ] and service [ %s ]", algo, p.getName(),
132 * Decrypt the provided encrypted text
134 * @param cipherText THe cipher text to be decrypted. If the ciphertext is not encrypted, then it is returned as
136 * @return the clear test of the (possibly) encrypted value. The original value if the string is not encrypted.
138 @SuppressWarnings("nls")
139 public synchronized String decrypt(String cipherText) {
140 if (isEncrypted(cipherText)) {
141 String encValue = cipherText.substring(ENCRYPTED_VALUE_PREFIX.length());
142 // return encryptor.decrypt(encValue);
143 byte[] plainByte = Base64.decodeBase64(encValue.getBytes());
144 byte[] decryptByte = xorWithSecret(plainByte);
145 return new String(decryptByte);
153 * Encrypt the provided clear text
155 * @param clearText The clear text to be encrypted
156 * @return the encrypted text. If the clear text is empty (null or zero length), then an empty string is returned.
157 * If the clear text is already encrypted, it is not encrypted again and is returned as is. Otherwise, the clear
158 * text is encrypted and returned.
160 @SuppressWarnings("nls")
161 public synchronized String encrypt(String clearText) {
162 if (clearText != null) {
163 byte[] encByte = xorWithSecret(clearText.getBytes());
164 String encryptedValue = new String(Base64.encodeBase64(encByte));
165 return ENCRYPTED_VALUE_PREFIX + encryptedValue;
172 * Is a value encrypted? A value is considered to be encrypted if it begins with the {@linkplain
173 * #ENCRYPTED_VALUE_PREFIX encrypted value prefix}.
175 * @param value the value to check.
176 * @return true/false;
178 private static boolean isEncrypted(final String value) {
179 return value != null && value.startsWith(ENCRYPTED_VALUE_PREFIX);
183 * XORs the input byte array with the secret key, padding 0x0 to the end of the secret key if the input is longer
184 * and returns a byte array the same size as input
186 * @param inp The byte array to be XORed with secret
187 * @return A byte array the same size as inp or null if input is null.
189 private byte[] xorWithSecret(byte[] inp) {
194 byte[] secretBytes = new String(secret).getBytes();
195 int size = inp.length;
197 byte[] out = new byte[size];
198 for (int i = 0; i < size; i++) {
199 out[i] = (byte) ((inp[i]) ^ (secretBytes[i % secretBytes.length]));