Recent

Author Topic: Lazarus Blowfish  (Read 14010 times)

philgre

  • New Member
  • *
  • Posts: 12
Lazarus Blowfish
« 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

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Lazarus Blowfish
« Reply #1 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')));   

philgre

  • New Member
  • *
  • Posts: 12
Re: Lazarus Blowfish
« Reply #2 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);         

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Lazarus Blowfish
« Reply #3 on: March 03, 2013, 07:32:21 pm »
UUENcode the encrypted text
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

philgre

  • New Member
  • *
  • Posts: 12
Re: Lazarus Blowfish
« Reply #4 on: March 03, 2013, 07:36:38 pm »
Well then ... why ?  :-[
And how ? I know the base64 unit in freepascal, is there a UUunit ?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Lazarus Blowfish
« Reply #5 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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Lazarus Blowfish
« Reply #6 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

philgre

  • New Member
  • *
  • Posts: 12
Re: Lazarus Blowfish
« Reply #7 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.

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: Lazarus Blowfish
« Reply #8 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; 
« Last Edit: March 04, 2013, 09:00:32 pm by jshand2010 »
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: Lazarus Blowfish
« Reply #9 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. 
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

jdp

  • Full Member
  • ***
  • Posts: 144
Re: Lazarus Blowfish
« Reply #10 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?

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Lazarus Blowfish
« Reply #11 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