#1 2023-08-14 14:40:17

narkotik
Member
Registered: 2023-08-14
Posts: 1

Aes 256 Ecb Encytpt

Hello, I need AES 256 ecb encryption. I do encryption, but the result is different from online editors. What's the problem

function EncryptItAES(const s: string; const AKey: TBytes): string;
var
  Key: TSHA256Digest;
  Aes: TAESECB;
  _s: RawByteString;
begin
  Result := EmptyStr;
  Key := SHA256Digest(Pointer(AKey), Length(AKey));
  Aes := TAESECB.Create(Key, 256);
  try
    _s := StringToUTF8(s);
    _s := Aes.EncryptPKCS7(_s, False);
    _s := BinToBase64(_s);
    Result := UTF8ToString(_s);
  finally
    Aes.Free();
  end;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  Enc: TEncoding;
  Key : TBytes;
  s : String;
begin
  Enc := TEncoding.ANSI;
  Key := Enc.GetBytes('Xk1891Js3TdaGNiXdFboGLOXHfLF0exu');
  Memo1.Lines.Text := EncryptItAES('deneme',Key);
end;

Editor

Offline

#2 2023-08-14 18:37:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,654
Website

Re: Aes 256 Ecb Encytpt

I don't know.

Are you sure it is AES 256 ECB with PKCS7 padding on both sides?
Is the output totally diverse, or only the last bytes, or only the beginning?
ECB has no IV needed by definition (it is a weak unchained mode) so keep IVAtBeginning=false.

For information, the mORMot AES modes are all validated against reference vectors, and OpenSSL.
So the raw result should be correct.
There may be some trouble with the key computation, or string-to-bytes conversion, between both sides/languages.

And don't use ECB it is a weak and unsafe mode.
https://en.wikipedia.org/wiki/Block_cip … book_(ECB)

Online

Board footer

Powered by FluxBB