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 ?

[3674 byte] By [Darwiina] at [2007-11-27 10:58:09]
# 1

This looks strangely like JCE code but there is no constant Cipher.ALG_DES_CBC_PKCS5 in javax.crypto.Cipher so which library are you using?

If you use the JCE then you require none of the contortions of having to calculate the remainder.

sabre150a at 2007-7-29 12:13:50 > top of Java-index,Security,Cryptography...
# 2

> This looks strangely like JCE code but there is no

> constant Cipher.ALG_DES_CBC_PKCS5 in

> javax.crypto.Cipher so which library are you using?

I'm a newbie in writing javacard applet, so I'm not sure what a good JCE code must look. It could be good If you have some advices... ;)

> If you use the JCE then you require none of the

> contortions of having to calculate the remainder.

I'm using Javacard API 2.2.1. Cipher and ALG_DES_CBC_PKCS5 is part of this API.

Regards

Darwiina at 2007-7-29 12:13:50 > top of Java-index,Security,Cryptography...
# 3

> > This looks strangely like JCE code but there is no

> > constant Cipher.ALG_DES_CBC_PKCS5 in

> > javax.crypto.Cipher so which library are you

> using?

>

> I'm a newbie in writing javacard applet, so I'm not

> sure what a good JCE code must look. It could be good

> If you have some advices... ;)

>

> > If you use the JCE then you require none of the

> > contortions of having to calculate the remainder.

>

> I'm using Javacard API 2.2.1. Cipher and

> ALG_DES_CBC_PKCS5 is part of this API.

>

Sorry, others may be able to help but I know nothing about Javacard so I can't help.

sabre150a at 2007-7-29 12:13:50 > top of Java-index,Security,Cryptography...