Hi all!
I�m 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 = new byte[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:
public byte[] 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 = new byte[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.