TAESEncryption and Delphi 13.1

i miss the property keylength it is disapeart!

An

in Scorcecode it is avalibie:

  published
    property keyLength: TAESKeyLength read FKeyLength write SetKeyLength
      default kl128;
    property key: string read FKey write SetKey;
    property AType: TAESType read FType write FType default atCBC;
    property inputFormat: TConvertType read FInputFormat write FInputFormat
      default raw;
    property outputFormat: TConvertType read FOutputFormat write FOutputFormat
      default raw;
    property IVMode: TIVMode read FIVMode write FIVMode default rand;
    property IV: string read FIV write SetIV;
    property paddingMode: TPaddingMode read FPaddingMode write FPaddingMode
      default PKCS7;
    property Unicode:  TUnicode read FUni write FUni default yesUni;
    property Endian:   TEndian read FEndian write SetEndian default BigEndian;
    property Progress: integer read FProgress write SetProgress default 0;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

crypt and decrypt don’t works it apears Error:

Decrypt: Invalid Operation : -208

function TDataCl.CodeAES(Text,Secret:string):string;
begin
 try
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  result:=AESEn.Encrypt(Text);
 except
    on E : Exception do begin
      LOG('Encrypt: '+e.Message);
    end;
 end;
end;

function TDataCl.DeCodeAES(Text,Secret:string):string;
begin
 try
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  result:=AESEn.Decrypt(Text);
 except
    on E : Exception do begin
      LOG('Decrypt: '+e.Message);
    end;
 end;
end;

in Delphi 12.3

Hello Arno,

KeyLength still exists but is no longer in the visual component, you can set it in your code like you did: AESEn.keyLength:= TAESKeyLength.kl256. This is a side effect of defining TAESKeyLength as a set of constants in CryptoConst.pas (i.e., globally vs locally).

Error 208 is the consequence of a conversion issue. You need to set inputFormat correctly and possibly convert the cryptogram before you decrypt it, depending on the settings.

Using AESEn.inputFormat := noFormat could be enough.

See similar posts on the support thread.

Regards,

bernard

Hi the “old Version” sends cryped Infos and i wont to decode this

it works with Delphi 12.3 in 13.1 it appears:

'Bad Type (5)'

function TDataCl.DeCodeAES(Text,Secret:string):string;
begin
 try
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  result:=AESEn.Decrypt(Text);
 except
    on E : Exception do begin
      LOG('Decrypt: '+e.Message);
    end;
 end;
end;

in the Sender

Try this, in DeCodeAES, outputFormat := raw.

If this fails, then convert the cryptogram:

  • add Conv: TConvert in the var section of DeCodeAES, then
  • Conv := TConvert.Create(hexa);
  • AESEn.inputFormat := raw;
    
  • Text := Conv.FormatToChar(Text);
    
  •   AESEn.keyLength:= TAESKeyLength.kl256;
      AESEn.key:=Secret;
      result:=AESEn.Decrypt(Text);
    
  • Conv.Free

Thank you that works:

uses ...  MiscObj ....

function TDataCl.DeCodeAES(Text,Secret:string):string;
 var  Conv: TConvert;
begin
 try
  Conv := TConvert.Create(hexa);
  Text:=Conv.FormatToChar(Text);
  AESEn.inputFormat:=TConvertType.raw;
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  result:=AESEn.Decrypt(Text);
 except
    on E : Exception do begin
      LOG('Decrypt: '+e.Message);
    end;
 end;
 Conv.Free;
end;


How the encrypt function ?

function TDataCl.CodeAES(Text,Secret:string):string;
 var  Conv: TConvert;
begin
 try
  Conv := TConvert.Create(hexa);
  Text:=Conv.FormatToChar(Text);
  AESEn.inputFormat:=TConvertType.raw;
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  result:=AESEn.Encrypt(Text);
 except
    on E : Exception do begin
      LOG('Encrypt: '+e.Message);
    end;
 end;
 Conv.Free;
end;

You don’t need to convert when you encrypt, unless your data has a specific format (e.g., base64 from another app). If you encrypt a string such as ‘MySecretDataToBeEncrypted’, then try something like this:

function TDataCl.CodeAES(Text,Secret:string):string;
begin
 try
  AESEn.inputFormat:=TConvertType.raw;
  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEn.key:=Secret;
  AESEn.outputFormat:=TConvertType.hexa; // that's the default value from the graphic class, as a reminder
  result:=AESEn.Encrypt(Text); // you will get an hex output
 except
    on E : Exception do begin
      LOG('Encrypt: '+e.Message);
    end;
 end;
end;

Sorry i works 8 Years with your TAESEncryption

Your new Version is broken its incompatible with Version 4.3.3 and older version.

Sorry that is very bad …. i can’t use this

I am not following you.

What is broken?

You said decryption worked. Is there another issue?

sample:

Version 4 and older

procedure TAESEncryption.SetKey(const Value: string);
begin
  if Value <> '' then
  begin
    if (Length(Value) <> (FKeyLengthNum div 8)) then
      raise ECryptoPack.Create('Keylength incorrect, must be ' +
        IntToStr(FKeyLengthNum div 8));
    FKey := Value;
  end;
end;

version 5

procedure TAESEncryption.SetKey(const Value: string);
var
  s: string;
  Conv: TConvert;

begin
  if (inputFormat <> noFormat) and (inputFormat <> raw) then begin
    Conv := TConvert.Create(inputFormat);
    s    := Conv.FormatToChar(Value);
    Conv.Free;
  end
  else
    s := Value;

  if s <> '' then begin
    if (Length(s) <> (FKeyLengthNum div 8)) then
      raise ECryptoPack.Create('Keylength incorrect, must be ' +
        IntToStr(FKeyLengthNum div 8));
    FKey := s;
  end;
end;

for example: if inputformat Hex then the key need 64 instad of 32 chars, it can’t comunicate with older versions.

Keylength not longer das property in the designer, grrrrr

it dont work corectly

That is because your key is not (really) in hex. It may look like hex but for a 256-bit key it shall be 64 chars. Say your key is ‘123456789ABCDEF123456789ABCDEF’, in 4.3, this is 32 ASCII bytes, then 256 bits. If you input this as hex, then it is only 16 bytes (a byte is coded over 2 chars), or a 128-bit key.

Try you key with inputFormat := raw.

i have made 2 testprg;

one with delphi 12.3 and AESEncryption Version 4.3.3

one with Delphi 13.1 and AESEncryption Version 5.1.0

Decrpyt will not work in the new version, and i think it is new property inputFormat

Crypt is working decrype is nocht working in the newer version ?

procedure TForm17.Button2Click(Sender: TObject); // this works with  V4.3.3
begin
 try
  //AESEncryption1.keyLength:= TAESKeyLength.kl256;
  AESEncryption1.key:=ED_KEY.Text;
  Edit2.Text:=AESEncryption1.Decrypt(ED_CRYTO.Text);
 except
    on E : Exception do begin
      ShowMessage('Decrypt: '+e.Message);
    end;
 end;
end;

procedure TForm17.Button3Click(Sender: TObject);
 var  Conv: TConvert;
begin
 try
  Conv := TConvert.Create(hexa);
  var  Text:=Conv.FormatToChar(ED_CRYTO.Text);
  AESEncryption1.inputFormat:=TConvertType.raw;
//  AESEn.keyLength:= TAESKeyLength.kl256;
  AESEncryption1.key:=ED_KEY.Text;
  Edit2.Text:=AESEncryption1.Decrypt(Text);
 except
    on E : Exception do begin
      ShowMessage('Decrypt: '+e.Message);
    end;
 end;
 Conv.Free;
end;

aesV4toV5.zip (2.4 MB)

i have attatched the 2 version code and exe

in the version from today (5.1.1.9) is this the solution:

procedure TForm17.Button3Click(Sender: TObject);
begin
 try
  Text:=(ED_CRYTO.Text);
  AESEncryption1.inputFormat:=TConvertType.noFormat; //  raw;
  AESEncryption1.keyLength:= TAESKeyLength.kl256;
  AESEncryption1.key:=ED_KEY.Text;
  Edit2.Text:=AESEncryption1.Decrypt(Text);
 except
    on E : Exception do begin
      ShowMessage('Decrypt: '+e.Message);
    end;
 end;
end;

Hi,

A couple of things.

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CryptBase, CryptoConst, AESObj, MiscObj;

The code should look like this:

procedure TForm17.Button1Click(Sender: TObject);
begin
 try
    AESEncryption1.keyLength   := TAESKeyLength.kl256;AESEncryption1.inputFormat := raw;
    AESEncryption1.key         := ED_KEY.Text;
    AESEncryption1.outputFormat:= hexa; // because it is modified in decrypt
    ED_CRYTO.Text              := AESEncryption1.Encrypt(Edit1.Text);
  except
    on E : Exception do begin
      ShowMessage('Crypt: '+e.Message);
    end;
  end;
end;

procedure TForm17.Button2Click(Sender: TObject);
var
  Conv: TConvert;

begin
  Conv := TConvert.Create(hexa);
  try
    AESEncryption1.keyLength   := TAESKeyLength.kl256;
    AESEncryption1.inputFormat := raw;
    AESEncryption1.outputFormat:= raw; // we want raw text (string) back
    Edit2.Text                 := AESEncryption1.Decrypt(Conv.FormatToChar(ED_CRYTO.Text)); // hex data is converted
  except
    on E : Exception do begin
    ShowMessage('Decrypt: '+e.Message);
    end;
  end;
  Conv.Free;
end;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.