3

我有 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;  

鉴于我的测试数据的长度,我正在避免任何填充问题。我究竟做错了什么?有什么建议么?

(不,您不必重新计算参数字符串的长度 - 我这样做了好几次。)

4

1 回答 1

8

Init 方法中的密钥大小参数以位为单位 - 如方法注释所述:

根据 Key 中的数据进行密钥设置,大小以位为单位

您正在计算 KeySize = 32 位的 AES,这是无效的。

因此,您计算最低可用密钥大小,即 128。返回的值对于 128 位是正确的 - 请参阅http://csrc.nist.gov/groups/STM/cavp/documents/aes/AESAVS.pdf第 20 页。

尝试指定 256 位作为密钥大小:

 DCP_rijndael1.Init(KeyBuf,256,@IVBuf);  
于 2011-11-29T16:40:31.707 回答