Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-04

Blowfish encryption: What I´m doing wrong?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Guionardo Furlan

Hello guys

I´ve a single code to encrypt/decrypt strings using Blowfish.

function TBlowFishCripto.Encrypt(const Value: string): string;
var
  en: TBlowFishEncryptStream;
  stream: TStringStream;
begin
  if length(Value) = 0 then
  begin
    Result := '';
    Exit;
  end;
  try
    stream := TStringStream.Create('');
    en := TBlowFishEncryptStream.Create(FKey, stream);
    en.Write(Value[1], Length(Value));
    Result := stream.DataString;
  finally
    FreeAndNil(en);
    FreeAndNil(stream);
  end;
end;
function TBlowFishCripto.Decrypt(const encrypted: string;
  out decrypted: string): boolean;
var
  de: TBlowFishDeCryptStream;
  s2: TStringStream;
  decr: string;
begin
  if length(encrypted) = 0 then
  begin
    Result := True;
    decrypted := '';
    Exit;
  end;
   { 4 }
  s2 := TStringStream.Create(encrypted);
  { 5 }
  de := TBlowFishDeCryptStream.Create(FKey, s2);
  { 6 }
  decr := de.ReadAnsiString;
  de.Free;
  s2.Free;
  decrypted:=decr;
  REsult:=true;
    FreeAndNil(de);
    FreeAndNil(s2);
end;

When the method Decrypt runs with a argument 'encrypted' with a string returned by the method Encrypt, it raise an exception 203 on line after {6} I don´t know how to debug this.

*Timeo hominem unius libri* []s Guionardo Furlan http://www.guionardofurlan.com.br
Peter
to Guionardo Furlan
Guionardo Furlan wrote 72 lines
Hello guys I´ve a single code to encrypt/decrypt strings using Blowfish. function TBlowFishCripto.Encrypt(const Value: string): string; var   en: TBlowFishEncryptStream;   stream: TStringStream; begin   if length(Value) = 0 then   begin     Result := '';     Exit;   end;   try     stream := TStringStream.Create('');     en := TBlowFishEncryptStream.Create(FKey, stream);     en.Write(Value[1], Length(Value));     Result := stream.DataString;   finally     FreeAndNil(en);     FreeAndNil(stream);   end; end;      function TBlowFishCripto.Decrypt(const encrypted: string;   out decrypted: string): boolean; var   de: TBlowFishDeCryptStream;   s2: TStringStream;   decr: string; begin   if length(encrypted) = 0 then   begin     Result := True;     decrypted := '';     Exit;   end;    { 4 }   s2 := TStringStream.Create(encrypted);   { 5 }   de := TBlowFishDeCryptStream.Create(FKey, s2);   { 6 }   decr := de.ReadAnsiString;   de.Free;   s2.Free;   decrypted:=decr;   REsult:=true;     FreeAndNil(de);     FreeAndNil(s2); end;  When the method Decrypt runs with a argument 'encrypted' with a string returned by the method Encrypt, it raise an exception 203 on line after {6}  I don´t know how to debug this. -- /Timeo hominem unius libri/ []s Guionardo Furlan http://www.guionardofurlan.com.br _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Hi,

Has your "FKey" been properly created and initialised?

Regards,
Peter B

 

Michael Van Canneyt
to Guionardo Furlan: Hello guys I´ve a single code to encrypt/decrypt strings using Blowfish. function…
Guionardo Furlan wrote 16 lines
Hello guys I´ve a single code to encrypt/decrypt strings using Blowfish. function TBlowFishCripto.Encrypt(const Value: string): string; var en: TBlowFishEncryptStream; stream: TStringStream; begin { 4 } s2 := TStringStream.Create(encrypted); { 5 } de := TBlowFishDeCryptStream.Create(FKey, s2); { 6 } decr := de.ReadAnsiString;

You can only read an ansistring that was written with WriteAnsistring. This is not the case here. You must either use WriteAnstring when writing, or read the max available bytes yourself.

Michael.