FCL : Blowfish.pp ... Help needed %) lazarus / 31-LCL ---------------------------------------------------------------------- jshand2010 · 2011-01-16 09:40 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 ---------------------------------------------------------------------- Laksen · 2011-01-16 09:56 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 · 2011-01-16 10:02 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 ---------------------------------------------------------------------- marcov · 2011-01-17 05:43 Quote from: jshand2010 on January 16, 2011, 11:02:22 amthanks 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 · 2011-01-19 22:43 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 ---------------------------------------------------------------------- captian jaster · 2011-01-20 02:28 Quote from: jshand2010 on January 19, 2011, 11:43:57 pmhi 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 · 2011-01-20 02:48 Quote from: jshand2010 on January 19, 2011, 11:43:57 pmhi 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 Quoteperhaps 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 · 2011-01-20 10:07 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; ---------------------------------------------------------------------- Laksen · 2011-01-20 10:39 What's the problem? ---------------------------------------------------------------------- jshand2010 · 2011-01-20 21:10 hi there the code returns a null string.  this is very frustrating when you cannot find any wikis about this feature.  please help ---------------------------------------------------------------------- DelphiFreak · 2011-01-20 22:59 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? ---------------------------------------------------------------------- Laksen · 2011-01-21 02:34 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 · 2011-01-21 19:41 if you use a function.  i am confused about the 'result' variable.  how do you use this? ---------------------------------------------------------------------- likedeeler · 2012-09-17 17:47 Hi, I had a similar problem. For me a O.seek(0,soFromBeginning) of the stream before the read did do the job.