gpt4 book ai didi

delphi - DCPCrypt/Delphi 未正确编码 Rijndael

转载 作者:行者123 更新时间:2023-12-02 04:09:04 31 4
gpt4 key购买 nike

我有 DCPCrypt 包(最新版本),并且正在尝试使用来自 NIST 分发的 AES 已知答案测试 (KAT) 向量的测试值在 Delphi2007 中进行 AES/Rijndael CBC 编码(128 位 block ,256 位 key )。一个样本测试向量:

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;

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

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

最佳答案

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

Do key setup based on the data in Key, size is in bits

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

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

尝试指定 256 位作为 key 大小:

 DCP_rijndael1.Init(KeyBuf,256,@IVBuf);  

关于delphi - DCPCrypt/Delphi 未正确编码 Rijndael,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8313992/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com