Blowfish encryption: What I´m doing wrong?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Blowfish encryption: What I´m doing wrong?

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Blowfish encryption: What I´m doing wrong?

Peter
On 25/04/18 03:56, Guionardo Furlan wrote:

> 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  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Hi,

Has your "FKey" been properly created and initialised?

Regards,
Peter B

 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Blowfish encryption: What I´m doing wrong?

Michael Van Canneyt
In reply to this post by Guionardo Furlan


On Tue, 24 Apr 2018, Guionardo Furlan wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal