Xor-el / CryptoLib4Pascal

Crypto for Modern Object Pascal
MIT License
211 stars 65 forks source link

trying to use AES/ECB/PKCS7PADDING #39

Closed RaySa74 closed 5 months ago

RaySa74 commented 5 months ago

trying to use AES/ECB/PKCS7PADDING getting Exception Please help

EArgumentCryptoLibException: Invalid Parameter Passed to AES Init - "TParametersWithIV"

///////////////////////////////////////////////////////////////////////////////////////////////////////// class function TUsageExamples.AES256CBCPascalCoinEncrypt(PlainText, PasswordBytes: TBytes): TBytes; var SaltBytes, KeyBytes, IVBytes, Buf: TBytes; KeyParametersWithIV: IParametersWithIV; cipher: IBufferedCipher; LBlockSize, LBufStart, Count: Int32; begin SaltBytes := EVP_GetSalt; EVP_GetKeyIV(PasswordBytes, SaltBytes, KeyBytes, IVBytes); //AES/ECB/PKCS7PADDING cipher := TCipherUtilities.GetCipher('AES/ECB/PKCS7PADDING'); //cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING'); KeyParametersWithIV := TParametersWithIV.Create (TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);

cipher.Init(True, KeyParametersWithIV); // init encryption cipher LBlockSize := cipher.GetBlockSize;

Xor-el commented 5 months ago

Hi @RaySa74 ECB should never use an IV. Please see here for a usage example for ECB.

RaySa74 commented 5 months ago

Hi @RaySa74 ECB should never use an IV. Please see here for a usage example for ECB.

Thank you for your reply is there a possibility you can provide a VCL form example where we use 256-bits AES key (AES should use ECB cipher mode and PKCS7 padding) . It is very difficult to follow in the CMD example that was provided with the code.. thanks

Xor-el commented 5 months ago

Hello @RaySa74 Apologies for the late response. below is a simple console program that encrypts and decrypts a string using AES/ECB/PKCS7PADDING

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  ClpKeyParameter,
  ClpIKeyParameter,
  ClpParametersWithIV,
  ClpIParametersWithIV,
  ClpSecureRandom,
  ClpISecureRandom,
  ClpCipherUtilities,
  ClpIBufferedCipher;

var
  KeyBytes, EncryptionResult, DecryptionResult: TBytes;
  KeyParameter: IKeyParameter;
  cipher: IBufferedCipher;
  SecureRandom: ISecureRandom;
  DataToEncrypt: string;

begin
  try
    DataToEncrypt := '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    SecureRandom := TSecureRandom.Create();

    KeyBytes := TSecureRandom.GetNextBytes(SecureRandom, 32);

    cipher := TCipherUtilities.GetCipher('AES/ECB/PKCS7PADDING');

    KeyParameter := TKeyParameter.Create(KeyBytes) as IKeyParameter;

    cipher.Init(True, KeyParameter); // init encryption cipher

    EncryptionResult := cipher.DoFinal(TEncoding.UTF8.GetBytes(dataToEncrypt));

    cipher.Init(false, KeyParameter); // init decryption cipher

    DecryptionResult := cipher.DoFinal(EncryptionResult);

    Writeln(TEncoding.UTF8.GetString(DecryptionResult));

    Readln;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.