program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
madcrypt;
const B64: ansistring = '
./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function PCindex(w: ansistring): Cardinal;
begin
Result:= Pos(w, B64);
if Result > 0
then dec(Result);
end;
function PCsubstr(w: ansistring; i: Integer): ansichar;
var s: ansistring;
begin
Result := #0;
s:= Copy(w, i+1, 1);
if (length(s) > 0)
then
Result:= s[1];
end;
function bytetoB64(
ec: ansistring): ansistring;
var dc: ansistring;
left, right: Cardinal;
i, k : Integer;
begin
dc := '
';
k := -1;
while(k < (length(
ec)-1))
do
begin
inc(k);
left := (ord(PCsubstr(
ec,k))
shl 24);
inc(k);
inc(left,(ord(PCsubstr(
ec,k))
shl 16));
inc(k);
inc(left, (ord(PCsubstr(
ec,k))
shl 8));
inc(k);
inc(left, ord(PCsubstr(
ec,k)));
inc(k);
right := (ord(PCsubstr(
ec,k))
shl 24);
inc(k);
inc(right,(ord(PCsubstr(
ec,k))
shl 16));
inc(k);
inc(right,(ord(PCsubstr(
ec,k))
shl 8));
inc(k);
inc(right, ord(PCsubstr(
ec,k)));
for i := 0
to 5
do
begin
dc :=
dc + PCsubstr(B64, right
and $3F);
right := right
shr 6;
end;
for i := 0
to 5
do
begin
dc :=
dc + PCsubstr(B64, left
and $3F);
left := left
shr 6;
end;
end;
Result:=
dc;
end;
function B64tobyte(
ec: ansistring): ansistring;
var dc: ansistring;
k: Integer;
i, right, left: Cardinal;
begin
dc:= '
';
k := -1;
while(k < (length(
ec)-1))
do
begin
right := 0;
left := 0;
for i := 0
to 5
do
begin
inc(k);
right := right
or (PCindex(PCsubstr(
ec, k))
shl (i * 6));
end;
for i := 0
to 5
do
begin
inc(k);
left := left
or (PCindex(PCsubstr(
ec, k))
shl (i * 6));
end;
for i := 0
to 3
do
begin
dc :=
dc + chr((left
and ($FF
shl ((3 - i) * 8)))
shr ((3 - i) * 8));
end;
for i := 0
to 3
do
begin
dc :=
dc + chr((right
and ($FF
shl ((3 - i) * 8)))
shr ((3 - i) * 8));
end;
end;
Result:=
dc;
end;
function set_key(key: ansistring): ansistring;
var i, keyLen: Integer;
newkey: ansistring;
begin
Result := key;
if(length(key) < 8)
then
begin
keyLen := length(key);
i := 8
div keyLen;
if (8
mod keyLen > 0)
then inc(i);
newkey := '
';
while (i > 0)
do
begin
newkey := newkey + key;
dec(i);
end;
Result := newkey;
end;
end;
function EncryptIrcBlowfish(dText, key: ansistring): ansistring;
stdcall;
var temp, eText: ansistring;
i: Integer;
mykey: ansistring;
begin
eText := '
';
if(dText<>'
')
then
begin
temp := '
';
if (length(dText)
mod 8 > 0)
then
for i:= 1
to 8 - (length(dText)
mod 8)
do
dText:= dText+ #0;
temp := '
';
mykey:= set_key(key);
for i:= 1
to length(dText)
div 8
do
begin
temp:= Copy(dText, 1+(i-1)*8,8);
SetLength(temp, 8);
madcrypt.Encrypt(PansiChar(temp), 8, mykey);
eText := eText + bytetoB64(temp);
end;
end;
Result:= eText;
end;
function DecryptIrcBlowfish(eText, key: ansistring): ansistring;
stdcall;
var temp, dText: ansistring;
i : Integer;
mykey: ansistring;
begin
mykey:= set_key(key);
for i:= 1
to length(eText)
div 12
do
begin
temp := B64tobyte(Copy(eText,1+(i-1)*12,12));
SetLength(temp, 8);
madcrypt.Decrypt(PansiChar(temp), 8, mykey);
dText := dtext + temp;
end;
Result:= dText;
end;
var
str, key, strEncrypted, strDecrypted: ansistring;
begin
try
str := '
t2e3s4t5t6e7st';
key := '
123';
writeln('
----');
strEncrypted := EncryptIrcBlowfish( str, key );
writeln('
Encrypted: '+strEncrypted);
strDecrypted := DecryptIrcBlowfish( strEncrypted, key );
writeln('
DEcrypted: ' +strDecrypted);
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
readln;
end.