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