Page 1 of 1

something wrong with TDES to encrypt data

Posted: Tue May 24, 2016 1:47 am
by Liguwu
Recently, I have completed an applet, in which I use TDES to encrypt data.
But now I am stuck with a problem that I could not pass dofinal function.
Can anyone point what happened?

Code: Select all

public void Encrypt3DES_CBC()
     {
          byte [] keydata = {(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11,(byte)0x11};
          byte [] input = {(byte)0x22,(byte)0x22,(byte)0x22};
          byte [] output = new byte [100];

          Cipher m_encryptCipher;
          DESKey m_desKey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, false);
               
          m_desKey.setKey(keydata,(short)0);
          m_encryptCipher = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M1, false);
          m_encryptCipher.init(m_desKey,Cipher.MODE_ENCRYPT);
         
          m_encryptCipher.doFinal(input,ISO7816.OFFSET_CDATA,(short)input.length,output,(short)0);
     }

Re: something wrong with TDES to encrypt data

Posted: Tue May 24, 2016 8:47 am
by Tarantino
There is something wrong with the last line of your code!
You have put the wrong offset into the input buffer.

Code: Select all

m_encryptCipher.doFinal(input,(short)0,(short)input.length,output,(short)0);

Re: something wrong with TDES to encrypt data

Posted: Tue May 24, 2016 11:42 pm
by Liguwu
Hi there! I have solved this problem. You really saved my day! Thanks so much