1 package org.openecomp.sdc.securityutil;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
7 import javax.crypto.BadPaddingException;
8 import javax.crypto.Cipher;
9 import javax.crypto.IllegalBlockSizeException;
10 import javax.crypto.NoSuchPaddingException;
11 import javax.crypto.spec.SecretKeySpec;
12 import java.io.UnsupportedEncodingException;
13 import java.nio.charset.StandardCharsets;
14 import java.security.InvalidKeyException;
15 import java.security.Key;
16 import java.security.NoSuchAlgorithmException;
17 import java.util.Base64;
19 public class SecurityUtil {
21 private static final Logger LOG = LoggerFactory.getLogger( SecurityUtil.class );
22 private static final byte[] KEY = new byte[]{-64,5,-32 ,-117 ,-44,8,-39, 1, -9, 36,-46,-81, 62,-15,-63,-75};
23 public static final SecurityUtil INSTANCE = new SecurityUtil();
24 public static final String ALGORITHM = "AES" ;
25 public static final String CHARSET = StandardCharsets.UTF_8.name();
27 public static Key secKey = null ;
31 * cmd commands >$PROGRAM_NAME decrypt "$ENCRYPTED_MSG"
32 * >$PROGRAM_NAME encrypt "message"
35 private SecurityUtil(){ super(); }
39 secKey = generateKey( KEY, ALGORITHM );
42 LOG.warn("cannot generate key for {}", ALGORITHM);
48 public static Key generateKey(final byte[] KEY, String algorithm){
49 return new SecretKeySpec(KEY, algorithm);
52 //obfuscates key prefix -> **********
53 public String obfuscateKey(String sensitiveData){
55 if (sensitiveData != null){
56 int len = sensitiveData.length();
57 StringBuilder builder = new StringBuilder(sensitiveData);
58 for (int i=0; i<len/2; i++){
59 builder.setCharAt(i, '*');
61 return builder.toString();
67 * @param strDataToEncrypt - plain string to encrypt
69 * a. Declare / Initialize the Data. Here the data is of type String
70 * b. Convert the Input Text to Bytes
71 * c. Encrypt the bytes using doFinal method
73 public Either<String,String> encrypt(String strDataToEncrypt){
74 if (strDataToEncrypt != null ){
76 LOG.debug("Encrypt key -> {}", secKey);
77 Cipher aesCipherForEncryption = Cipher.getInstance("AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!!
78 aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secKey);
79 byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
80 byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);
81 String strCipherText = new String( Base64.getMimeEncoder().encode(byteCipherText), CHARSET );
82 LOG.debug("Cipher Text generated using AES is {}", strCipherText);
83 return Either.left(strCipherText);
84 } catch( NoSuchAlgorithmException | UnsupportedEncodingException e){
85 LOG.warn( "cannot encrypt data unknown algorithm or missing encoding for {}" ,secKey.getAlgorithm());
86 } catch( InvalidKeyException e){
87 LOG.warn( "invalid key recieved - > {} | {}" , Base64.getDecoder().decode( secKey.getEncoded() ), e.getMessage() );
88 } catch( IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e){
89 LOG.warn( "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding" , e.getMessage() );
92 return Either.right("Cannot encrypt "+strDataToEncrypt);
97 * @param byteCipherText - should be valid bae64 input in the length of 16bytes
98 * @param isBase64Decoded - is data already base64 encoded&aligned to 16 bytes
99 * a. Initialize a new instance of Cipher for Decryption (normally don't reuse the same object)
100 * b. Decrypt the cipher bytes using doFinal method
102 public Either<String,String> decrypt(byte[] byteCipherText , boolean isBase64Decoded){
103 if (byteCipherText != null){
104 byte[] alignedCipherText = byteCipherText;
107 alignedCipherText = Base64.getDecoder().decode(byteCipherText);
108 LOG.debug("Decrypt key -> "+secKey.getEncoded());
109 Cipher aesCipherForDecryption = Cipher.getInstance("AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!!
110 aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secKey);
111 byte[] byteDecryptedText = aesCipherForDecryption.doFinal(alignedCipherText);
112 String strDecryptedText = new String(byteDecryptedText);
113 LOG.debug("Decrypted Text message is: {}" , obfuscateKey( strDecryptedText ));
114 return Either.left(strDecryptedText);
115 } catch( NoSuchAlgorithmException e){
116 LOG.warn( "cannot encrypt data unknown algorithm or missing encoding for {}" ,secKey.getAlgorithm());
117 } catch( InvalidKeyException e){
118 LOG.warn( "invalid key recieved - > {} | {}" , Base64.getDecoder().decode( secKey.getEncoded() ), e.getMessage() );
119 } catch( IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e){
120 LOG.warn( "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding" , e.getMessage() );
123 return Either.right("Decrypt FAILED");
126 public Either<String,String> decrypt(String byteCipherText){
128 return decrypt(byteCipherText.getBytes(CHARSET),true);
129 } catch( UnsupportedEncodingException e ){
130 LOG.warn( "Missing encoding for {} | {} " ,secKey.getAlgorithm() , e.getMessage());
132 return Either.right("Decrypt FAILED");