· 7 years ago · Sep 08, 2018, 04:40 PM
1Decryption Error: Pad block corrupted
2byte[] input = etInput.getText().toString().getBytes();
3 byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
4 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
5
6 SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
7
8 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
9
10 // encryption pass
11 cipher.init(Cipher.ENCRYPT_MODE, key);
12
13 byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
14 int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
15 ctLength += cipher.doFinal(cipherText, ctLength);
16
17 cipher.init(Cipher.DECRYPT_MODE, key);
18 byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
19 int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
20
21 String strLength = new String(cipherText,"US-ASCII");
22 byte[] byteCiphterText = strLength.getBytes("US-ASCII");
23 Log.e("Decrypt", Integer.toString(byteCiphterText.length));
24
25 etOutput.setText(new String(cipherText,"US-ASCII"));
26
27 cipherText = etOutput.getText().toString().getBytes("US-ASCII");
28 Log.e("Decrypt", Integer.toString(cipherText.length));
29
30 ptLength += cipher.doFinal(plainText, ptLength);
31 Log.e("Decrypt", new String(plainText));
32 Log.e("Decrypt", Integer.toString(ptLength));
33
34ptLength += cipher.doFinal(plainText, ptLength);
35
36public String Encrypt(String strPlainText) throws Exception, NoSuchProviderException,
37 NoSuchPaddingException {
38 byte[] input = strPlainText.getBytes();
39 byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
40 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
41 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
42
43 SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
44
45 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
46
47 // encryption pass
48 cipher.init(Cipher.ENCRYPT_MODE, key);
49
50 byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
51 int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
52 ctLength += cipher.doFinal(cipherText, ctLength);
53
54 return new String(cipherText, "US-ASCII");
55}
56
57public String Decrypt(String strCipherText) throws Exception,
58 NoSuchProviderException, NoSuchPaddingException {
59 byte[] cipherText = strCipherText.getBytes("US-ASCII");
60 int ctLength = cipherText.length;
61 byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
62 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
63 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
64
65 SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
66
67 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
68
69 // decryption pass
70 cipher.init(Cipher.DECRYPT_MODE, key);
71 byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
72 int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
73 ptLength += cipher.doFinal(plainText, ptLength);
74
75 return new String(plainText);
76}
77
78// this is a quick example - dont use sun.misc inproduction
79// - go for some open source implementation
80String encryptedString = new sun.misc.BASE64Encoder.encodeBuffer(encryptedBytes);
81
82static public byte[] decrypt(Cipher cipher, SecretKey key, byte[]... bytes)
83 throws GeneralSecurityException, IOException {
84 cipher.init(Cipher.DECRYPT_MODE, key);
85 ByteArrayOutputStream bos = new ByteArrayOutputStream();
86 for (int i = 0; i < bytes.length; i++) {
87 bos.write(cipher.update(bytes[i]));
88 }
89 bos.write(cipher.doFinal());
90 return bos.toByteArray();
91}