Lazarus
Programming => General => Topic started by: philgre on March 03, 2013, 07:06:19 pm
-
Hello
I know that this question have been asked few times but I didn't find clear answer or working example.
I try to make some function to encrypt/decrypt string using the blowfish unit from lazarus.
At this time my decrypt function doesn't work.
function encrypt(cle:string;chaine:string):string;
var
en: TBlowFishEncryptStream;
de: TBlowFishDeCryptStream;
s1,s2: TStringStream;
value,temp: String;
begin
s1 := TStringStream.Create('');
en := TBlowFishEncryptStream.Create(cle,s1);
en.WriteAnsiString(chaine);
en.Free;
result:=s1.datastring;
end;
function decrypt(cle:string;chaine:string):string;
var
en: TBlowFishEncryptStream;
de: TBlowFishDeCryptStream;
s1,s2: TStringStream;
value,temp: String;
begin
s2 := TStringStream.Create(chaine);
de := TBlowFishDecryptStream.Create(cle,s2);
temp:=de.ReadAnsiString;
result:=temp;
end;
My code is based on example from http://www.lazarus.freepascal.org/index.php/topic,19663.msg112106.html#msg112106
Thanks for helping
-
Why do you think it does not work?
It works here:
ShowMessage(decrypt('testkey',encrypt('testkey','this is a string')));
-
Like you use it, it works fine, I agree.
But when I want to write string to Edit.Text to check intermediate results, my app crashes
edit4.text:=encrypt('testkey','this is a string');
edit5.text:=decrypt('testkey',edit4.text);
-
UUENcode the encrypted text
-
Well then ... why ? :-[
And how ? I know the base64 unit in freepascal, is there a UUunit ?
-
Well then ... why ? :-[
The result of the encrypt function is binary data not a compatible string. It may contain #0.
If you pass it to the interface (TEdit) the string is "cropped" at #0
-
theo answered why, base64 will work as well any method that converts binary data to string will do just fine.
-
Working now with base64 encoding, thank you :)
Wondering why there's difference with online encrypt tool (dunno if fpc use cbc/ebc etc.), but it's enough for me at the moment.
-
if you are wanting to use blowfish encryption do it this way
var
s1, s2: TStringStream;
ec: TBlowfishEncryptStream;
dc: TBlowfishDecryptStream;
function encrypt(s: string): string; //note you may need to use a try...finally...end; statement
begin
if (s<>'') then begin
s1:=TStringStream.Create(s); //used as your source string
s2:=TStringStream.Create(''); //make sure destination stream is blank
bf:=TBlowfishEncryptStream.Create('password', s2); //writes to destination stream
bf.copyfrom(s1, s1.size);
bf.free;
result:=s2.datastring;
s2.free;
s1.free;
end;
for decryption you just do the opposite like this:
function decryptt(s: string): string; //note you may need to use a try...finally...end; statement
begin
if (s<>'') then begin
s1:=TStringStream.Create(s); //used as your source string
s2:=TStringStream.Create(''); //make sure destination stream is blank
bf:=TBlowfishDecryptStream.Create('password', s1); //reads from source stream
s2.copyfrom(bf, s1.size); //to destination stream copy contents from bf to the size of source stream
bf.free;
result:=s2.datastring;
s2.free;
s1.free;
end;
-
dont forget to include the blowfish.pp module
create two definitions:
ec: TBlowfishEncryptStream;
dc: TBlowfishDecryptStream;
these can be anything you like.
-
Hi. I know this is an old topic. How do you set the mobes of this library? I want to decrypt a string that is encrypted using another language like Python and there you have to set the mode. How do you set the mode using this lib?
-
Hi. I know this is an old topic. How do you set the mobes of this library? I want to decrypt a string that is encrypted using another language like Python and there you have to set the mode. How do you set the mode using this lib?
You need to add which mode (you wanted MODE_CBC).
blowfish.pp in the standard source of FPC doesn't have modes.
TDCP_blowfish from DCPcrypt (DCPblowfish) does have (En/De)cryptECB.
Does anybody know if you can use CBC blowfish with DCPcrypt?
(which is compatible with Blowfish.MODE_CBC in python.)