【发布时间】:2014-01-06 05:05:33
【问题描述】:
上周我一直在尝试使用默认密钥 (00000000h) 使用 MIFARE DESFire EV1 卡进行身份验证,但无济于事。我已按照this blog 的指示写信。我像这样实现了Send mode CBC 和Receive mode CBC:
var
SendVector, ReceiveVector: UInt64;
procedure ResetVectors;
begin
SendVector := 0;
ReceiveVector := 0;
end;
procedure Encrypt(var Data: TBytes; Key: TBytes);
var
iData, iKey: UInt64;
i: Integer;
begin
if Length(Data) mod 8 > 0 then
SetLength(Data, Length(Data) + (8 - Length(Data) mod 8));
Move(Key[0], iKey, 8);
for i := 0 to (Length(Data) - 1) div 8 do
begin
Move(Data[i * 8], iData, 8);
EncryptInt64(iData, iKey);
Move(iData, Data[i * 8], 8);
end;
end;
procedure EncryptInt64(var Data, Key: Int64);
begin
Data := Data xor SendVector;
DESEncrypt(@Data, @Key);
SendVector := Data;
end;
procedure Decrypt(var Data: TBytes; Key: TBytes);
var
iData, iKey: UInt64;
i: Integer;
begin
Move(Key[0], iKey, 8);
for i := 0 to (Length(Data) - 1) div 8 do
begin
Move(Data[i * 8], iData, 8);
DecryptInt64(iData, iKey);
Move(iData, Data[i * 8], 8);
end;
end;
procedure DecryptInt64(var Data, Key: Int64);
var
Tmp: UInt64;
begin
Tmp := ReceiveVector;
ReceiveVector := Data;
DESDecrypt(@Data, @Key);
Data := Data xor Tmp;
end;
这是我发送到卡的 APDU 命令的日志,以及它们对应的响应:
-->90 6A 00 00 00 // List Applications
<--01 02 03
<--9100 (OK)
-->90 5A 00 00 03 00 00 00 00 // Select PICC
<--9100 (OK)
-->90 1A 00 00 01 00 00 // ISO Authenticate with master key (00000000h)
<--91AF
-->90 AF 00 00 00 // Retreive RndB
<--A4 4C 2B D1 EB 6F 64 0C
<--9100 (OK)
-->90 AF 00 00 10 0D 9F 27 9B A5 D8 72 60 25 DD 7A 19 63 0F 26 2D 00 // Send DES(RndA + RndB')
<--91AE (AUTHENTICATION_FAILURE)
这是我的Authenticate 方法的完整代码:
procedure Authenticate;
var
Key, Data: TBytes;
s: string;
b: Byte;
RndA: UInt64;
i: Integer;
begin
ResetVectors;
Key := HexStringToBuffer('00 00 00 00 00 00 00 00');
s := '90 1A 00 00 01 00 00';
s := SendAPDU(s, False);
Data := HexStringToBuffer(s);
Decrypt(Data, Key);
b := Data[0];
for i := 0 to 6 do
Data[i] := Data[i + 1];
Data[7] := b;
RndA := 1; // not very wise
SetLength(Data, 16);
Move(Data[0], Data[8], 8);
Move(RndA, Data[0], 8);
Encrypt(Data, Key);
s := '90 AF 00 00 10 ' + BufferToHexString(Data) + ' 00';
SendAPDU(s, False);
end;
我不知道为什么卡会断然拒绝我的身份验证尝试。有什么想法吗?
这是根据 DESFire EV1 制造商说明的 CBC 发送和 CBC 接收算法图:
【问题讨论】:
-
你链接的文章没有这样的代码。你有任何工作代码吗?例如一些示例 C 代码。 Length(Data) mod 8 看起来很奇怪。你确定吗? FillChar 调用可以是 := 0。
-
@DavidHeffernan 您对 SetLength() 和 FillChar 调用都是正确的。我修改了我的代码以更正它,但我仍然有问题。不幸的是,我在任何地方都找不到 working 代码......只需在这里和那里编写 sn-ps 代码。这是一个链接:stackoverflow.com/questions/11385963/…
-
也许this post 有帮助?伊曼,你用的是什么 DES 加密库?
-
@whosrdaddy 我正在使用一个库,我已经使用 ACOS3X 接触式智能卡很长一段时间了。我将那篇文章中的代码翻译成 Pascal,却得到同样的错误:-(
标签: delphi nfc delphi-xe5 des mifare