-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathAESEncryption.java
99 lines (88 loc) · 3.1 KB
/
AESEncryption.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
90
91
92
93
94
95
96
97
98
99
package com.cdac.encrypt.aes;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/*
* import java.util.Base64; this class is available from java 1.8
* else you can use apache library for Base64
*/
public class AESEncryption {
public static void main(String[] args)
{
final String strToEncrypt = "My text to encrypt";
final String strPssword = "f2fc2007-b24b-4ab5-b62f-8dba873d0341";
encryptdecrypt.setKey(strPssword);
//encryptdecrypt is called from main funcation
encryptdecrypt.encrypt(strToEncrypt.trim());
System.out.println("String to Encrypt: " + strToEncrypt);
System.out.println("Encrypted: " + encryptdecrypt.getEncryptedString());
final String strToDecrypt = encryptdecrypt.getEncryptedString();
encryptdecrypt.decrypt(strToDecrypt.trim());
System.out.println("String To Decrypt : " + strToDecrypt);
System.out.println("Decrypted : " + encryptdecrypt.getDecryptedString());
}
}
//class encryptdecrypt increases the readability of the code. So, it is extracted to another new class.
//funcationality remains the same besides having refactoring
class encryptdecrypt
{
private static SecretKeySpec secretKey;
private static byte[] key;
private static String decryptedString;
private static String encryptedString;
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
System.out.println(key.length);
sha = MessageDigest.getInstance("SHA-256");
key = sha.digest(key);
//key = Arrays.copyOf(key, 32);
System.out.println(key.length);
System.out.println(new String(key, "UTF-8"));
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String getDecryptedString() {
return decryptedString;
}
public static void setDecryptedString(String decString) {
decryptedString = decString;
}
public static String getEncryptedString() {
return encryptedString;
}
public static void setEncryptedString(String encString) {
encryptedString = encString;
}
public static String encrypt(String strToEncrypt) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] buff = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
setEncryptedString(Base64.getEncoder().encodeToString(buff));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static String decrypt(String strToDecrypt) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
setDecryptedString(new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
}