Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

Implement CRC algorithm

Algorithm School

Moderator: UNKNwYSHSA

aahmadzadeh
Posts: 27
Joined: Mon Sep 28, 2015 2:31 am
Points :400
Contact:

Implement CRC algorithm

Post by aahmadzadeh » Mon Aug 22, 2016 12:06 am

How can i implement bellow CRC algorithm in java card (without using of Int data type)?
My problem is "ushort" data type, and also i think here byte is unsigned value.

Code: Select all

ushort __crcBDefault = 0x6363;

private ushort UpdateCrc(byte b, ushort crc)
{
      byte ch = (byte)(b ^ (byte)(crc & 0x00ff));
      ch = (byte)(ch ^ (ch << 4));
      return (ushort)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
}

public ushort ComputeCrc(byte[] bytes)
{
   var res = __crcBDefault;
   foreach (var b in bytes)
      res = UpdateCrc(b, res);
   return res;
}

sample input: 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
current output: 0xCA 0XDD

thank you

User avatar
Tolice
Posts: 40
Joined: Wed May 20, 2015 2:41 am
Points :254
Contact:

Re: Implement CRC algorithm

Post by Tolice » Mon Aug 22, 2016 10:58 pm

The "ushort" data type ?It is not a standard data type, Is it yourself defined?

aahmadzadeh
Posts: 27
Joined: Mon Sep 28, 2015 2:31 am
Points :400
Contact:

Re: Implement CRC algorithm

Post by aahmadzadeh » Mon Aug 22, 2016 11:19 pm

Tolice wrote:The "ushort" data type ?It is not a standard data type, Is it yourself defined?


ushort is a data type in C#
ushort = unsigned short

User avatar
Tolice
Posts: 40
Joined: Wed May 20, 2015 2:41 am
Points :254
Contact:

Re: Implement CRC algorithm

Post by Tolice » Mon Aug 22, 2016 11:55 pm

So, I don't understand the C#. :shock:

User avatar
mabel
Posts: 237
Joined: Mon May 18, 2015 3:09 am
Points :1705
Contact:

Re: Implement CRC algorithm

Post by mabel » Tue Aug 23, 2016 3:33 am

Why don't you use CRC implementation of java card API?

It's not necessary to implement it yourself.

aahmadzadeh
Posts: 27
Joined: Mon Sep 28, 2015 2:31 am
Points :400
Contact:

Re: Implement CRC algorithm

Post by aahmadzadeh » Tue Aug 23, 2016 7:10 am

mabel wrote:Why don't you use CRC implementation of java card API?

It's not necessary to implement it yourself.

Because i need special type of CRC which dose not implemented in JavaCard

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3053
Contact:

Re: Implement CRC algorithm

Post by UNKNwYSHSA » Thu Aug 25, 2016 1:21 am

Code:

Code: Select all

package testCRC;

import javacard.framework.*;

public class testCRC extends Applet
{

   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new testCRC().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }
   
   short __crcBDefault = 0x6363;

   private short UpdateCrc(byte b, short crc)
   {
      byte ch = (byte)(b ^ (byte)(crc & 0x00FF));
      ch = (byte)(ch ^ ((ch << 4) & 0xF0));
      return (short)(((crc >> 8) & 0x00FF) ^ ((ch << 8) & 0xFF00) ^ ((ch << 3) & 0x07F8) ^ ((ch >> 4) & 0x000F));
   }

   public short ComputeCrc(byte[] bytes, short offset, short length)
   {
      short res = __crcBDefault;
      for (short i = 0; i < length; ++i) {
         byte b = bytes[(short) (offset + i)];
        res = UpdateCrc(b, res);
      }

      return res;
   }

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

      byte[] buf = apdu.getBuffer();
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte)0x00:
         break;
      case (byte)0x01:
         short dataLen = apdu.setIncomingAndReceive();
         short crc = ComputeCrc(buf, ISO7816.OFFSET_CDATA, dataLen);
         Util.setShort(buf, (short) 0, crc);
         apdu.setOutgoingAndSend((short) 0, (short) 2);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }

}

Test:

Code: Select all

>> /select 112233445500
>> 00 A4 04 00 06 11 22 33 44 55 00 00
<< 90 00

>> /send 000100001011223344556677889900112233445566
>> 00 01 00 00 10 11 22 33 44 55 66 77 88 99 00 11 22 33 44 55 66
<< DD CA 90 00


Notice:
Make sure that the shift operation of type java short is same as type unsigned short.
sense and simplicity

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 16 guests

JavaCard OS : Disclaimer