Fuandi 0 Posted July 7, 2022 Hi, I need to do base64 decoding then decrypt the result with aes256 I managed to do it in linux, this is the command printf "<encrypted string>" | base64 -d | gpg --no-options --batch --cipher-algo AES256 --passphrase "<client secret>" --decrypt How to do this with delphi ? Anyone can help me ? Share this post Link to post
PeterBelow 238 Posted July 7, 2022 (edited) 6 hours ago, Fuandi said: Hi, I need to do base64 decoding then decrypt the result with aes256 I managed to do it in linux, this is the command printf "<encrypted string>" | base64 -d | gpg --no-options --batch --cipher-algo AES256 --passphrase "<client secret>" --decrypt How to do this with delphi ? Anyone can help me ? Well, Delphi has the System.NetEncoding.TBase64Encoding class you can use for the first step to convert the string to an array of bytes (TBytes) or put it into a stream directly, e.g. a TMemorystream. The decryption is a different matter, though. There was a recent thread on using the free Lockbox 3 library for this purpose on these groups. See here... Edited July 7, 2022 by PeterBelow Share this post Link to post
ertank 27 Posted July 8, 2022 (edited) 22 hours ago, Fuandi said: Hi, I need to do base64 decoding then decrypt the result with aes256 I managed to do it in linux, this is the command printf "<encrypted string>" | base64 -d | gpg --no-options --batch --cipher-algo AES256 --passphrase "<client secret>" --decrypt How to do this with delphi ? Anyone can help me ? I am not good at such cryptographic coding. My limited knowledge, you will probably need to know "mode of operation" https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation I do not see any initialization vector (IV) information in your example so that might suggest you need ECB, but that is something you should clarify. I personally like CryptoLib4Pascal. Multi platform support (Lazarus, FMX, VCL, x64). Contains lots of features. Also includes Base64 routines. You will need to get codes from all three links below https://github.com/Xor-el/CryptoLib4Pascal https://github.com/Xor-el/HashLib4Pascal https://github.com/Xor-el/SimpleBaseLib4Pascal Sample AESCBC256 encrypt/decrypt code would look like as following. (AES128 or AES256 is auto determined by the length of Key parameter). You can take it as starting point and modify to your needs. uses ClpIBufferedCipher, ClpCipherUtilities, ClpIParametersWithIV, ClpParametersWithIV, ClpParameterUtilities, ClpEncoders, ClpSecureRandom, ClpISecureRandom; function EncryptAES(const Key, IV: TBytes; const PlainText: string; out CryptBase64Text: string): Boolean; var Cipher: IBufferedCipher; KeyParametersWithIV: IParametersWithIV; Buf: TBytes; CryptBytes: TBytes; begin try Cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING'); KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', Key), IV); Cipher.Init(True, KeyParametersWithIV); // init encryption cipher Buf := TEncoding.ANSI.GetBytes(PlainText); CryptBytes := Cipher.DoFinal(Buf); CryptBase64Text := TBase64.Encode(CryptBytes); except Exit(False); end; Result := True; end; function DecryptAES(const Key, IV: TBytes; const CryptBase64Text: string; out PlainText: string): Boolean; var Cipher: IBufferedCipher; KeyParametersWithIV: IParametersWithIV; Buf: TBytes; PlainBytes: TBytes; begin try Cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING'); KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', Key), IV); Cipher.Init(False, KeyParametersWithIV); // init decryption cipher Buf := TBase64.Decode(CryptBase64Text); PlainBytes := Cipher.DoFinal(Buf); PlainText := TEncoding.ANSI.GetString(PlainBytes); except Exit(False); end; Result := True; end; Edited July 8, 2022 by ertank Share this post Link to post