· 7 years ago · Sep 15, 2018, 03:50 AM
1IllegalBLockSizeException when decrypting
2public static String aes_encrypt (String text, String key)
3{
4 SecretKey skey = new SecretKeySpec(key.getBytes(), "AES");
5 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
6 cipher.init(Cipher.ENCRYPT_MODE, skey);
7
8 return new String((cipher.doFinal(text.getBytes())));
9
10}
11
12public static String aes_decrypt (String text, String key)
13{
14
15 SecretKey skey = new SecretKeySpec(key.getBytes(), "AES");
16 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
17 cipher.init(Cipher.DECRYPT_MODE, skey);
18
19 return new String((cipher.doFinal(text.getBytes())));
20}
21
22public static void main (String args[])
23{
24 String text = "Hello, world!";
25 String key = "nv93h50sk1zh508v";
26 String en, de;
27
28 System.out.println("Text: " + text);
29 System.out.println("Encrypted: " + (en = aes_encrypt(text, key))
30 + " length = " + en.length());
31 System.out.println("Decrypted: " + (de = aes_decrypt(en, key)));
32}