#1 2024-09-24 08:45:41

anouri
Member
Registered: 2024-02-11
Posts: 47

Text encrypted with aes in mormot2 is not decrypted by tms aes

var
  PlainText: string;
  CipherText: RawByteString;
  Bytes: TBytes;
begin
  Key := GenerateRandomString(32);
  PlainText := 'да, это работает. спасибо';
  Bytes := TEncoding.UTF8.GetBytes(PlainText);
  akey := HexToBin(SHA256(Key));
  AES := TAesGcm.Create(pointer(akey)^, 256);
  try
    Bytes := AES.EncryptPkcs7(Bytes,  True);
    CipherText := TEncoding.ANSI.GetString(Bytes);
    Result := BinToBase64(CipherText);
  finally
    AES.Free;
  end;

Decrypt with mormot2 is ok and return original text properly.
other party tools like tms saye:
No mapping for the Unicode character in target multi-byte

Last edited by anouri (2024-09-24 10:31:43)

Offline

#2 2024-09-24 09:55:49

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Text encrypted with aes in mormot2 is not decrypted by tms aes

I test result in online site like devglan. for same key and IV result is different.

Offline

#3 2024-09-24 10:04:08

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

Re: Text encrypted with aes in mormot2 is not decrypted by tms aes

Your code is weird / incorrect:
- the Bytes variable is not set before encryption.
- TAesGcm is not to be used as a plain cipher, only via EncryptPkcs7(): you need to use the proper AEAD method also after, via AesGcmFinal().

The error message "No mapping for the Unicode character in target multi-byte" looks like you did made some confusion between text encodings in your own code.

Note that we validate our AES-GCM code with some reference vectors, and also with OpenSSL itself.
Properly used, it works.

If you are lost with the low-level TAesGcm class, you could just use the high-level CipherAlgo('aes-gcm') which uses the regular format with IV + cipheredtext + AEAD signature.

Offline

#4 2024-09-24 10:30:50

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Text encrypted with aes in mormot2 is not decrypted by tms aes

It is correct in actual code and I forgot to add here:

Bytes := TEncoding.UTF8.GetBytes(PlainText);
before encryption

Last edited by anouri (2024-09-24 10:31:08)

Offline

#5 2024-09-24 13:36:30

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Text encrypted with aes in mormot2 is not decrypted by tms aes

I use encrypt method and same again ?
It is decrypted with motmot, but with another tool it gives no multi byte string error

var
  key: string;
  PlainText: string;
  akey : RawByteString;
  CryptCipher: ICryptCipher;
  dest: RawByteString;
  InputRawString: RawByteString;
  Bytes: TBytes;
begin
  PlainText := 'да, это работает. спасибо';
  Key := '12345678901234567890123456789012';
  akey := HexToBin(SHA256(Key));
  Bytes := TEncoding.UTF8.GetBytes(PlainText);
  SetString(InputRawString, PAnsiChar(PByte(Bytes)), Length(Bytes));
  CryptCipher := Encrypt('aes-256-gcm', Pointer(akey));
  CryptCipher.Process(InputRawString, dest, '');
  dest := BinToBase64(dest);
  Memo2.Text := dest;

Offline

#6 2024-09-24 15:37:42

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Text encrypted with aes in mormot2 is not decrypted by tms aes

I think AES/GCM/NOPADDING used by tms and mormot uses pkc7 pasdding. and it is different

Offline

Board footer

Powered by FluxBB