Page 1 of 1

DES Encrypt/Decrypt

Posted: Wed Mar 23, 2016 7:48 am
by Danieken
I have completed an applet. But when I run my code, JCWDE answer to me with this Status Word: 00 05. That is Illegal Use. Could anyone give me any advice or comments? thx

Here is my code.

Code: Select all

byte[] key1 = {(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11};
byte[] cipherText = {(byte)0x9e,(byte)0x90,(byte)0xde,(byte)0x82,(byte)0x74,(byte)0x5e,(byte)0x78,(byte)0x52};
byte[] plainText = new byte[(short)8];

DESKey deskey =  (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES,KeyBuilder.LENGTH_DES, false); 

desKey.setKey(key1,(short)0);

Cipher cipherDES = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M2 , false);

cipherDES.init(deskey ,Cipher.MODE_DECRYPT);

try
{
     cipherDES.doFinal(cipherText, (short)0, (short)cipherText.length, plainText, (short)0 );
}
catch(CryptoException c)
{
     if(c.getReason()==CryptoException.UNINITIALIZED_KEY)
          ISOException.throwIt(CryptoException.UNINITIALIZED_KEY);
         
     else if (c.getReason()==CryptoException.ILLEGAL_USE)
          ISOException.throwIt(CryptoException.ILLEGAL_USE);
               
     else if(c.getReason()==CryptoException.INVALID_INIT)
          ISOException.throwIt(CryptoException.INVALID_INIT);
}

Re: DES Encrypt/Decrypt

Posted: Wed Mar 23, 2016 8:32 am
by Tarantino
Is cipherText correct? As I know, one of the ILLEGAL_USE exception reasons is that padding is incorrect.

Re: DES Encrypt/Decrypt

Posted: Wed Mar 23, 2016 8:49 am
by Danieken
Tarantino wrote:Is cipherText correct? As I know, one of the ILLEGAL_USE exception reasons is that padding is incorrect.


I have crypted this

Code: Select all

byte[] plainText = {(byte)0x11,(byte)0x22,(byte)0x33,(byte)0x44,(byte)0x55,(byte)0x66,(byte)0x77,(byte)0x88};


with this key:

Code: Select all

byte[] key = {(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11};


the Result is:

Code: Select all

byte[] cipherText = {(byte)0x9e,(byte)0x90,(byte)0xde,(byte)0x82,(byte)0x74,(byte)0x5e,(byte)0x78,(byte)0x52,(byte)0x1c,(byte)0xd6,(byte)0xc3,(byte)0xf6,(byte)0xea,(byte)0x4d,(byte)0x9d,(byte)0x64};


But what do I have to pass to the doFinal() method, in order to obtain the plaintext from the ciphertext?

Re: DES Encrypt/Decrypt

Posted: Thu Mar 24, 2016 9:01 am
by Tarantino
You have to pass full encrypted string, it is 16 byte length because padding was added before encryption.