Recent

Author Topic: Runtime exception on encrypting/decrypting streams using DCP  (Read 563 times)

circuitchaser

  • Newbie
  • Posts: 2
Runtime exception on encrypting/decrypting streams using DCP
« on: December 13, 2023, 12:58:25 am »
Hello all,

I trying to make enc/dec using the DCP suite, but I get the "Stream Read Error" from EReadError class on executing the function below.

Code: Pascal  [Select][+][-]
  1. function TForm1.EncriptDecript: boolean;
  2. var
  3.   cipher, decipher: TDCP_rijndael;
  4.   data, outputData: TBytes;
  5.   raw, encripted, decripted: TMemoryStream;
  6. begin
  7.   try
  8.     raw:= TMemoryStream.Create;
  9.     encripted:= TMemoryStream.Create;
  10.     decripted:= TMemoryStream.Create;
  11.  
  12.     SetLength(data, 2);
  13.     SetLength(outputData, 2);
  14.  
  15.     data[0]:= 157;
  16.     data[1]:= 210;
  17.  
  18.     raw.Position:= 0;
  19.     raw.Write(data, sizeof(data));
  20.  
  21.     try
  22.       cipher:= TDCP_rijndael.Create(nil);
  23.       cipher.BlockSize:= 256;
  24.       cipher.InitStr('test', TDCP_sha256);
  25.       cipher.EncryptStream(raw, encripted, raw.Size);
  26.       cipher.Burn;
  27.       cipher.Free;
  28.  
  29.       decipher:= TDCP_rijndael.Create(nil);
  30.       decipher.BlockSize:= 256;
  31.       decipher.InitStr('test', TDCP_sha256);
  32.       decipher.DecryptStream(encripted, decripted, encripted.Size);
  33.       decipher.Burn;
  34.       decipher.Free;
  35.  
  36.       decripted.Position:= 0;
  37.       decripted.ReadBuffer(outputData, SizeOf(outputData));
  38.  
  39.       result:= true;
  40.     except
  41.       ShowMessage('Fatal error.');
  42.     end;
  43.   finally
  44.     FreeAndNil(decripted);
  45.     FreeAndNil(encripted);
  46.     FreeAndNil(raw);
  47.   end;
  48. end;  
  49.  

The objective is read the "outputData" identical as "data" byte array after e/d operations.

Can someone help me please?
Thanks!

dsiders

  • Hero Member
  • *****
  • Posts: 1282
Re: Runtime exception on encrypting/decrypting streams using DCP
« Reply #1 on: December 13, 2023, 01:22:19 am »
Hello all,

I trying to make enc/dec using the DCP suite, but I get the "Stream Read Error" from EReadError class on executing the function below.

Code: Pascal  [Select][+][-]
  1. function TForm1.EncriptDecript: boolean;
  2. var
  3.   cipher, decipher: TDCP_rijndael;
  4.   data, outputData: TBytes;
  5.   raw, encripted, decripted: TMemoryStream;
  6. begin
  7.   try
  8.     raw:= TMemoryStream.Create;
  9.     encripted:= TMemoryStream.Create;
  10.     decripted:= TMemoryStream.Create;
  11.  
  12.     SetLength(data, 2);
  13.     SetLength(outputData, 2);
  14.  
  15.     data[0]:= 157;
  16.     data[1]:= 210;
  17.  
  18.     raw.Position:= 0;
  19.     raw.Write(data, sizeof(data));
  20.  
  21.     try
  22.       cipher:= TDCP_rijndael.Create(nil);
  23.       cipher.BlockSize:= 256;
  24.       cipher.InitStr('test', TDCP_sha256);
  25.       cipher.EncryptStream(raw, encripted, raw.Size);
  26.       cipher.Burn;
  27.       cipher.Free;
  28.  
  29.       decipher:= TDCP_rijndael.Create(nil);
  30.       decipher.BlockSize:= 256;
  31.       decipher.InitStr('test', TDCP_sha256);
  32.       decipher.DecryptStream(encripted, decripted, encripted.Size);
  33.       decipher.Burn;
  34.       decipher.Free;
  35.  
  36.       decripted.Position:= 0;
  37.       decripted.ReadBuffer(outputData, SizeOf(outputData));
  38.  
  39.       result:= true;
  40.     except
  41.       ShowMessage('Fatal error.');
  42.     end;
  43.   finally
  44.     FreeAndNil(decripted);
  45.     FreeAndNil(encripted);
  46.     FreeAndNil(raw);
  47.   end;
  48. end;  
  49.  

The objective is read the "outputData" identical as "data" byte array after e/d operations.

Can someone help me please?
Thanks!

You did not reset the position for the Encripted stream after it was written by Cipher and before it was read by Decipher.

Code: Pascal  [Select][+][-]
  1. Encipted.Position := 0;
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

circuitchaser

  • Newbie
  • Posts: 2
Re: Runtime exception on encrypting/decrypting streams using DCP
« Reply #2 on: December 13, 2023, 02:43:16 am »
Hello dsiders, thanks for the answer.

Quote
You did not reset the position for the Encripted stream after it was written by Cipher and before it was read by Decipher.

I changed the code, but the error persists.
See:

Code: Pascal  [Select][+][-]
  1. try
  2.       cipher:= TDCP_rijndael.Create(nil);
  3.       cipher.BlockSize:= 256;
  4.       cipher.InitStr('test', TDCP_sha256);
  5.       cipher.EncryptStream(raw, encripted, raw.Size);
  6.       cipher.Burn;
  7.       cipher.Free;
  8.  
  9.       //The change
  10.       encripted.Position:= 0;
  11.  
  12.       decipher:= TDCP_rijndael.Create(nil);
  13.       decipher.BlockSize:= 256;
  14.       decipher.InitStr('test', TDCP_sha256);
  15.       decipher.DecryptStream(encripted, decripted, encripted.Size);
  16.       decipher.Burn;
  17.       decipher.Free;
  18.  
  19.       decripted.Position:= 0;
  20.       decripted.ReadBuffer(outputData, SizeOf(outputData));
  21.  
  22.       result:= true;
  23.     except
  24.       ShowMessage('Fatal error.');
  25.     end;    
  26.  

Note: when debugging, I saw from watches and noted the "encripted" shows "MEMORY: nil" on properties (attached images).

TRon

  • Hero Member
  • *****
  • Posts: 3634
Re: Runtime exception on encrypting/decrypting streams using DCP
« Reply #3 on: December 13, 2023, 04:30:03 am »
@circuitchaser:
It goes for every stream so also for your raw stream. You have written data to it yet expect the position to be at position 0 when you feed it to encryptstream.

Also when you read and write buffers to and from streams always use their index(ed byte) otherwise it will overwrite the types internal storage format.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

 

TinyPortal © 2005-2018