Page 1 of 1
Implement CRC algorithm
Posted: Mon Aug 22, 2016 12:06 am
by aahmadzadeh
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
Re: Implement CRC algorithm
Posted: Mon Aug 22, 2016 10:58 pm
by Tolice
The "ushort" data type ?It is not a standard data type, Is it yourself defined?
Re: Implement CRC algorithm
Posted: Mon Aug 22, 2016 11:19 pm
by aahmadzadeh
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
Re: Implement CRC algorithm
Posted: Mon Aug 22, 2016 11:55 pm
by Tolice
So, I don't understand the C#.
![Shocked :shock:](./images/smilies/icon_eek.gif)
Re: Implement CRC algorithm
Posted: Tue Aug 23, 2016 3:33 am
by mabel
Why don't you use CRC implementation of java card API?
It's not necessary to implement it yourself.
Re: Implement CRC algorithm
Posted: Tue Aug 23, 2016 7:10 am
by aahmadzadeh
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
Re: Implement CRC algorithm
Posted: Thu Aug 25, 2016 1:21 am
by UNKNwYSHSA
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.