I'm sorry that I posted too many questions on the same topic. I've been searching Google all day to solve this alone, but I couldn't solve it with my own ability. I am thinking about it, and I am posting the last question.
====Other Developer Settings====
Encryption key value: 1q2w3e4r
AES encryption settings)
Cipher mode: CBC
Key size = 16 bytes
Block size = 16 bytes
Ding pk : PKCS7
the right value ???????-> AES256-CBC-PKCS7 Value: gRI2SaW/HMknK/5tuJ2S9Q==
My value ???????-> AES256-CBC-PKCS7 Value: A8MAUAUIDpQcNEKNFqZDA==
I don't know why it's different when the results come up as above. The number of characters is the same, but the encrypted value is incorrect and cannot be approved. AES256 encryption is successful, but what is wrong? 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;
CodePudding user response:
I solved it with my senior developer.
If anyone is looking for it, I hope it helps. Please refer to the question for setting
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;