Recent

Author Topic: FCL : Blowfish.pp ... Help needed %)  (Read 16472 times)

jshand2010

  • Full Member
  • ***
  • Posts: 236
FCL : Blowfish.pp ... Help needed %)
« on: January 16, 2011, 10:40:28 am »
Hi there.  just wanting to know more about the blowfish encryption that is part of the fcl.  i would appreciate all the help i can get in how to use it.

Please give me some examples on how you can use this fcl feature

i am attempting to use the TStringStream function

your help would be appreciated
« Last Edit: January 16, 2011, 10:47:53 am by jshand2010 »
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #1 on: January 16, 2011, 10:56:45 am »
Here's an example that shows encryption and decryption. It follows the normal stream model, be sure to call Flush or Destroy(free) before you need the data, otherwise it might not have written all. Really simple :)
Code: [Select]
procedure Encrypt(InStream, OutStream: TStream; const Key: string);
var EC: TStream;
begin
   EC := TBlowFishEncryptStream.Create(Key, OutStream);
   try
      EC.CopyFrom(InStream, InStream.Size);
   finally
      EC.Free; // Destroy automatically calls Flush
   end;
end;

procedure Decrypt(InStream, OutStream: TStream; const Key: string);
var EC: TStream;
begin
   EC := TBlowFishDeCryptStream.Create(Key, InStream);
   try
      OutStream.CopyFrom(EC, InStream.Size);
   finally
      EC.Free; // Destroy automatically calls Flush
   end;
end;

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #2 on: January 16, 2011, 11:02:22 am »
thanks for giving me the heads up.

however, i am a little confused to how i can incorporate my user strings.  instream and outstring and key have me quite confused.

thanks heaps
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #3 on: January 17, 2011, 06:43:10 am »
thanks for giving me the heads up.

however, i am a little confused to how i can incorporate my user strings.  instream and outstring and key have me quite confused.

thanks heaps

I've happened to have done something similar for the base64 classes yesterday:

Code: [Select]
function DecodeStringBase64(const s:string):String;

var
  Instream,
  Outstream : TStringStream;
  Decoder   : TBase64DecodingStream;
begin
  Instream:=TStringStream.Create(s);
  try
    Outstream:=TStringStream.Create('');
    try
      Decoder:=TBase64DecodingStream.Create(Instream,bdmMIME);
      try
         Outstream.CopyFrom(Decoder,Decoder.Size);
         Outstream.Position:=0;
         Result:=Outstream.ReadString(Outstream.Size);
      finally
        Decoder.Free;
        end;
    finally
     Outstream.Free;
     end;
  finally
    Instream.Free;
    end;
end;

function EncodeStringBase64(const s:string):String;

var
  Outstream : TStringStream;
  Encoder   : TBase64EncodingStream;
begin
  Outstream:=TStringStream.Create('');
  try
    Encoder:=TBase64EncodingStream.create(outstream);
    try
      Encoder.Write(s[1],Length(s));
    finally
      Encoder.Free;
      end;
    Outstream.Position:=0;
    Result:=Outstream.ReadString(Outstream.Size);
  finally
    Outstream.free;
    end;
end;

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #4 on: January 19, 2011, 11:43:57 pm »
hi there.  now am very confused.  i have an array with the value tl[1].username.... how do i encrypt this with blowfish.pp?  i have tried the above code, but it makes no sense.

perhaps a rewrite on blowfish.pp would help maybe.  don't honestly know how to integrate this properly with my data.

help please :)
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

captian jaster

  • Guest
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #5 on: January 20, 2011, 03:28:08 am »
hi there.  now am very confused.  i have an array with the value tl[1].username.... how do i encrypt this with blowfish.pp?  i have tried the above code, but it makes no sense.

perhaps a rewrite on blowfish.pp would help maybe.  don't honestly know how to integrate this properly with my data.

help please :)
Well lets try to make this easy to understand..
The "Key" is the string that will be used to encrypt your data.. This makes it "secret". The blowfish encrypt stream will require another stream to store the encrypted data.. If you want to save it to a file use a "TFileStream" or for any other reason you could use a TStringtream or TMemoryStream. Its more simple to understand when you have a better understanding of streams ;). I couldn't find anything in the wiki about it but you can read about a lot of streams in the Classes unit. Once you get a hang of this. You could get a hang of any other stream..

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #6 on: January 20, 2011, 03:48:54 am »
hi there.  now am very confused.  i have an array with the value tl[1].username.... how do i encrypt this with blowfish.pp?  i have tried the above code, but it makes no sense.
Basically what happens is that you write some data to a TBlowFishEncryptStream, which in turn writes it to another stream

For example, a simpler version of the code I wrote earlier could be something like this:
Code: [Select]
function Encrypt(const a,key: string): string;
var Inp, Outp, EC: TStream;
begin
   Inp := TStringStream.Create(a);
   try
      Outp := TStringStream.Create('');
      try
         EC := TBlowFishEncryptStream.Create(Key, Outp);
         try
            EC.CopyFrom(Inp, Inp.Size);
         finally
            EC.Free; // Destroy automatically calls Flush
         end;
         result := TStringStream(Outp).DataString;
      finally
         Outp.Free;
      end;
   finally
      Inp.Free;
   end;
end;
Maybe all the try/finally/end blocks confuse a bit. Essentially you could remove them and have the same code, but if one of the inner streams generate an error, a structure like this will prevent the program from crashing or start leaking memory :)

Then just call it like
Code: [Select]
var MyEncryptedUsername: string;
begin
   MyEncryptedUsername := Encrypt(tl[1].username, 'MySecretKey');
end;
You should of course beware that the output won't look pretty, before you decrypt it

Quote
perhaps a rewrite on blowfish.pp would help maybe.  don't honestly know how to integrate this properly with my data.
It's a very fine generalization as it is currently. Once you get the hang of streams I think you'll learn to appreciate it a lot :)

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #7 on: January 20, 2011, 11:07:40 am »
hi there

i have tried to get it running, but it doesn't seem to work at all

here is my code:

function Encrypt(const a, key: string): string;
var
  I, O, EC: TStream;

begin
  I := TStringStream.Create(a);
  try
    O := TStringStream.Create('');
    try
      EC := TblowfishEncryptStream.Create(Key, O);
      try
        EC.CopyFrom(I, I.Size);
      finally
        EC.Free;
      end;
      result := TStringStream(O).DataString;
    finally
      O.Free;
    end;
  finally
    I.Free;
  end;
end;

procedure EncryptStr;
begin
  el[1].eusername:=Encrypt(tl[1].tempusername, 'key');
  el[1].epassword:=Encrypt(tl[1].temppassword, 'key');
end;                                                     
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #8 on: January 20, 2011, 11:39:43 am »
What's the problem?

jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #9 on: January 20, 2011, 10:10:21 pm »
hi there

the code returns a null string.  this is very frustrating when you cannot find any wikis about this feature.  please help
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

DelphiFreak

  • Sr. Member
  • ****
  • Posts: 255
    • Fresh sound.
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #10 on: January 20, 2011, 11:59:56 pm »
Hi,
I just had a look at the code.

How do the encrypted things from EC get into O?

You say that the Output O is an empty string.
You might want to try to read the string before EC.free is called?
Linux Mint 20.3, Lazarus 2.3, Windows 10, Delphi 10.3 Rio, Delphi 11.1 Alexandria

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #11 on: January 21, 2011, 03:34:12 am »
Try this then
Code: [Select]
...
try
   EC.CopyFrom(I, I.Size);
   EC.Flush;
finally
...

instead of
Code: [Select]
...
try
   EC.CopyFrom(I, I.Size);
finally
...


jshand2010

  • Full Member
  • ***
  • Posts: 236
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #12 on: January 21, 2011, 08:41:45 pm »
if you use a function.  i am confused about the 'result' variable.  how do you use this?
OpenSUSE Tumbleweed x86_64, Lazarus 2.2.0RC2 fixes branch, fpc 3.2.3 fixes branch

likedeeler

  • Newbie
  • Posts: 1
Re: FCL : Blowfish.pp ... Help needed %)
« Reply #13 on: September 17, 2012, 07:47:15 pm »
Hi,

I had a similar problem.
For me a O.seek(0,soFromBeginning) of the stream before the read did do the job.

 

TinyPortal © 2005-2018