[fpc-pascal] Blowfish encryption: What I´m doing wrong?

Guionardo Furlan guionardo at gmail.com
Wed Apr 25 04:56:43 CEST 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180424/31909f6f/attachment.html>


More information about the fpc-pascal mailing list