unit Unit2;
interface
uses
System.SysUtils, System.TypInfo, Generics.Collections, FMX.
Platform,
DECCipherBase, DECFormatBase, DECBaseClass, DECFormat, DECCipherModes,
DECCipherFormats, DECCiphers, DECUtil, DECHashBase, DECHash, DECRandom,
System.Classes, FMX.Dialogs;
type
DECEnc =
class(TObject)
private
Cipher: TCipher_AES;
FFiller: Byte;
FIV: TBytes;
FPassword: UTF8String;
function GenerateHashedPw: TBytes;
procedure SetPassword(
const Value: UTF8String);
public
constructor Create;
property Password: UTF8String
read FPassword
write SetPassword;
destructor Destroy;
override;
function DecryptString(ENC_Base64String: UTF8String): UTF8String;
function EncryptString(
const PlainText: UTF8String): UTF8String;
end;
implementation
constructor DECEnc.Create;
begin
inherited;
FIV:= TEncoding.UTF8.GetBytes('
2A7Q5!8;');
RandomBuffer(FFiller,4);
Cipher:= TCipher_AES.Create;
Cipher.Mode := cmCBCx;
end;
destructor DECEnc.Destroy;
begin
cipher.Free;
inherited;
end;
function DECEnc.DecryptString(ENC_Base64String: UTF8String): UTF8String;
var
output: TBytes;
DecodedString: UTF8String;
input: TBytes;
begin
Cipher.Init(GenerateHashedPw,FIV,FFiller);
Input := System.SysUtils.BytesOf(ENC_Base64String);
output := Cipher.DecodeBytes(TFormat_Base64.decode(Input));
result:= TEncoding.UTF8.GetString(output);
end;
function DECEnc.EncryptString(
const PlainText: UTF8String): UTF8String;
var
output: utf8String;
Data:TBytes;
begin
Cipher.Init(GenerateHashedPw,FIV,FFiller);
output:= UTF8Encode(Cipher.EncodeStringToString(PlainText, TFormat_BASE64));
result:= output;
end;
function DECEnc.GenerateHashedPw: TBytes;
var
Hash: THash_SHA256;
Salt: utf8string;
SaltBytes: TBytes;
PW: TBytes;
HashedPw: TBytes;
begin
Salt:= '
wer 9845747 198498 34534';
Hash:= THash_SHA256.create;
try
SaltBytes:= TEncoding.utf8.GetBytes(Salt);
PW:= TEncoding.utf8.GetBytes(FPassword);
HashedPw:= Hash.KDF1(PW,SaltBytes,Cipher.Context.KeySize);
result:= HashedPw;
finally
Hash.Free;
end;
end;
procedure DECEnc.SetPassword(
const Value: UTF8String);
begin
FPassword := Value;
end;
end.