use
Classes, SysUtils, Dialogs,
DCPrc6, DCPsha512, base64,
zstream;
// ************************
// ***** Crypt String *****
// ************************
function Encrypt(SInput, SKey: String): String;
var
DecodedStream: TStringStream;
EncodedStream: TStringStream;
Str: String;
Cipher: TDCP_rc6;
begin
try
DecodedStream := TStringStream.Create(SInput);
EncodedStream := TStringStream.Create('');
Cipher:= TDCP_rc6.Create(nil);
Cipher.InitStr(SKey,TDCP_sha512); // initialize the cipher with a hash of the passphrase
Cipher.EncryptStream(DecodedStream,EncodedStream,DecodedStream.Size); // encrypt the contents of the file
Str := EncodeStringBase64(EncodedStream.DataString); // Convert to Base64
Cipher.Burn;
Cipher.Free;
DecodedStream.Free;
EncodedStream.Free;
exit(Str);
except
exit('');
end;
exit('');
end;
// **************************
// ***** Decrypt String *****
// **************************
function Decrypt(Sinput, SKey: String): String;
var
DecodedStream: TStringStream;
EncodedStream: TStringStream;
Str: String;
Cipher: TDCP_rc6;
begin
try
EncodedStream := TStringStream.Create(DecodeStringBase64(SInput)); // Base 64 Decode the Data
DecodedStream := TStringStream.Create('');
Cipher:= TDCP_rc6.Create(nil);
Cipher.InitStr(SKey,TDCP_sha512); // initialize the cipher with a hash of the passphrase
Cipher.DecryptStream(EncodedStream,DecodedStream,EncodedStream.Size); // decrypt the contents of the file
Str := DecodedStream.DataString;
Cipher.Burn;
Cipher.Free;
EncodedStream.Free;
DecodedStream.Free;
exit(Str)
except
exit('');
end;
exit('');
end;
// ***************************
// ***** Compress String *****
// ***************************
function Compress(SInput: String): String;
var
Compressor: TCompressionStream;
strInput, strOutput: TStringStream;
Str: String;
begin
strInput := TStringStream.Create(SInput);
strOutput := TStringStream.Create('');
try
Compressor := TCompressionStream.Create(clDefault, strOutput);
Compressor.CopyFrom(strInput, strInput.Size);
Compressor.Free;
Str := strOutput.DataString;
strOutput.Free;
strInput.Free;
exit(Str);
except
exit('');
end;
exit('');
end;
// *****************************
// ***** Decompress String *****
// *****************************
function Decompress(Sinput: String): String;
var
Decompressor: TDecompressionStream;
strInput, strOutput: TStringStream;
Str: String;
begin
strInput := TStringStream.Create(Sinput);
strOutput := TStringStream.Create('');
try
Decompressor := TDecompressionStream.Create(strInput);
strOutput.Position := 0;
strOutput.Size := 0;
strOutput.CopyFrom(Decompressor, 0);
Decompressor.Free;
Str := strOutput.DataString;
strOutput.Free;
strInput.Free;
exit(Str);
except
exit('');
end;
exit('');
end;