Exception pad block corrupted using AESLight from BouncyCastle
Hi all!
I磎 trying to encrypt and decrypt using AESLight from Bouncy Castle to provide cryptographi to my mobile application. I encrypt the text with no problems, and sometimes i decrypted with no problems to!
But, with some messages i got the follow exception:
org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted
at org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(Unknown Source)
at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source)
at softcomex.sfw2go.server.seguranca.TranslatorAESLight.processMsg(TranslatorAESLight.java:40)
at softcomex.sfw2go.server.Waiter.sendMessageToClient(Waiter.java:251)
at softcomex.sfw2go.server.Waiter.run(Waiter.java:211)
My class that uses AESLight:
public TranslatorAESLight (byte[] key,boolean cripto)
{
this.keyAES =newbyte[16];
System.arraycopy(key, 0, this.keyAES, 0, key.length>16?16:key.length);
this.cripto = cripto;
}
And, when I have to encrypt/decrypt i call the method processMsg:
publicbyte[] processMsg (String msg)
{
byte[] msgReturn =null;
int length = 0;
BlockCipher engine =new AESLightEngine();
BufferedBlockCipher cipher =new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
KeyParameter key =new KeyParameter(keyAES);
cipher.init(cripto, key);
msgReturn =newbyte[cipher.getOutputSize(msg.getBytes().length)];
length = cipher.processBytes(msg.getBytes(), 0, msg.getBytes().length, msgReturn, 0);
try{
cipher.doFinal(msgReturn, length);
}catch ( Exception e){
e.printStackTrace();
}
return msgReturn;
}
Please, there is something wrong with this code?
Thanks.
[2645 byte] By [
NetoMarina] at [2007-10-3 3:54:02]

> Please, there is something wrong with this code?
I'll bet there is something wrong with the code you haven't shown. The clues are that 1) your processMsg() method does double duty as both an encryptor and decryptor, 2) it takes a String and spits out a byte[], and 3) you are using the default character encoding.
The question is, on the decrypt side, how do you take the raw bytes and make them into a String that get passed to processMsg()? Wait, don't answer that, because it probably doesn't matter how you do it, it is wrong!
I believe sabre150 has posted a good link in this forum explaining why this doesn't work, but the bottom line is that you cannot create a String from an arbitrary byte array.
> I believe sabre150 has posted a good link in this
> forum explaining why this doesn't work, but the
> bottom line is that you cannot create a String from
> an arbitrary byte array.
I can't remember a single definitive reference (my memory is suspect these days) but I do have an example that shows why one should not try to convert bytes to a string using new String(bytes).
I got the basics of the Levenshtein algorithm on this forum somewhere but I can't find it. If anyone can give me the reference I will acknowledge it in the code.
import java.util.*;
import java.text.*;
import java.nio.charset.*;
public class BytesToStringErrorsDemonstration
{
public static int levenshteinDistance( byte[] s, byte[] t)
{
int n = s.length;
int m = t.length;
if (n == 0) return m;
if (m == 0) return n;
int[][] d = new int[n + 1][m + 1];
for ( int i = 0; i <= n; d[i][0] = i++ );
for ( int j = 1; j <= m; d[0][j] = j++ );
for ( int i = 1; i <= n; i++ )
{
byte sc = s[ i-1 ];
for (int j = 1; j <= m;j++)
{
int v = d[i-1][j-1];
if ( t[ j-1 ] != sc ) v++;
d[i][j] = Math.min( Math.min( d[i-1][ j] + 1, d[i][j-1] + 1 ), v );
}
}
return d[n][m];
}
public static void main(String[] args)
{
// Create a set of random bytes
Random random = new Random();
byte[] originalBytes = new byte[2048];
random.nextBytes(originalBytes);
// Go through each supported character set
SortedMap<String,Charset> availableCharsets = Charset.availableCharsets();
for (String encoding : availableCharsets.keySet())
{
System.out.print(encoding);
// Turn the bytes into a String
String string;
try
{
string = new String(originalBytes, encoding);
}
catch (Exception e)
{
System.out.println(" - unable to convert bytes to String");
continue;
}
// Get back the bytes from the string
byte[] recoveredBytes;
try
{
recoveredBytes = string.getBytes(encoding);
}
catch (Exception e)
{
System.out.println(" - unable to convert String back to bytes");
continue;
}
// Test the original against the recovered
boolean areSame = Arrays.equals(originalBytes, recoveredBytes);
if (areSame)
{
System.out.println(" - no differences");
}
else
{
final DecimalFormat percentFormatter = new DecimalFormat("0.0");
int levenshteinCount = levenshteinDistance(originalBytes, recoveredBytes);
System.out.println(" - differences - Levenshtein count = " + levenshteinCount + " (" + percentFormatter.format(100.0 * levenshteinCount / originalBytes.length) + " %)");
}
}
}
}