uses
System.SysUtils,
System.Classes,
System.Crypto,
System.Convert;
function Encrypt(clearText: string): string;
var
EncryptionKey: string;
clearBytes: TBytes;
encryptor: TAesManaged;
pdb: TRfc2898DeriveBytes;
ms: TMemoryStream;
cs: TCryptoStream;
begin
EncryptionKey := 'MAKV2SPBNI99212';
clearBytes := TEncoding.Unicode.GetBytes(clearText);
encryptor := TAesManaged.Create;
try
pdb := TRfc2898DeriveBytes.Create(EncryptionKey, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]);
encryptor.Key := pdb.GetBytes(32);
encryptor.IV := pdb.GetBytes(16);
ms := TMemoryStream.Create;
try
cs := TCryptoStream.Create(ms, encryptor.CreateEncryptor, CryptoStreamMode.Write);
try
cs.Write(clearBytes, 0, Length(clearBytes));
finally
cs.Free;
end;
clearText := TBase64Encoding.UTF8.EncodeToString(ms.ToArray);
finally
ms.Free;
end;
finally
encryptor.Free;
end;
Result := clearText;
end;
function Decrypt(cipherText: string): string;
var
EncryptionKey: string;
cipherBytes: TBytes;
encryptor: TAesManaged;
pdb: TRfc2898DeriveBytes;
ms: TMemoryStream;
cs: TCryptoStream;
begin
EncryptionKey := 'MAKV2SPBNI99212';
cipherBytes := TBase64Encoding.UTF8.DecodeToBytes(cipherText);
encryptor := TAesManaged.Create;
try
pdb := TRfc2898DeriveBytes.Create(EncryptionKey, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]);
encryptor.Key := pdb.GetBytes(32);
encryptor.IV := pdb.GetBytes(16);
ms := TMemoryStream.Create;
try
cs := TCryptoStream.Create(ms, encryptor.CreateDecryptor, CryptoStreamMode.Write);
try
cs.Write(cipherBytes, 0, Length(cipherBytes));
finally
cs.Free;
end;
cipherText := TEncoding.Unicode.GetString(ms.ToArray);
finally
ms.Free;
end;
finally
encryptor.Free;
end;
Result := cipherText;
end.