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