Here is my
Plain APDU : 00 A4 00 00
kSMac: 2F 34 E0 0D 1C 46 76 67 43 BA 6B 26 AB 20 4F D3
SSC:20 29 73 0A 6D 5E 7C C4
I am working on constructing a secure message and I am following an example and I am getting a wrong Mac value.
Here is what I have done:
byte[] modifiedAPDU = new byte[] { 0x0C, 0xA4, 0x00, 0x00 };
//pad the APDU
byte[] paddedAPDU = Pad(modifiedAPDU);
//append Apdu to SSC
byte[] apduWithSSC = new byte[SSC.Length + paddedAPDU.Length];
Array.Copy(SSC, apduWithSSC, SSC.Length);
Array.Copy(paddedAPDU, 0, apduWithSSC, SSC.Length, paddedAPDU.Length);
//print the APDU with SSC
Console.WriteLine("APDU with SSC: " + BitConverter.ToString(apduWithSSC).Replace("-", ""));
byte[] mac = CalculateMac(paddedAPDU, KSMac);
CalculateMac()
static byte[] CalculateMac(byte[] data, byte[] key)
{
// Initialize DES engine
DesEngine desEngine = new DesEngine();
// Initialize ISO9797Alg3Mac with DES engine, 64-bit block size, and ISO7816d4Padding
ISO9797Alg3Mac mac = new ISO9797Alg3Mac(desEngine, 64, new ISO7816d4Padding());
// Initialize with the key
KeyParameter keyParameter = new KeyParameter(key);
mac.Init(keyParameter);
// Update with the data
mac.BlockUpdate(data, 0, data.Length);
// Compute MAC
byte[] output = new byte[mac.GetMacSize()];
mac.DoFinal(output, 0);
return output;
}
Pad
static byte[] PadData(byte[] data)
{
int blockSize = 8; // Block size for 3DES
int paddingLength = blockSize - (data.Length % blockSize);
byte[] paddedData = new byte[data.Length + paddingLength];
Array.Copy(data, paddedData, data.Length);
paddedData[data.Length] = 0x80; // Append 0x80
for (int i = data.Length + 1; i < paddedData.Length; i++)
{
paddedData[i] = 0x00; // Append 0x00
}
return paddedData;
}
Expected Mac= A8 FB 97 D2 99 C8 05 2B, what could be the reason I not getting it – I am getting 7B 21 7B 67 47 42 3C FD