function Encrypt(
const AText:
string;
const APassword:
string):
string;
{ -Encrypt file InName to OutName using password }
// http://www.wolfgang-ehrhardt.de/crypt_en.html
const
bufSize = $C000;
var
N : word;
len : int64;
hdr : TFCA256Hdr;
cxe : TAES_EAXContext;
auth : TFCA256_AuthBlock;
TextOut, TextIn: TStringStream;
buf :
array [0 .. bufSize - 1]
of Byte;
begin
randomize;
TextIn := TStringStream.Create(AText);
TextOut := TStringStream.Create;
try
len := TextIn.Size;
GetSalt(hdr.salt);
if FCA_EAX256_initS(cxe, APassword, hdr) <> 0
then
raise Exception.Create('
Fehler');
TextOut.WriteBuffer(hdr, SizeOf(hdr));
while len > 0
do
begin
if len > SizeOf(buf)
then
N := SizeOf(buf)
else
N := len;
TextIn.ReadBuffer(buf, N);
dec(len, N);
if FCA_EAX256_encrypt(cxe, buf, N) <> 0
then
raise Exception.Create('
Fehler');
TextOut.WriteBuffer(buf, N);
end;
FCA_EAX256_final(cxe, auth);
TextOut.WriteBuffer(auth, SizeOf(auth));
Result := Base64EncStr(TextOut.DataString);
finally
TextOut.Free;
TextIn.Free;
end;
end;
function Decrypt(
const AText:
string;
const APassword:
string):
string;
{ -Decrypt file InName to OutName using password sPW }
// http://www.wolfgang-ehrhardt.de/crypt_en.html
const
bufSize = $C000;
var
N : word;
I, len : longint;
hdrk : TFCA256Hdr;
hdrf : TFCA256Hdr;
cxe : TAES_EAXContext;
cxh : TFCA_HMAC256_Context;
authf : TFCA256_AuthBlock;
authc : TFCA256_AuthBlock;
UseEAX : boolean;
TextOut, TextIn: TStringStream;
buf :
array [0 .. bufSize - 1]
of Byte;
begin
if AText = '
'
then
Exit('
');
TextIn := TStringStream.Create(Base64DecStr(AText));
TextOut := TStringStream.Create;
try
len := TextIn.Size - SizeOf(hdrf) - SizeOf(authf);
TextIn.ReadBuffer(hdrf, SizeOf(hdrf));
if (hdrf.FCASig <> C_FCA_Sig)
or (hdrf.Flags
and $F0 <> $A0)
then
raise Exception.Create('
Fehler');
if hdrf.Flags
and $02 <> 0
then
begin
writeln(#7'
*** Warning: Found zlib compression flag, use t_zlibex to inflate <outfile>');
end;
if not(hdrf.Flags
and $04 <> 0)
then
begin
raise Exception.Create('
Fehler');
end;
hdrk := hdrf;
UseEAX := odd(hdrf.Flags);
if UseEAX
then
begin
if FCA_EAX256_initS(cxe, APassword, hdrk) <> 0
then
raise Exception.Create('
Fehler');
end
else
begin
if FCA_HMAC256_initS(cxh, APassword, hdrk) <> 0
then
raise Exception.Create('
Fehler');
end;
if hdrf.PW_ver <> hdrk.PW_ver
then
raise Exception.Create('
Fehler');
while len > 0
do
begin
if len > SizeOf(buf)
then
N := SizeOf(buf)
else
N := len;
TextIn.ReadBuffer(buf, N);
dec(len, N);
if UseEAX
then
begin
if FCA_EAX256_decrypt(cxe, buf, N) <> 0
then
raise Exception.Create('
Fehler');
end
else
begin
if FCA_HMAC256_decrypt(cxh, buf, N) <> 0
then
raise Exception.Create('
Fehler');
end;
TextOut.WriteBuffer(buf, N);
end;
if UseEAX
then
begin
FCA_EAX256_final(cxe, authc);
end
else
begin
FCA_HMAC256_final(cxh, authc);
end;
TextIn.ReadBuffer(authf, SizeOf(authf));
for I := 0
to 15
do
begin
if authf[I] <> authc[I]
then
begin
raise Exception.Create('
Fehler');
end;
end;
Result := TextOut.DataString;
finally
TextOut.Free;
TextIn.Free;
end;
end;