uses
DECUtil, DECCipher, DECHash, DECFmt,
X.System.Data;
{$R *.dfm}
var
ACipherClass: TDECCipherClass = TCipher_Rijndael;
ACipherMode: TCipherMode = cmCBCx;
AHashClass: TDECHashClass = THash_MD5;
ATextFormat: TDECFormatClass = TFormat_Mime64;
AKDFIndex: LongWord = 1;
function DoDecrypt(
const AText: WideString;
const APassword: WideString): WideString;
overload;
var
ASalt: Binary;
AData: Binary;
ACheck: Binary;
APass: Binary;
ALen: Integer;
begin
with ValidCipher(ACipherClass).Create, Context
do
try
ASalt := ValidFormat(ATextFormat).Decode(AText);
ALen := Length(ASalt) - 16 - BufferSize;
AData := System.Copy(ASalt, 17, ALen);
ACheck := System.Copy(ASalt, ALen + 17, BufferSize);
SetLength(ASalt, 16);
APass := ValidHash(AHashClass).KDFx(APassword[1], Length(APassword) * SizeOf(APassword[1]), ASalt[1], Length(ASalt), KeySize, TFormat_Copy, AKDFIndex);
Mode := ACipherMode;
Init(APass);
SetLength(Result, ALen
div SizeOf(AText[1]));
Decode(AData[1], Result[1], ALen);
if ACheck <> CalcMAC
then
raise Exception.Create('
Invalid data');
finally
Free;
ProtectBinary(ASalt);
ProtectBinary(AData);
ProtectBinary(ACheck);
ProtectBinary(APass);
end;
end;
procedure TForm1.DecryptClick(Sender: TObject);
var
Key: WideString;
Text: WideString;
begin
Key := '
test';
Text := '
U2FsdGVkX19h+WKvkdYWILeBgb10Yg6pVrXS1qAQmmo=';
Output.Text := DoDecrypt(Text, Key);
end;