我有 DCPCrypt 包(最新版本),并试图在 Delphi2007 中使用 NIST 分发的 AES 已知答案测试(KAT)向量中的测试值进行 AES/Rijndael CBC 编码(128 位块,256 位密钥)。一个样本测试向量:
KEY = 0000000000000000000000000000000000000000000000000000000000000000
IV = 00000000000000000000000000000000
PLAINTEXT = 80000000000000000000000000000000
CIPHERTEXT = ddc6bf790c15760d8d9aeb6f9a75fd4e
下面的代码返回:
Cyphertext (bytes): 58 215 142 114 108 30 192 43 126 191 233 43 35 217 236 52
Cyphertext (hex): 3AD78E726C1EC02B7EBFE92B23D9EC34
Cyphertext (base64): OteOcmwewCt+v+krI9nsNA==
这显然是不正确的。
procedure TFrmKATVectors.TestData(Key,IV,PlainText: String);
var
InBuf,OutBuf: TestBuffer;
KeyBuf: KeyBuffer;
IVBuf: IVBuffer;
l,i: Integer;
Bytes,
SOut: String;
begin
Memo1.Lines.Add('Key: ' + Key);
Memo1.Lines.Add('IV: ' + IV);
Memo1.Lines.Add('Plaintext: ' + Plaintext);
l := Length(Key) DIV 2;
for i := 1 to l do KeyBuf[i] := HexToInt(Copy(Key,2*(i-1)+1,2));
l := Length(IV) DIV 2;
for i := 1 to l do IVBuf[i] := HexToInt(Copy(IV,2*(i-1)+1,2));
l := Length(PlainText) DIV 2;
for i := 1 to l do InBuf[i] := HexToInt(Copy(PlainText,2*(i-1)+1,2));
DCP_rijndael1.Init(KeyBuf,32,@IVBuf);
DCP_rijndael1.EncryptCBC(InBuf,OutBuf,TestBufSize);
SOut := '';
for i := 1 to Length(OutBuf) do
begin
SOut := SOut + Chr(OutBuf[i]);
Bytes := Bytes + IntToStr(OutBuf[i]) + ' ';
end;
Memo1.Lines.Add('Cyphertext (bytes): ' + Bytes);
Memo1.Lines.Add('Cyphertext (hex): ' + StringToHex(SOut));
Memo1.Lines.Add('Cyphertext (base64): ' + Base64EncodeStr(SOut));
Memo1.Lines.Add('');
end;
我打电话
TestData('0000000000000000000000000000000000000000000000000000000000000000',
'00000000000000000000000000000000', '80000000000000000000000000000000');
和
const
TestBufSize = 16;
type
TestBuffer = packed Array[1..TestBufSize] of Byte;
KeyBuffer = packed Array[1..32] of Byte;
IVBuffer = packed Array[1..16] of Byte;
鉴于我的测试数据的长度,我正在避免任何填充问题。我究竟做错了什么?有什么建议么?
(不,您不必重新计算参数字符串的长度 - 我这样做了好几次。)