当我转换到Delphi AES256并且值错误时会发生什么

  • 本文关键字:错误 转换 Delphi AES256 delphi
  • 更新时间 :
  • 英文 :


很抱歉我发布了太多关于同一主题的问题。我一个人在谷歌上搜索了一整天,但我无法用自己的能力解决这个问题。我正在考虑这个问题,我正在发布最后一个问题。

====Other Developer Settings====
Encryption key value: 1q2w3e4r
AES encryption settings)
Cipher mode: CBC
Key size = 16 bytes
Block size = 16 bytes
Ding pk : PKCS7

正确的价值->AES256-CBC-PKCS7值:gRI2SaW/HMknK/5tuJ2S9Q==

我的价值->AES256-CBC-PKCS7值:A8MAUAUIDpQcNEKNFqZDA==

我不知道为什么当结果如上所述时会有所不同。字符数相同,但加密值不正确,无法批准。AES256加密是成功的,但哪里出了问题?ToT

function EncryptData(Data: string; AKey: AnsiString; AIv: AnsiString): string;
var
cipher: TDCP_rijndael;
key, iv, src, dest, b64: TBytes;
index, slen, bsize, pad: integer;
begin
//key := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AKey));
//iv := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AIv));
key := TEncoding.UTF8.GetBytes('1q2w3e4r1q2w3e4r1q2w3e4r1q2w3e4r');
iv := TEncoding.UTF8.GetBytes('1q2w3e4r1q2w3e4r');
src := TEncoding.UTF8.GetBytes(Data);
cipher := TDCP_rijndael.Create(nil);
try
cipher.CipherMode := cmCBC;
// Add padding.
// Resize the Value array to make it a multiple of the block length.
// If it's already an exact multiple then add a full block of padding.
slen := Length(src);
bsize := (cipher.BlockSize div 8);
pad := bsize - (slen mod bsize);
Inc(slen, pad);
SetLength(src, slen);
for index := pad downto 1 do
begin
src[slen - index] := pad;
end;
SetLength(dest, slen);
cipher.Init(key[0], 256, @iv[0]); // DCP uses key size in BITS not BYTES
cipher.Encrypt(src[0], dest[0], slen);
b64 := Base64EncodeBytes(dest);
result := TEncoding.Default.GetString(b64);
finally
cipher.Free;
end;
end;
function Base64EncodeBytes(Input: TBytes): TBytes;
var
ilen: integer;
begin
ilen := Length(Input);
SetLength(result, ((ilen + 2) div 3) * 4);
Base64Encode(@Input[0], @result[0], ilen);
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
Edit2.Text := EncryptData(EditCarNumber.Text,'','');
end;

我和我的高级开发人员一起解决了这个问题。

如果有人在找它,我希望它能有所帮助。请参阅设置的问题

function EncryptData(Data: string; AKey: AnsiString; AIv: AnsiString): string;
var
cipher: TDCP_rijndael;
key, iv, src, dest, b64: TBytes;
index, slen, bsize, pad: integer;
begin
key := TEncoding.UTF8.GetBytes('1q2w3e4r');       //Specify key value
iv := TEncoding.UTF8.GetBytes('1q2w3e4r');        //Specify iv value
SetLength(key, 16);                               //Set the key value to 16 bytes
SetLength(iv, 16);                                //Set the iv value to 16 bytes
src := TEncoding.UTF8.GetBytes(Data);
cipher := TDCP_rijndael.Create(nil);
try
cipher.CipherMode := cmCBC;
slen := Length(src);
bsize := (cipher.BlockSize div 8);             //The block size is 8byte
pad := bsize - (slen mod bsize);
Inc(slen, pad);
SetLength(src, slen);
for index := pad downto 1 do
begin
src[slen - index] := pad;
end;
SetLength(dest, slen);
cipher.Init(key[0], 128, @iv[0]);             //key[0], 128bit, @iv[0]take as
cipher.Encrypt(src[0], dest[0], slen);
b64 := Base64EncodeBytes(dest);              //Encoding received values to Base64 values
result := TEncoding.Default.GetString(b64);  //Get b64 value by string.
finally
cipher.Free;
end;
end;

最新更新