Recent

Author Topic: [SOLVED] Failed to decrypt string  (Read 564 times)

artem101

  • Full Member
  • ***
  • Posts: 114
[SOLVED] Failed to decrypt string
« on: October 23, 2025, 03:01:29 pm »
I need to encrypt and decrypt string with key. I made two methods used blowfish and base64.

Code: Pascal  [Select][+][-]
  1. function Encrypt(AKey, ANormalStr: string): string;
  2. var
  3.   EncrStream: TBlowFishEncryptStream;
  4.   StrStream: TStringStream;
  5. begin
  6.   Result := '';
  7.  
  8.   StrStream := TStringStream.Create('');
  9.   try
  10.     EncrStream := TBlowFishEncryptStream.Create(AKey, StrStream);
  11.     try
  12.       EncrStream.WriteAnsiString(ANormalStr);
  13.       Result := EncodeStringBase64(StrStream.DataString);
  14.     finally
  15.       EncrStream.Free;
  16.     end;
  17.   finally
  18.     StrStream.Free;
  19.   end;
  20. end;
  21.  
  22. function Decrypt(AKey, AEncryptedStr: string): string;
  23. var
  24.   DecrStream: TBlowFishDeCryptStream;
  25.   StrStream: TStringStream;
  26. begin
  27.   Result := '';
  28.  
  29.   StrStream := TStringStream.Create(DecodeStringBase64(AEncryptedStr));
  30.   try
  31.     DecrStream := TBlowFishDecryptStream.Create(AKey, StrStream);
  32.     try
  33.       Result := DecrStream.ReadAnsiString;
  34.     finally
  35.       DecrStream.Free;
  36.     end;
  37.   finally
  38.     StrStream.Free;
  39.   end;
  40. end;        

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   s: string;
  4. begin
  5.   s:= 'hellow';
  6.   s := Encrypt('123', s);
  7.   writeln(s); // 0/6+UPi7OPI=
  8.   s := Decrypt('123',s);
  9.   writeln(s);
  10. end;      

Encryption works, but decryption failed with stream read error. What's wrong?
« Last Edit: October 23, 2025, 06:12:21 pm by artem101 »

cdbc

  • Hero Member
  • *****
  • Posts: 2522
    • http://www.cdbc.dk
Re: Failed to decrypt string
« Reply #1 on: October 23, 2025, 03:08:36 pm »
Hi
I once saw an error like that, when I erroneously called "EncryptString" on an encrypted string, instead of calling the right one "DecryptString"...
So I learned the hard way:
Encrypt -> Encrypt = Crashing with streamerrors...!
Just FYI
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Handoko

  • Hero Member
  • *****
  • Posts: 5507
  • My goal: build my own game engine using Lazarus
Re: Failed to decrypt string
« Reply #2 on: October 23, 2025, 04:06:39 pm »
Bug fixed:

Code: Pascal  [Select][+][-]
  1. function Encrypt(AKey, ANormalStr: string): string;
  2. var
  3.   EncrStream: TBlowFishEncryptStream;
  4.   StrStream: TStringStream;
  5. begin
  6.   Result := '';
  7.  
  8.   StrStream := TStringStream.Create('');
  9.   try
  10.     EncrStream := TBlowFishEncryptStream.Create(AKey, StrStream);
  11.     try
  12.       EncrStream.WriteAnsiString(ANormalStr);
  13.     finally
  14.       EncrStream.Free;
  15.       Result := EncodeStringBase64(StrStream.DataString);
  16.     end;
  17.   finally
  18.     StrStream.Free;
  19.   end;
  20. end;

Alternatively:
Code: Pascal  [Select][+][-]
  1. function Encrypt(AKey, ANormalStr: string): string;
  2. var
  3.   EncrStream: TBlowFishEncryptStream;
  4.   StrStream: TStringStream;
  5. begin
  6.   Result := '';
  7.  
  8.   StrStream := TStringStream.Create('');
  9.   try
  10.     EncrStream := TBlowFishEncryptStream.Create(AKey, StrStream);
  11.     try
  12.       EncrStream.WriteAnsiString(ANormalStr);
  13.       EncrStream.Flush;
  14.       Result := EncodeStringBase64(StrStream.DataString);
  15.     finally
  16.       EncrStream.Free;
  17.     end;
  18.   finally
  19.     StrStream.Free;
  20.   end;
  21. end;
« Last Edit: October 23, 2025, 04:08:29 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Failed to decrypt string
« Reply #3 on: October 23, 2025, 05:27:09 pm »
Stream.position := 0;
« Last Edit: October 23, 2025, 05:29:24 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

artem101

  • Full Member
  • ***
  • Posts: 114
Re: Failed to decrypt string
« Reply #4 on: October 23, 2025, 06:11:20 pm »

 

TinyPortal © 2005-2018