· 7 years ago · Aug 26, 2018, 03:00 PM
1import java.io.UnsupportedEncodingException;
2import java.security.MessageDigest;
3import java.security.NoSuchAlgorithmException;
4import java.security.SecureRandom;
5import java.security.spec.AlgorithmParameterSpec;
6import java.security.spec.InvalidKeySpecException;
7import java.security.spec.KeySpec;
8import java.util.Arrays;
9
10import javax.crypto.Cipher;
11import javax.crypto.SecretKey;
12import javax.crypto.SecretKeyFactory;
13import javax.crypto.spec.IvParameterSpec;
14import javax.crypto.spec.PBEKeySpec;
15import javax.crypto.spec.SecretKeySpec;
16import javax.print.DocFlavor.STRING;
17
18import org.apache.commons.codec.binary.Base64;
19
20
21
22public class AES {
23
24
25 private static SecretKeySpec secretKey;
26 private static byte[] key;
27
28 private static String decryptedString;
29 private static String encryptedString;
30
31
32 public static void setKey(String myKey) {
33
34
35 MessageDigest sha = null;
36 try {
37 key = myKey.getBytes("UTF-8");
38 System.out.println(key.length);
39 sha = MessageDigest.getInstance("SHA-1");
40 key = sha.digest(key);
41 key = Arrays.copyOf(key, 16); // use only first 128 bit
42 System.out.println(key.length);
43 System.out.println(new String(key, "UTF-8"));
44 secretKey = new SecretKeySpec(key, "AES");
45
46
47 } catch (NoSuchAlgorithmException e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 } catch (UnsupportedEncodingException e) {
51 // TODO Auto-generated catch block
52 e.printStackTrace();
53 }
54
55
56
57
58 }
59
60 public static String getDecryptedString() {
61 return decryptedString;
62 }
63
64 public static void setDecryptedString(String decryptedString) {
65 AES.decryptedString = decryptedString;
66 }
67
68 public static String getEncryptedString() {
69 return encryptedString;
70 }
71
72 public static void setEncryptedString(String encryptedString) {
73 AES.encryptedString = encryptedString;
74 }
75
76 public static String encrypt(String strToEncrypt) {
77 try {
78 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
79
80 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
81
82
83 setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
84
85 } catch (Exception e) {
86
87 System.out.println("Error while encrypting: " + e.toString());
88 }
89 return null;
90
91 }
92
93 public static String decrypt(String strToDecrypt) {
94 try {
95 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
96
97 cipher.init(Cipher.DECRYPT_MODE, secretKey);
98 setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));
99
100 } catch (Exception e) {
101
102 System.out.println("Error while decrypting: " + e.toString());
103
104 }
105 return null;
106 }
107
108
109 public static void main(String args[]) {
110
111 final String strToEncrypt = "My text to encrypt";
112 final String strPssword = "secret key";
113 AES.setKey(strPssword);
114
115 AES.encrypt(strToEncrypt.trim());
116
117 System.out.println("String to Encrypt: " + strToEncrypt);
118 System.out.println("Encrypted: " + AES.getEncryptedString());
119
120 final String strToDecrypt = AES.getEncryptedString();
121 AES.decrypt(strToDecrypt.trim());
122
123 System.out.println("String To Decrypt : " + strToDecrypt);
124 System.out.println("Decrypted : " + AES.getDecryptedString());
125
126 }
127
128}