File size: 6,958 Bytes
67204ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

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 '<input>') |  For Decrypt use command (sh encodeco.sh d '<input>')  | 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());
				}
			}
		}
	}
}