· 9 years ago · Oct 01, 2016, 05:00 PM
1try{
2 String plainData="my name is laksahan",cipherText,decryptedText;
3 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
4 keyGen.init(128);
5 SecretKey secretKey = keyGen.generateKey();
6 Cipher aesCipher = Cipher.getInstance("AES");
7 aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
8 byte[] byteDataToEncrypt = plainData.getBytes();
9 byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
10 cipherText = new BASE64Encoder().encode(byteCipherText);
11System.out.println(cipherText);
12}catch(Exception e){
13
14}
15
16import javax.crypto.Cipher;
17import javax.crypto.KeyGenerator;
18import javax.crypto.SecretKey;
19import sun.misc.BASE64Encoder;
20import sun.misc.BASE64Decoder;
21
22public class AESExample
23{
24 public static void main(String[] args)
25 {
26 try
27 {
28 String plainData = "my name is laksahan", cipherText, decryptedText;
29 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
30 keyGen.init(128);
31 SecretKey secretKey = keyGen.generateKey();
32 cipherText = encrypt(plainData, secretKey);
33 System.out.println(cipherText);
34 decryptedText = decrypt(cipherText, secretKey);
35 System.out.println(decryptedText);
36 } catch (Exception e)
37 {
38 e.printStackTrace();
39 }
40
41 }
42
43 public static String encrypt(String plainData, SecretKey secretKey) throws Exception
44 {
45 Cipher aesCipher = Cipher.getInstance("AES");
46 aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
47 byte[] byteDataToEncrypt = plainData.getBytes();
48 byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
49 return new BASE64Encoder().encode(byteCipherText);
50 }
51
52 public static String decrypt(String cipherData, SecretKey secretKey) throws Exception
53 {
54 byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
55 Cipher aesCipher = Cipher.getInstance("AES");
56 aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
57 byte[] plainData = aesCipher.doFinal(data);
58 return new String(plainData);
59 }
60
61}
62
63import javax.crypto.Cipher;
64import javax.crypto.spec.SecretKeySpec;
65
66import sun.misc.BASE64Encoder;
67import sun.misc.BASE64Decoder;
68
69public class AESExample
70{
71
72 public static void main(String[] args)
73 {
74 try
75 {
76 byte[]key={-4, -14, 106, -75, -9, 65, -95, 77, -52, 73, -87, -101, 80, 94, -59, -66};
77 String plainData = "my name is laksahan", cipherText, decryptedText;
78 System.out.println(key.length);
79 cipherText = encrypt(plainData, key);
80 System.out.println(cipherText);
81 decryptedText = decrypt(cipherText, key);
82 System.out.println(decryptedText);
83 } catch (Exception e)
84 {
85 e.printStackTrace();
86 }
87 }
88
89 public static String encrypt(String plainData, byte[] key) throws Exception
90 {
91 Cipher aesCipher = Cipher.getInstance("AES");
92 SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
93 aesCipher.init(Cipher.ENCRYPT_MODE, keySpec);
94 byte[] byteDataToEncrypt = plainData.getBytes();
95 byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
96 return new BASE64Encoder().encode(byteCipherText);
97 }
98
99 public static String decrypt(String cipherData, byte[] key) throws Exception
100 {
101 byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
102 SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
103 Cipher aesCipher = Cipher.getInstance("AES");
104 aesCipher.init(Cipher.DECRYPT_MODE, keySpec);
105 byte[] plainData = aesCipher.doFinal(data);
106 return new String(plainData);
107 }
108
109}
110
111byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
112 Cipher aesCipher = Cipher.getInstance("AES");
113 aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting);
114 byte[] plainData = aesCipher.doFinal(data);
115 return new String(plainData);