Hello,
Anyone know what night be causing a "pad block corrupted" exception to
be raised with decryption on Android? Here is the scenario:
1. On Windows Java app (NetBeans) read a small file, encrypt its
contents and write it as a new encrypted file.
2. adb push the encrypted file to an Android
3. On Android, open the encrypted file and try to decrypt using same
cipher parameters as set on the Windows side and it gets the "pad
block corrupted" error.
(Running the decryption code on within the Windows Java app decrypts
OK, no "pad block corrupted" error)
Thank you for any ideas!
Here are the relevant code fragments with the trying/catching removed
for clarity.
On Windows Java app (NetBeans)
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new
SecureRandom("MyKey".getBytes());
kgen.init(128, secureRandom);
SecretKey skey = kgen.generateKey();
rawKey = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
FileOutputStream fileOutputStream = new FileOutputStream(new
File("filename"));
CipherOutputStream cipherOutputStream = new
CipherOutputStream(fileOutputStream, cipher);
int numRead = 0;
byte[] buf = new byte[1024];
while ((numRead = iS.read(buf)) >= 0) // iS is an input stream
{
cipherOutputStream.write(buf, 0, numRead);
}
cipherOutputStream.close();
Use ADB to push the encrypted file to the Android
On Android
String cryptKey = "MyKey";
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new
SecureRandom(cryptKey.getBytes());
kgen.init(128, secureRandom);
SecretKey skey = kgen.generateKey();
rawKey = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
FileInputStream fileInputStream = new FileInputStream("filename");
CipherInputStream cipherInputStream = new
CipherInputStream(fileInputStream, cipher);
int numRead;
byte[] buf = new byte[1024];
try {
while ((numRead = cipherInputStream.read(buf)) >= 0)
{
if (retStr==null)
retStr = new String(buf, 0, numRead);
else
retStr = retStr.concat(new String(buf, 0,
numRead));
}
cipherInputStream.close();
} catch (IOException e1) {
// *************
// This exception is raised with message "pad block
corrupted"
// *************
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en