DCPCrypt / Delphi无法正确编码Rijndael [英] DCPCrypt/Delphi not properly encoding Rijndael
问题描述
我有DCPCrypt软件包(最新版本),并尝试使用Delphi2007中的AES / Rijndael CBC编码(128位块,256位密钥),使用NIST分发的AES已知答案测试(KAT)向量中的测试值进行编码。一个样本测试向量:
I have the DCPCrypt package (latest version) and am trying to do AES/Rijndael CBC encoding (128 bit blocks, 256 bit key) in Delphi2007 with test values from the AES Known Answer Test (KAT) Vectors distributed by NIST. One sample test vector:
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;
鉴于测试数据的长度,我避免了任何填充问题。
我在做什么错?有什么建议吗?
Given the length of my test data I'm avoiding any padding issues. What am I doing wrong? Any suggestions?
(不,您不必重新计算参数字符串的长度-我已经做了几次。)
(No, you don't have to recount the parameter string lengths - I did that several times.)
推荐答案
Init方法中的密钥大小参数以位为单位-如方法注释所述:
Key size parameter in Init method is in bits - as stated by the method comment:
根据Key中的数据执行密钥设置,大小以位为单位
计算密钥大小为32位的AES,这是无效的。
You are calculating the AES with KeySize = 32 bits, which is invalid.
因此,您需要计算最小可用密钥大小,即128。返回值对于128位是正确的-参见 http://csrc.nist.gov/groups/STM/ cavp / documents / aes / AESAVS.pdf 第20页。
So you compute for the lowest available Keysize, which is 128. The returned value is correct for 128 bit - see http://csrc.nist.gov/groups/STM/cavp/documents/aes/AESAVS.pdf page 20.
尝试指定256位作为密钥大小:
Try to specify 256 bits as key size:
DCP_rijndael1.Init(KeyBuf,256,@IVBuf);
这篇关于DCPCrypt / Delphi无法正确编码Rijndael的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!