Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-05-2010, 06:02 PM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default Loading some Strings from a TStream

Hey peeps.

I am trying to keep my textfile encrypted at all times, and my app will of course decrypt it to be able to read it (duh).

The problem is, the only way I got this working, was by saving the Stream to a file, then load that file into my TStringlist. I want to use the LoadFromStream(); method. Heres my en/decryption routines:

Code:
Function TCustomFile.Decrypt(Filename : String):TMemoryStream;
Var
InMS,OutMS : TMemoryStream;
C : Byte;
I : Integer;
Begin

InMS := TMemoryStream.Create;
OutMS := TMemoryStream.Create;
Result := TMemoryStream.Create;
Try

  InMS.LoadFromFile(FileName);
  InMS.Position := 0;
  for I := 0 to InMS.Size - 1 do
    Begin
      InMS.Read(C,1);
      C := (C xor not (ord(FEncKey shr I))) ; //FEncKey is a Word type. Use whatever you'd like, just make sure to use the same value when encrypting!
      OutMS.Write(C,1);
    End;

  OutMS.SaveToStream(Result);

Finally
  InMS.Free;
  OutMS.Free;
  Result.Free;
End;

End;


Procedure TCustomFile.Encrypt(Strm : TStream);
Var
InMS,OutMS : TMemoryStream;
C : Byte;
I : Integer;
Begin

InMS := TMemoryStream.Create;
OutMS := TMemoryStream.Create;


Try

  InMS.LoadFromStream(Strm);
  InMS.Position := 0;
  for I := 0 to InMS.Size - 1 do
    Begin
      InMS.Read(C,1);
      C := (C xor not (ord(FEncKey shr I))) ;
      OutMS.Write(C,1);
    End;
  OutMS.SaveToFile(FFileName);
Finally
  InMS.Free;
  OutMS.Free;
End;

End;
As you see, that routine decrypts my file, but I want to load the decrypted text into my FContents list. Whenever I try to do that, I get nothing. No compiler errors or warnings, or hints. The list just does not have anything in it. But as I said, if I save the OutMS to a file, then load that file into the TStringlist, then it works!..

Thanks!

- Jeff

Last edited by Jeffijoe; 12-07-2010 at 02:56 PM.
Reply With Quote
  #2  
Old 12-07-2010, 02:53 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default

Post your code that writes the file so I can create one to decrypt.
__________________
MrBaseball34

Hook 'em Horns
2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #3  
Old 12-07-2010, 08:08 AM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default

Quote:
Originally Posted by MrBaseball34 View Post
Post your code that writes the file so I can create one to decrypt.
The code that encrypts the file is actually the same as the decryption, heheh :P

I cant post it now, as I dont have my IDE here.
Reply With Quote
  #4  
Old 12-07-2010, 01:23 PM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default

Updated the original post with the routines.
Reply With Quote
  #5  
Old 12-07-2010, 05:42 PM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default

Cool, will take alook this evening.
__________________
MrBaseball34

Hook 'em Horns
2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #6  
Old 12-07-2010, 06:33 PM
mdixon7610 mdixon7610 is offline
Senior Member
 
Join Date: Sep 2003
Posts: 141
Default

You are freeing the Result? Are you using the result of Decrypt?
Reply With Quote
  #7  
Old 12-08-2010, 09:31 AM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default

Quote:
Originally Posted by mdixon7610 View Post
You are freeing the Result? Are you using the result of Decrypt?
Yes to both. I also tried to load the content before freeing, which did not help either.
Reply With Quote
  #8  
Old 12-08-2010, 11:33 AM
mdixon7610 mdixon7610 is offline
Senior Member
 
Join Date: Sep 2003
Posts: 141
Default

You shouldn't be freeing the result. The calling routine should be responsible for that.
Reply With Quote
  #9  
Old 12-08-2010, 04:17 PM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default

Quote:
Originally Posted by mdixon7610 View Post
You shouldn't be freeing the result. The calling routine should be responsible for that.
Well, as I said, I tried to load the contents of the file before freeing it, and that did not work either?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:25 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.