- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathAES.java
89 lines (87 loc) · 4.13 KB
/
AES.java
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
importjavax.crypto.Cipher;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.IvParameterSpec;
importjavax.crypto.spec.PBEKeySpec;
importjavax.crypto.spec.SecretKeySpec;
importjava.nio.charset.StandardCharsets;
importjava.security.InvalidAlgorithmParameterException;
importjava.security.InvalidKeyException;
importjava.security.NoSuchAlgorithmException;
importjava.security.spec.InvalidKeySpecException;
importjava.security.spec.KeySpec;
importjava.util.Base64;
importjavax.crypto.BadPaddingException;
importjavax.crypto.IllegalBlockSizeException;
importjavax.crypto.NoSuchPaddingException;
publicclassAESExample
{
/* Private variable declaration */
privatestaticfinalStringSECRET_KEY = "123456789";
privatestaticfinalStringSALTVALUE = "abcdefg";
/* Encryption Method */
publicstaticStringencrypt(StringstrToEncrypt)
{
try
{
/* Declare a byte array. */
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpecivspec = newIvParameterSpec(iv);
/* Create factory for secret keys. */
SecretKeyFactoryfactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
/* PBEKeySpec class implements KeySpec interface. */
KeySpecspec = newPBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
SecretKeytmp = factory.generateSecret(spec);
SecretKeySpecsecretKey = newSecretKeySpec(tmp.getEncoded(), "AES");
Ciphercipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
/* Retruns encrypted value. */
returnBase64.getEncoder()
.encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
}
catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingExceptione)
{
System.out.println("Error occured during encryption: " + e.toString());
}
returnnull;
}
/* Decryption Method */
publicstaticStringdecrypt(StringstrToDecrypt)
{
try
{
/* Declare a byte array. */
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpecivspec = newIvParameterSpec(iv);
/* Create factory for secret keys. */
SecretKeyFactoryfactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
/* PBEKeySpec class implements KeySpec interface. */
KeySpecspec = newPBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
SecretKeytmp = factory.generateSecret(spec);
SecretKeySpecsecretKey = newSecretKeySpec(tmp.getEncoded(), "AES");
Ciphercipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
/* Retruns decrypted value. */
returnnewString(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingExceptione)
{
System.out.println("Error occured during decryption: " + e.toString());
}
returnnull;
}
/* Driver Code */
publicstaticvoidmain(String[] args)
{
/* Message to be encrypted. */
Stringoriginalval = "AES Encryption";
/* Call the encrypt() method and store result of encryption. */
Stringencryptedval = encrypt(originalval);
/* Call the decrypt() method and store result of decryption. */
Stringdecryptedval = decrypt(encryptedval);
/* Display the original message, encrypted message and decrypted message on the console. */
System.out.println("Original value: " + originalval);
System.out.println("Encrypted value: " + encryptedval);
System.out.println("Decrypted value: " + decryptedval);
}
}