You are not logged in.
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
I test result in online site like devglan. for same key and IV result is different.
Offline
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
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
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
I think AES/GCM/NOPADDING used by tms and mormot uses pkc7 pasdding. and it is different
Offline