[fpc-pascal] TBlowfishDecryptStream flaw

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Sun Mar 6 02:40:59 CET 2011


i have a problem with TBlowfishDecryptStream :

			Try
				lBuffer := TMemoryStream.Create;
				lSource := (aObject As TMemoryStream);
				lSource.Seek(0, soFromBeginning);
				lCypher := TBlowfishDecryptStream.Create(fKey, lSource);
				lBuffer.LoadFromStream(lCypher);
			Except
				On E: Exception Do
					WriteLn(E.ClassName, '->', E.Message);
			End;

Spits out the "EBlowFishError" exception with "Seek not allowed on
encryption streams"

The problem is basically that TBlowfishDecryptStream does not allow to
use the "size" propertie, making it very hard to use it.

There are various ways to do this, for one :

			Try
				lBuffer := TMemoryStream.Create;
				lSource := (aObject As TMemoryStream);
				lSource.Seek(0, soFromBeginning);
				lCypher := TBlowfishDecryptStream.Create(fKey, lSource);
				lBuffer.CopyFrom(lCypher, lCypher.Size);
			Except
				On E: Exception Do
					WriteLn(E.ClassName, '->', E.Message);
			End;

But this gives the exact same error,

the last one is this :

			Try
				lBuffer := TMemoryStream.Create;
				lSource := (aObject As TMemoryStream);
				lSource.Seek(0, soFromBeginning);
				lCypher := TBlowfishDecryptStream.Create(fKey, lSource);
				lBuffer.CopyFrom(lCypher, lSource.Size);
			Except
				On E: Exception Do
					WriteLn(E.ClassName, '->', E.Message);
			End;

The problem now is that it eats chars at the end of the generated
lBuffer (tested by taking a tmemorystream thru tblowfishencryptstream,
then taking the result to this tblowfishdecryptstream, than back to a
tmemorystream and comparing the results, the original tmemorystream
shows to be bigger than the resulting tmemorystream)...

Basically i need :

1 - That TBlowfishEncryptStream guarantees that the same ammount of
bytes feed to it, ends up at the output of it
2 - That TBlowfishDecryptStream simply allows us to use Size property
(Well, sometimes i receive a tmemorystream in an unrelated part of the
code so i cannot read the original size the buffer had before
encrypting)...

Test case :

===== Test7.pas ======

Uses
	Classes,
	BlowFish;

Var
	lSource,
	lIntermediate,
	lDestination : TMemoryStream;
	lEncrypter : TBlowfishEncryptStream;
	lDecrypter : TBlowfishDecryptStream;

Begin
	lSource := TMemoryStream.Create;
	lIntermediate := TMemoryStream.Create;
	lDestination := TMemoryStream.Create;

	lSource.LoadFromFile('test7.pas');
	WriteLn(lSource.Size);
	
	lEncrypter := TBlowfishEncryptStream.Create('123', lIntermediate);
	lEncrypter.CopyFrom(lSource, lSource.Size);
	WriteLn(lIntermediate.Size);
	lIntermediate.Seek(0, soFromBeginning);

	lDecrypter := TBlowfishDecryptStream.Create('123', lIntermediate);
	lDestination.CopyFrom(lDecrypter, lSource.Size);
	
	WriteLn(lDestination.Size);
	
	lDestination.SaveToFile('test7.txt');
	
	lSource.Free;
	lIntermediate.Free;
	lDestination.Free;
End.
==== output =====

2147
2144
2144

IE : It misses 3 chars...



More information about the fpc-pascal mailing list