Too many passwords to remember? Use KeePassNFC to make your password management super easy.

KeePassNFC is a free open source implementation of the KeePass Password Safe for Android,which helps you to manage your passwords in a secure way.

Please see this article to know how to use KeePassNFC step by step.

Problem about AES algorithm

Algorithm School

Moderator: UNKNwYSHSA

Vlidda
Posts: 13
Joined: Tue Sep 06, 2016 2:59 am
Points: 158
Contact:

Problem about AES algorithm

Postby Vlidda » Thu Aug 10, 2017 11:24 pm

Hi Friends here,

I have encountered a problem when testing my applet, which implemented AES alg.

When running to the following line, it always breaks.

Code: Select all

aeskey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);


My code:

Code: Select all

package com.cpit.javacard;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.security.AESKey;
import javacard.security.DESKey;
import javacard.security.Key;
import javacard.security.KeyBuilder;
import javacardx.crypto.Cipher;

public class DES3 extends Applet
{

     final static byte AES = (byte) 0x50;

     public static void install(byte[] bArray, short bOffset, byte bLength)
     {
          new DES3().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
     }

     public boolean select()
     {
          return true;

     }

     public void deselect()
     {

          // reset the pin value

     }

     public void process(APDU apdu)
     {
          if (selectingApplet())
          {
               return;
          }

          byte[] buf = apdu.getBuffer();
          switch (buf[ISO7816.OFFSET_INS])
          {
               case AES:
                    Crypt_AES(apdu);
                    break;
               default:

                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
          }
     }

     private void Crypt_AES(APDU apdu)
     {

          byte[] buffer = apdu.getBuffer();

          Cipher CipherObj;
          AESKey aeskey;
          byte[] dkey = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08,
                    (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08 };

          byte[] cleartext = { (byte) 0x77, (byte) 0xA7, (byte) 0xd6, (byte) 0xbc, (byte) 0xf5, (byte) 0x79, (byte) 0x62, (byte) 0xb9,
                    (byte) 0x77, (byte) 0xA7, (byte) 0xd6, (byte) 0xbc, (byte) 0xf5, (byte) 0x79, (byte) 0x62, (byte) 0xb9 };

          CipherObj = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
          aeskey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
          aeskey.setKey(dkey, (short) 0);
          CipherObj.init((Key) aeskey, Cipher.MODE_ENCRYPT);
          apdu.setOutgoingLength((byte) 0x10);
          CipherObj.doFinal(cleartext, (short) 0, (byte) 0x10, buffer, (short) 0);
          apdu.sendBytes((short) 0, (byte) 0x10);

     }

}

User avatar
marjkbadboy
Posts: 28
Joined: Fri Jul 31, 2015 2:47 am
Points: 171
Contact:

Re: Problem about AES algorithm

Postby marjkbadboy » Fri Aug 11, 2017 3:48 am

You just forgot to switch to outgoing mode before setting the outgoingLength.

Code: Select all

apdu.setOutgoing();
apdu.setOutgoingLength((byte) 0x10);
It's hunting season!

Vlidda
Posts: 13
Joined: Tue Sep 06, 2016 2:59 am
Points: 158
Contact:

Re: Problem about AES algorithm

Postby Vlidda » Fri Aug 11, 2017 6:16 am

Thanks for your kind reply. I will add this code and then have a try.


Return to “Algorithm School”

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer