#1 2025-01-09 12:08:15

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

AES GCM Issue

Encrypted string does not decrypt with AESDecrypt with error: TAesGcm.DecryptPkcs7: Invalid Input.

function AESEncrypt2(PlainText: string; Key: RawByteString): string;
var
  akey : RawByteString;
  CryptCipher: ICryptCipher;
  dest,Bytes: TBytes;
begin
  akey := HexToBin(SHA256(Key));
  Bytes := TEncoding.UTF8.GetBytes(PlainText);
  CryptCipher := Encrypt('aes-256-gcm', Pointer(akey));
  CryptCipher.Process(Bytes, dest, nil);
  Result := TNetEncoding.Base64.EncodeBytesToString(dest);
end;
function AESDecrypt(CipherText: string; Key: RawByteString): string;
var
  AES: mormot.crypt.core.TAesGcm;
  akey : RawByteString;
begin
  akey := HexToBin(SHA256(Key));
  AES := mormot.crypt.core.TAesGcm.Create(pointer(akey)^, 256);
  try
    Result := AES.DecryptPkcs7(Base64ToBin(CipherText), False);
  finally
    AES.Free;
  end;
end;

Last edited by anouri (2025-01-09 12:30:13)

Offline

#2 2025-01-09 12:56:02

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

Re: AES GCM Issue

You need to follow TCryptAesCipher.Process() encoding, i.e. standard GCM algorithm with trailing 128-bit GMAC.
Your AESDecrypt() does not follow this encoding.

Why are you mixing APIs?
Use ICryptCipher on both sides.

Offline

#3 2025-01-09 16:26:55

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

Re: AES GCM Issue

I had used TAes for Aes Gcm before, you suggested I use ICryptCipher. I tried using the suggested method.
But I wanted to see if the string encrypted with the first method can be decrypted with high level ICryptCipher. My attempt was unsuccessful.

Last edited by anouri (2025-01-09 16:27:11)

Offline

#4 2025-01-09 17:44:43

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

Re: AES GCM Issue

Yes, because you made it wrong.

ICryptCipher uses TAesGcm underneath.
If you are lost within details, just use only ICryptCipher.

Offline

#5 2025-01-11 09:03:26

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

Re: AES GCM Issue

TCryptAesCipher in mormot.crypt.secure is not visible?!
I can't use it

Offline

#6 2025-01-11 11:18:04

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

Re: AES GCM Issue

No, it is not visible, on purpose.

You have to use the Encrypt() factory function to use it.

Offline

#7 2025-01-12 08:02:40

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

Re: AES GCM Issue

After  couple of days I can't figure out right way to encrypt AES GCM.
Even AIs don't give the right answer. I can't find any educational resources on YouTube or anywhere else.

Offline

#8 2025-01-12 08:32:45

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

Re: AES GCM Issue

What is wrong with
- Encrypt('aes-256-gcm', pointer(key)).Process()
- Decrypt('aes-256-gcm', pointer(key)).Process()

What do you want to achieve exactly?

Forget about AI they are far too limited to understand pascal.

Offline

#9 2025-01-12 09:11:00

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

Re: AES GCM Issue

I just wanted to make sure that what I was doing was correct and that I had done a standard and proper encryption. And I needed your confirmation.
Which seemed to be correct. Thank you.

Offline

Board footer

Powered by FluxBB