import java.security.SecureRandom; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import java.security.SecureRandom; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; import java.util.Set; /** * Encoder/Decoder based on AES. * * @author Shiv */ public class AESUtils { // private static final String IV = "F27D5C9927726BCEFE7510B1BDD3D137"; // private static final String SALT = "3FF2EC019C627B945225DEBAD71A01B6985FE84C95A70EB132882F88C0A59A55"; // private static final int KEY_SIZE = 128; // private static final int ITERATION_COUNT = 10000; // private static final String PASSPHRASE = getDecodedPassphrase(); // // private AESUtils() {} // // protected static String getDecodedPassphrase() { // byte[] encodedBytes = { 86, 71, 104, 112, 99, 121, 66, 112, 99, 121, 66, 109, 98, 51, 74, 108, 99, 50, 108, 110, 97, // 72, 81, 103, 90, 109, 57, 121, 73, 71, 86, 117, 89, 51, 74, 53, 99, 72, 81, 103, 89, 87, 53, 107, 73, 71, 82, // 108, 89, 51, 74, 53, 99, 72, 81, 61 }; // return new String(Base64.decodeBase64(encodedBytes)); // } // // public static String encrypt(String plainText) { // AESEncoder util = new AESEncoder(KEY_SIZE, ITERATION_COUNT); // return util.encrypt(SALT, IV, PASSPHRASE, plainText); // } // // public static String decrypt(String cipherText) { // AESEncoder util = new AESEncoder(KEY_SIZE, ITERATION_COUNT); // return util.decrypt(SALT, IV, PASSPHRASE, cipherText); // } private static final int KEY_SIZE = 128; private static final int ITERATION_COUNT = 10000; private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String SECRET_KEY_ALGORITHM = "PBKDF2WithHmacSHA256"; private static final String PASSPHRASE = getDecodedPassphrase(); private AESUtils() {} protected static String getDecodedPassphrase() { byte[] encodedBytes = { 86, 71, 104, 112, 99, 121, 66, 112, 99, 121, 66, 109, 98, 51, 74, 108, 99, 50, 108, 110, 97, 72, 81, 103, 90, 109, 57, 121, 73, 71, 86, 117, 89, 51, 74, 53, 99, 72, 81, 103, 89, 87, 53, 107, 73, 71, 82, 108, 89, 51, 74, 53, 99, 72, 81, 61 }; return new String(Base64.decodeBase64(encodedBytes)); } public static String encrypt(String plainText) throws Exception { SecureRandom secureRandom = new SecureRandom(); byte[] iv = new byte[16]; // 16 bytes for a 128-bit AES block size byte[] salt = new byte[16]; secureRandom.nextBytes(iv); secureRandom.nextBytes(salt); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); SecretKeySpec secretKeySpec = generateSecretKeySpec(salt); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); String ivBase64 = Base64.encodeBase64String(iv); String saltBase64 = Base64.encodeBase64String(salt); String encryptedBase64 = Base64.encodeBase64String(encryptedBytes); // Structuring output as iv:salt:encryptedData return ivBase64 + ":" + saltBase64 + ":" + encryptedBase64; } public static String decrypt(String cipherText) throws Exception { String[] parts = cipherText.split(":"); if (parts.length != 3) { throw new IllegalArgumentException("Invalid encrypted text format"); } byte[] iv = Base64.decodeBase64(parts[0]); byte[] salt = Base64.decodeBase64(parts[1]); byte[] encryptedBytes = Base64.decodeBase64(parts[2]); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); SecretKeySpec secretKeySpec = generateSecretKeySpec(salt); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, "UTF-8"); } private static SecretKeySpec generateSecretKeySpec(byte[] salt) throws Exception { KeySpec keySpec = new PBEKeySpec(PASSPHRASE.toCharArray(), salt, ITERATION_COUNT, KEY_SIZE); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SECRET_KEY_ALGORITHM); byte[] keyBytes = secretKeyFactory.generateSecret(keySpec).getEncoded(); return new SecretKeySpec(keyBytes, "AES"); } //public static void main(String[] args) throws Exception //{ //System.out.println("COMMONS_STORAGE_ENDPOINT "+encrypt("jdbc:mysql://mysql-platform-cluster.ansible.svc.cluster.local:6446/PLATFORM?autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false&noAccessToProcedureBodies=true&disableSslHostnameVerification=true")); //} public static void main(String args[]) throws Exception { //System.out.println(args.length); if (args.length < 2) { System.out.println("ArrayIndexOutOfBoundsException: 1, For Encrypt use command (sh encodeco.sh e '') | For Decrypt use command (sh encodeco.sh d '') | Note: Input must be in single quote" ); } else if (args[0].toString().equals("e")) { System.out.println("Encrypted One"); System.out.println(AESUtils.encrypt(args[1].toString())); } else if (args[0].toString().equals("d")) { System.out.println("Decrypted One"); if(args[1].contains(".properties")) { System.out.println("Decrypted file " + args[1]); decodePropertiesFile(args[1]); }else { System.out.println("Decrypted One"); System.out.println(AESUtils.decrypt(args[1].toString())); } } } private static void decodePropertiesFile(String file) throws FileNotFoundException, IOException { FileReader reader = new FileReader(file); Properties p = new Properties(); p.load(reader); Set s = p.keySet(); for (Object o : s) { String v = (String) p.get(o); if (v.contains(",")) { String[] s1 = v.split(","); for (String s2 : s1) { try { System.out.println(o+"===="+s2+"======="+AESUtils.decrypt(s2.trim())); } catch (Exception e) { System.out.println(e.getMessage()); } } } else { try { System.out.println(o+"===="+v+"========="+AESUtils.decrypt(v.trim())); } catch (Exception e) { System.out.println(e.getMessage()); } } } } }