· 8 years ago · Jan 13, 2018, 03:10 PM
1Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
2 at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
3 at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
4 at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
5 at javax.crypto.Cipher.doFinal(Cipher.java:2165)
6 at io.crypto.Crypto.doFinal(Crypto.java:60)
7 at io.crypto.Crypto.decrypt(Crypto.java:50)
8 at io.Controller.main(Controller.java:38)
9
10public class Crypto {
11
12 private static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'b', 'c', 'D', 'e', 'F'};
13 private static Cipher cipher;
14
15 public static void init() {
16
17 try {
18 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
19 } catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
20 NetworkModule.handleException(e);
21 }
22
23 }
24
25 public static String encrypt(String password, String message) throws Exception {
26
27 String salt = random(16);
28 String iv = random(16);
29 SecretKey key = generateKey(salt, password);
30 byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, message.getBytes("UTF-8"));
31 String code = Base64.getEncoder().encodeToString(encrypted);
32 return salt + code.substring(0, code.length() - 2) + iv;
33
34 }
35
36 public static String decrypt(String password, String message) throws Exception {
37
38 String salt = message.substring(0, 32);
39 String iv = message.substring(message.length() - 32, message.length());
40 String base = message.substring(32, message.length() - 32) + "==";
41 SecretKey key = generateKey(salt, password);
42 byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, Base64.getDecoder().decode(base));
43 return new String(decrypted, "UTF-8");
44
45 }
46
47 private static byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {
48
49 try {
50
51 cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
52 return cipher.doFinal(bytes);
53
54 } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
55 NetworkModule.handleException(e);
56 return null;
57 }
58
59 }
60
61 private static SecretKey generateKey(String salt, String passphrase) {
62
63 try {
64
65 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
66 KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), 1000, 128);
67 SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
68 return key;
69
70 } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
71 NetworkModule.handleException(e);
72 return null;
73 }
74
75 }
76
77 private static String random(int length) {
78 byte[] salt = new byte[length];
79 new SecureRandom().nextBytes(salt);
80 return hex(salt);
81 }
82
83 private static String hex(byte[] data) {
84
85 int l = data.length;
86 char[] out = new char[l << 1];
87 int i = 0;
88
89 for (int var5 = 0; i < l; ++i) {
90 out[var5++] = HEX[(240 & data[i]) >>> 4];
91 out[var5++] = HEX[15 & data[i]];
92 }
93
94 return new String(out);
95
96 }
97
98 private static byte[] hex(String hex) {
99
100 char[] data = hex.toCharArray();
101 int len = data.length;
102
103 if ((len & 1) != 0) {
104 return null;
105 } else {
106
107 byte[] out = new byte[len >> 1];
108 int i = 0;
109
110 for (int j = 0; j < len; ++i) {
111
112 int f = Character.digit(data[j], 16) << 4;
113 ++j;
114 f |= Character.digit(data[j], 16);
115 ++j;
116 out[i] = (byte) (f & 255);
117
118 }
119
120 return out;
121
122 }
123
124 }
125
126}
127
128Crypto.init();
129String password = "42cb54a0b6a89a53709301ee320f45de102dda05ccd1a49c3c62c19b7319ca73";
130String message = "Hello World";
131System.out.println(message);
132String encrypt = Crypto.encrypt(password, message);
133System.out.println(encrypt);
134String decrypt = Crypto.decrypt(password, encrypt);
135System.out.println(decrypt);
136
137Crypto.init();
138String password = "42cb54a0b6a89a53709301ee320f45de102dda05ccd1a49c3c62c19b7319ca73";
139String message = new PacketBuilder("example").build();
140System.out.println(message);
141String encrypt = Crypto.encrypt(password, message);
142System.out.println(encrypt);
143String decrypt = Crypto.decrypt(password, encrypt);
144System.out.println(decrypt);