3 * ============LICENSE_START=======================================================
\r
4 * Copyright (C) 2019 Nordix Foundation.
\r
5 * ================================================================================
\r
6 * Licensed under the Apache License, Version 2.0 (the "License");
\r
7 * you may not use this file except in compliance with the License.
\r
8 * You may obtain a copy of the License at
\r
10 * http://www.apache.org/licenses/LICENSE-2.0
\r
12 * Unless required by applicable law or agreed to in writing, software
\r
13 * distributed under the License is distributed on an "AS IS" BASIS,
\r
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 * See the License for the specific language governing permissions and
\r
16 * limitations under the License.
\r
18 * SPDX-License-Identifier: Apache-2.0
\r
19 * ============LICENSE_END=========================================================
\r
22 package org.onap.dmaap.datarouter.provisioning.utils;
\r
24 import java.nio.charset.StandardCharsets;
\r
25 import java.security.GeneralSecurityException;
\r
26 import java.util.Base64;
\r
28 import javax.crypto.Cipher;
\r
29 import javax.crypto.SecretKey;
\r
30 import javax.crypto.SecretKeyFactory;
\r
31 import javax.crypto.spec.PBEKeySpec;
\r
32 import javax.crypto.spec.PBEParameterSpec;
\r
35 * The Processing of a Password. Password can be encrypted and decrypted.
\r
36 * @author Vikram Singh
\r
37 * @version $Id: PasswordProcessor.java,v 1.0 2016/12/14 10:16:52 EST
\r
39 public class PasswordProcessor {
\r
41 private static final String SECRET_KEY_FACTORY_TYPE = "PBEWithMD5AndDES";
\r
42 private static final String PASSWORD_ENCRYPTION_STRING = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.passwordencryption");
\r
43 private static final char[] PASSWORD = PASSWORD_ENCRYPTION_STRING.toCharArray();
\r
44 private static final byte[] SALT = {(byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};
\r
46 private PasswordProcessor(){
\r
51 * @param property the Password
\r
52 * @return Encrypted password.
\r
54 public static String encrypt(String property) throws GeneralSecurityException {
\r
55 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);
\r
56 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
\r
57 Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);
\r
58 pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 32));
\r
59 return Base64.getEncoder().encodeToString(pbeCipher.doFinal(property.getBytes(StandardCharsets.UTF_8)));
\r
64 * @param property the Password
\r
65 * @return Decrypt password.
\r
67 public static String decrypt(String property) throws GeneralSecurityException {
\r
68 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);
\r
69 SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
\r
70 Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);
\r
71 pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 32));
\r
72 return new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), StandardCharsets.UTF_8);
\r