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;
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.
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;
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;
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
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.
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;
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;
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;