Encryption problem in javacard
Hi,
I met a problem using DES encryption in my applet. The encryption process works fine only when the data to encrypt is surrounded by dots ('.'). I can't understand why... Without dots, I can only encrypt data which length doesn't exceed 7 characters. Here's my code :
publicstaticbyte[] encrypt(byte[] data)throws CryptoException{
Cipher cipher = Cipher.getInstance(Cipher.ALG_DES_CBC_PKCS5,false );//Test
cipher.init(getKey(),Cipher.MODE_ENCRYPT,IV,(short)0,(short)IV.length);
short blockSize = 8;
short cipherDataLength;
short remainder = (short)((short)data.length % blockSize);
if (remainder == (short)0)
cipherDataLength = (short)data.length;
else
cipherDataLength = (short)((short)data.length - remainder + blockSize);
byte[] cipherData =newbyte[cipherDataLength];
cipher.doFinal(data,(short)0,(short)data.length,cipherData,(short)0);
return cipherData;
}
publicstaticbyte[] decrypt(byte[] cipherData)throws CryptoException{
Cipher cipher = Cipher.getInstance(Cipher.ALG_DES_CBC_PKCS5,false );
cipher.init(getKey(),Cipher.MODE_DECRYPT,IV,(short)0,(short)IV.length);
short blockSize = 8;
short remainder = (short)(cipherData.length % blockSize);
short clearDataLength=0;
if (remainder == 0)
clearDataLength = (short)cipherData.length;
else
clearDataLength = (short)(cipherData.length - remainder + blockSize);
// Decrypt
byte[] clearData =newbyte[clearDataLength];
cipher.doFinal(cipherData,(short)0,(short)cipherData.length,clearData,(short)0);
return clearData;
}
Encrypting "Problem" will give me "Problem" with a strange character on the last letter "m" after decryption, whereas encrypting ".Problem." will return ".Problem." after decryption.
Have you any idea ?

