Lazarus

Programming => General => Topic started by: philgre on March 03, 2013, 07:06:19 pm

Title: Lazarus Blowfish
Post 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.

Code: [Select]
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
Title: Re: Lazarus Blowfish
Post by: theo on March 03, 2013, 07:18:17 pm
Why do you think it does not work?

It works here:
Code: [Select]
  ShowMessage(decrypt('testkey',encrypt('testkey','this is a string')));   
Title: Re: Lazarus Blowfish
Post by: philgre on March 03, 2013, 07:29:59 pm
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

Code: [Select]
  edit4.text:=encrypt('testkey','this is a string');
  edit5.text:=decrypt('testkey',edit4.text);         
Title: Re: Lazarus Blowfish
Post by: taazz on March 03, 2013, 07:32:21 pm
UUENcode the encrypted text
Title: Re: Lazarus Blowfish
Post by: philgre on March 03, 2013, 07:36:38 pm
Well then ... why ?  :-[
And how ? I know the base64 unit in freepascal, is there a UUunit ?
Title: Re: Lazarus Blowfish
Post by: theo on March 03, 2013, 07:48:30 pm
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
Title: Re: Lazarus Blowfish
Post by: taazz on March 03, 2013, 08:07:12 pm
theo answered why, base64 will work as well any method that converts binary data to string will do just fine.
Title: Re: Lazarus Blowfish
Post by: philgre on March 03, 2013, 09:37:18 pm
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.
Title: Re: Lazarus Blowfish
Post by: jshand2010 on March 04, 2013, 04:24:02 am
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; 
Title: Re: Lazarus Blowfish
Post by: jshand2010 on March 04, 2013, 04:25:40 am
dont forget to include the blowfish.pp module

create two definitions:

ec: TBlowfishEncryptStream;
dc: TBlowfishDecryptStream;

these can be anything you like. 
Title: Re: Lazarus Blowfish
Post by: jdp on September 29, 2020, 02:03:17 pm
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?
Title: Re: Lazarus Blowfish
Post by: rvk on September 29, 2020, 03:32:42 pm
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.)
TinyPortal © 2005-2018