Recent

Author Topic: Lazarus Blowfish  (Read 15995 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18521
  • Here stood a man who saw the Elbe and jumped it.
Re: Lazarus Blowfish
« Reply #15 on: November 09, 2025, 05:06:37 pm »
The code gives a correct result because you use Base64, but a byte encryption would be more efficient and renders also a correct result. (and shorter).
If you want I can add an example.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

BSaidus

  • Hero Member
  • *****
  • Posts: 643
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Lazarus Blowfish
« Reply #16 on: November 09, 2025, 05:44:42 pm »
Yes, Please
Of course an exemple is welcom
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 18521
  • Here stood a man who saw the Elbe and jumped it.
Re: Lazarus Blowfish
« Reply #17 on: November 10, 2025, 09:30:07 am »
Actually the code I came up with looks remarkably like the answer that was given many moons ago by @jshand2010.
I also noticed that the blowfish unit is architecturally not very sound, but works.
Compilable example follows later today.
In honour of the correct answer by @jshand2010 translated into working example  >:D  and in the presence of sheer laziness:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses classes, blowfish;
  3.  
  4.  
  5. function encrypt(s: string): string; //note you may need to use a try...finally...end; statement
  6. var
  7.   bf:TBlowfishEncryptStream = nil;
  8.   s1, s2: TStringStream;
  9. begin
  10.   Result := '';
  11.   if (s<>'') then
  12.   begin
  13.     s1:=TStringStream.Create(s); //used as your source string
  14.     s2:=TStringStream.Create('');  //make sure destination stream is blank
  15.     bf:=TBlowfishEncryptStream.Create('password', s2);  //writes to destination stream
  16.     bf.copyfrom(s1, s1.size);
  17.     bf.free;
  18.     result:=s2.datastring;
  19.     s2.free;
  20.     s1.free;
  21.   end;
  22. end;
  23.  
  24. // for decryption you just do the opposite like this:
  25.  
  26. function decrypt(s: string): string; //note you may need to use a try...finally...end; statement
  27. var
  28.   bf:TBlowfishDecryptStream;
  29.   s1, s2: TStringStream;
  30. begin
  31.   Result := '';
  32.   if (s<>'') then begin
  33.     s1:=TStringStream.Create(s); //used as your source string
  34.     s2:=TStringStream.Create('');  //make sure destination stream is blank
  35.     bf:=TBlowfishDecryptStream.Create('password', s1);  //reads from source stream
  36.     s2.copyfrom(bf, s1.size); //to destination stream copy contents from bf to the size of source stream
  37.     bf.free;
  38.     result:=s2.datastring;
  39.     s2.free;
  40.     s1.free;
  41.   end;
  42. end;
  43. var
  44.   s:String = 'Whatever' ;
  45. begin
  46.   s:= encrypt(s);
  47.   writeln(s);
  48.   s:= decrypt(s);
  49.   writeln(s);
  50. end.
You could have done that too.... No Base64 anywhere.
I expect YOU to lift out the password into a parameter, which I did, but the answer from 12.5 years ago was already correct.
Hence I repeated it, because I am not blind yet. :D : I read the thread....
I left in all original code without change, including the comments. Just made it compile for the lazy.


« Last Edit: November 10, 2025, 09:57:10 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

BSaidus

  • Hero Member
  • *****
  • Posts: 643
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Lazarus Blowfish
« Reply #18 on: November 10, 2025, 04:06:04 pm »
@Thaddy, thank you for the given exemple by @jshand2010. (thank you @jshand2010) But, I'm not blind & I read the whole thread as well.
My question is about if my code is wrong  ::) and why I receive the uncrypted string different from the original.
By the way, When using just variables type string, it works well.
Thanks.

PS: I think an article in wiki will be very helpfull about using cryptography in FPC/Lazarus.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

rvk

  • Hero Member
  • *****
  • Posts: 6908
Re: Lazarus Blowfish
« Reply #19 on: November 10, 2025, 05:17:17 pm »
My question is about if my code is wrong  ::) and why I receive the uncrypted string different from the original.
Wasn't this already discussed?

If you do this: Edit3.Text := EncryptBf('secret', Edit2.Text);
the encrypted string (WHICH ISN'T JUST NORMAL TEXT) is put in an Edit3.Text.
Windows mangles this string to make sure it can display it.... So it changes unreadable characters to ? and block characters (for example ?)P} ) . You CAN'T use that changed string to unencrypt.

If you use a separate S: String.... it doesn't change the content and you don't have the same problem.

So... Don't UNENCRYPT from a Edit3.Text (where the string is changed)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.   S := EncryptBf('secret', Edit2.Text);
  6.   Edit3.Text := S;
  7.   Memo1.Append(DecryptBf('secret', S));
  8.   Edit4.Text := DecryptBf('secret', S);
  9. end;
« Last Edit: November 10, 2025, 05:23:05 pm by rvk »

BSaidus

  • Hero Member
  • *****
  • Posts: 643
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Lazarus Blowfish
« Reply #20 on: November 12, 2025, 07:53:39 pm »
My question is about if my code is wrong  ::) and why I receive the uncrypted string different from the original.
Wasn't this already discussed?

If you do this: Edit3.Text := EncryptBf('secret', Edit2.Text);
the encrypted string (WHICH ISN'T JUST NORMAL TEXT) is put in an Edit3.Text.
Windows mangles this string to make sure it can display it.... So it changes unreadable characters to ? and block characters (for example ?)P} ) . You CAN'T use that changed string to unencrypt.

If you use a separate S: String.... it doesn't change the content and you don't have the same problem.

So... Don't UNENCRYPT from a Edit3.Text (where the string is changed)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.   S := EncryptBf('secret', Edit2.Text);
  6.   Edit3.Text := S;
  7.   Memo1.Append(DecryptBf('secret', S));
  8.   Edit4.Text := DecryptBf('secret', S);
  9. end;

Thanks @rvk, I've realise that I need to save the encrypted on a string variable, not on an edit component.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018