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