Hi,
I am trying to Encrypt and Decrypt RTF in a TMemoryStream using TRichViewEdit but I am having major problems with it. To Encrypt/Decrypt I'm using the DCPcrypt latest versions the Encryption Component I'm using is TDCP_rc4
I Encrypt the RTF like this:
procedure TForm1.Button1Click(Sender: TObject);//Encrypt
var
Password: string;
source,Dest: TMemoryStream;
begin
Password := '3FB86FD0FE30BA119033447D0CF16CE4';
Source := TMemoryStream.Create;
Dest := TMemoryStream.Create;
Source.Seek(0,soFromBeginning);
Dest.Seek(0,soFromBeginning);
RichViewEdit1.SaveRTFToStream(Source,false);
Source.Seek(0,soFromBeginning);
RichViewEdit1.Clear;
Cipher.InitStr(Password,TDCP_sha512);
Cipher.EncryptStream(Source,Dest,Source.Size);
Dest.Seek(0,soFromBeginning);
RichViewEdit1.LoadRTFFromStream(Dest);
RichViewEdit1.Format;
Source.Free;
Dest.Free;
Cipher.Burn;
end;
and Decrypt like this:
procedure TForm1.Button2Click(Sender: TObject);//Decrypt
var
Password: string;
source,Dest: TMemoryStream;
begin
Password := '3FB86FD0FE30BA119033447D0CF16CE4';
Source := TMemoryStream.Create;
Dest := TMemoryStream.Create;
Source.Seek(0,soFromBeginning);
Dest.Seek(0,soFromBeginning);
RichViewEdit1.SaveRVFToStream(Source,false);
Source.Seek(0,soFromBeginning);
RichViewEdit1.Clear;
Cipher.InitStr(Password,TDCP_sha512);
Cipher.DecryptStream(Source,Dest,Source.Size);
Dest.Seek(0,soFromBeginning);
RichViewEdit1.LoadRTFFromStream(Dest);
RichViewEdit1.Format;
Source.Free;
Dest.Free;
Cipher.Burn;
end;
The problem I'm getting is the DecryptStream part, it just formats jumbled up characters instead of Decrypting it from the Stream, is this something I'm doing wrong in the Decryption part or should I be doing something else with TRichViewEdit?
Is it because TRichViewEdit cannot display some Characters that are encrypted? I have tried using LoadRVFFromStream but I always get nothing as if there isn't even anything in the Stream
Please if anyone knows what is wrong, please help, I have been tearing my hair out with it for days!!
many thanks
Chris
RichViewEdit and Streams
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
RichViewEdit and Streams
Hi,
Thank you very much for replying,
I have tried what you suggested and it works ok but I did notice that it adds a clear line at the beginning of the inserted stream. Here is what I have done:
//----------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);//Encrypt
var
source: TMemoryStream;
begin
Source := TMemoryStream.Create;
Source.Seek(0,soFromBeginning);
RichViewEdit1.SaveRTFToStream(Source,false);
Source.Seek(0,soFromBeginning);
RichViewEdit1.Clear;
Source.Seek(0,soFromBeginning);
RichViewEdit2.LoadRTFFromStream(Source);
RichViewEdit2.Format;
Source.Free;
end;
//----------------------------------------------------------------------------------
I have just commented out the Encryption relevant part which I have not included again here to make it easier on the eyes! and added another TRichViewEdit to load the text into. There is a Blank Line at the top of the RichViewEdit after the text has been loaded from the stream.
Do you think this is what is causing the problem? How do I remove this blank line at the beginning?
Thanks again for replying,
Chris
Thank you very much for replying,
I have tried what you suggested and it works ok but I did notice that it adds a clear line at the beginning of the inserted stream. Here is what I have done:
//----------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);//Encrypt
var
source: TMemoryStream;
begin
Source := TMemoryStream.Create;
Source.Seek(0,soFromBeginning);
RichViewEdit1.SaveRTFToStream(Source,false);
Source.Seek(0,soFromBeginning);
RichViewEdit1.Clear;
Source.Seek(0,soFromBeginning);
RichViewEdit2.LoadRTFFromStream(Source);
RichViewEdit2.Format;
Source.Free;
end;
//----------------------------------------------------------------------------------
I have just commented out the Encryption relevant part which I have not included again here to make it easier on the eyes! and added another TRichViewEdit to load the text into. There is a Blank Line at the top of the RichViewEdit after the text has been loaded from the stream.
Do you think this is what is causing the problem? How do I remove this blank line at the beginning?
Thanks again for replying,
Chris
-
- Site Admin
- Posts: 17557
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
LoadRTF and LoadRTFFromStream do not clear the existing document, they append RTF data to the end of it.
Each TRichViewEdit initially has one empty text line.
Solution: call RichViewEdit.Clear before loading RTF. Do not call RichViewEdit.Format between Clear and loading, because Format adds this line again if RichViewEdit is empty.
Each TRichViewEdit initially has one empty text line.
Solution: call RichViewEdit.Clear before loading RTF. Do not call RichViewEdit.Format between Clear and loading, because Format adds this line again if RichViewEdit is empty.