Recent

Author Topic: encrypt text and savedialog, opendialog and decrypt text file  (Read 2918 times)

diesel

  • Newbie
  • Posts: 5
encrypt text and savedialog, opendialog and decrypt text file
« on: February 14, 2019, 07:39:20 pm »
Hello i am using savedialog to save memo into TEXT FILE, and opendialog to open that TEXT FILE (it is app for more users).
it works, i am using these codes:
Code: Pascal  [Select][+][-]
  1. if SaveDialog1.Execute then
  2.     Memo1.Lines.SaveToFile(SaveDialog1.Filename);

Code: Pascal  [Select][+][-]
  1. OpenDialog1.Filter := 'All Files|*.txt';
  2.     if opendialog1.execute then
  3.     begin
  4.      assignfile(t,opendialog1.filename);
  5.      reset(t);
  6.  
  7.      while not  eof(t) do
  8.   begin
  9.     ReadLn(t,s);
  10.     memo2.lines.add(s);
  11.   end;

I need to encrypt memo.lines then save it. So Saved text document that is in computer is encrypted. Then open text file(opendialog) put it into memo and decrypt it for using.
 I found this

Code: Pascal  [Select][+][-]
  1. uses BlowFish;
  2.  
  3. procedure EncryptAndWriteToFile(Filename: string; KeyPhrase: string; Value: string);
  4. var
  5.   en: TBlowFishEncryptStream;
  6.   s1: TFileStream;
  7. begin
  8.   s1 := TFileStream.Create(Filename, fmCreate or fmOpenReadWrite);
  9.   en := TBlowFishEncryptStream.Create(KeyPhrase, s1);
  10.   try
  11.     en.WriteAnsiString(Value);
  12.   finally
  13.     en.Free; // important to free en first (need for flush to s1)
  14.     s1.Free;
  15.   end;
  16. end;
  17.  
  18. function LoadFromFileAndDecrypt(Filename: string; KeyPhrase: string): string;
  19. var
  20.   de: TBlowFishDeCryptStream;
  21.   s1: TFileStream;
  22.   Value, temp: string;
  23. begin
  24.   Result := 'error';
  25.   s1 := TFileStream.Create(Filename, fmOpenRead);
  26.   de := TBlowFishDecryptStream.Create(KeyPhrase, s1);
  27.   try
  28.     Result := de.ReadAnsiString;
  29.   finally
  30.     s1.Free;
  31.     de.Free;
  32.   end;
  33. end;
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.   Encrypted: string;
  38. begin
  39.   EncryptAndWriteToFile('C:\TEMP\TEST.TXT', '123', Memo1.Lines.Text);
  40. end;
  41.  
  42. procedure TForm1.Button2Click(Sender: TObject);
  43. begin
  44.   Memo2.Lines.Text := LoadFromFileAndDecrypt('C:\TEMP\TEST.TXT', '123');
  45. end;

And it works but you can see it is not using savedialog, neither opendialog. It has defined path to the file.
Can you help me how to rewrite it for open/save dialog?
I am sure there are mistakes in my eng text, sorry.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #1 on: February 14, 2019, 08:05:40 pm »
Code: Pascal  [Select][+][-]
  1.   if OpenDialog.Execute then
  2.     Memo.Text := LoadFromFileAndDecrypt(OpenDialog.Filename, KeyPhrase);
  3.  
  4. { ... }
  5.  
  6.   if SaveDialog.Execute then
  7.     EncryptAndWriteToFile(SaveDialog.Filename, KeyPhrase, Memo.Lines.Text);
  8.  

Remember that OpenDialog and SaveDialog only function is to give you a file name. Whatever you do with it is up to you :)
« Last Edit: February 14, 2019, 08:09:54 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

diesel

  • Newbie
  • Posts: 5
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #2 on: February 15, 2019, 05:27:15 pm »
Thank you it works. But when i try to opendialog, i have errors when i try to open UNencrypted file, or crypted in else keyphrase as i set. Can you help me change
Code: Pascal  [Select][+][-]
  1. function LoadFromFileAndDecrypt(Filename: string; KeyPhrase: string): string;
  2. var
  3.   de: TBlowFishDeCryptStream;
  4.   s1: TFileStream;
  5.   Value, temp: string;
  6. begin
  7.   Result := 'error';
  8.   s1 := TFileStream.Create(Filename, fmOpenRead);
  9.   de := TBlowFishDecryptStream.Create(KeyPhrase, s1);
  10.   try
  11.     Result := de.ReadAnsiString;
  12.   finally
  13.     s1.Free;
  14.     de.Free;
  15.   end;
  16. end;

to be able do this:

Code: Pascal  [Select][+][-]
  1. Memo1.Text := LoadFromFileAndDecrypt(Memo2.text, KeyPhrase);

Like we can rename it to ReadFromFileAndDecrypt, decrypt memo2.text to memo1 text. Or am i trying to do this bad?
I tryed to change function- filename string-S1 to something like TstringList but not succesfully

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #3 on: February 15, 2019, 10:46:45 pm »
When you try to decrypt a file which is not encrypted or is encrypted with a different key the result, obviously, can not be correct. You should try to determine whether a file is encrypted and, if so, ask the user for the key. A quick and dirty example:

Code: Pascal  [Select][+][-]
  1. resourcestring
  2.   sPassCaption = 'Need a password!';
  3.   sPassPrompt  = 'File %s is encrypted; please supply the key';
  4.  
  5. procedure OpenFileInMemo(AMemo: TMemo; DontDecrypt: Boolean = False);
  6. var
  7.   AFilename,
  8.   APassword: String;
  9. begin
  10.   if OpenDialog.Execute then begin
  11.     AFilename := OpenDialog.Filename;
  12. { You'll have to build your own FileIsEncrypted() function!!! }
  13.     if DontDecrypt or (not FileIsEncrypted(AFilename)) then
  14.       AMemo.LoadFromFile(AFilename)
  15.     else begin
  16.       APassword := PasswordBox(sPassCaption,
  17.                      Format(sPassPrompt, [ExtractFilename(AFilename)]);
  18.       AMemo.Text := LoadFromFileAndDecrypt(AFilename, APasword);
  19.     end;
  20.   end;
  21. end;

Do note that this loads (and, if needed, decrypts) directly from a file to a TMemo. If, as it looks from your questions, you want to have two memos, one with the encrypted and one with the decrypted text you'll have to do something like:

Code: Pascal  [Select][+][-]
  1. function DecryptFromMemo(AMemo: TMemo; AKey: String): String;
  2. var
  3.   de: TBlowFishDeCryptStream;
  4.   s1: TStringStream;
  5.   Value, temp: string;
  6. begin
  7.   Result := 'error';
  8.   s1 := TStringStream.Create(AMemo.Text);
  9.   try
  10.     de := TBlowFishDecryptStream.Create(KeyPhrase, s1);
  11.     try
  12.       Result := de.ReadAnsiString;
  13.     finally
  14.       de.Free;
  15.     end;
  16.   finally
  17.     s1.Free;
  18.   end;
  19. end;

Which you may then use from something like:

Code: Pascal  [Select][+][-]
  1. procedure LoadMemos;
  2. var
  3.   APassword: String;
  4. begin
  5.   if OpenDialog.Execute then begin
  6.     Memo2.LoadFromFile(OpenDialog.Filename);
  7.     APassword := PasswordBox('My kingdom for a key!', 'Have you got a key?');
  8.     if APassword <> '' then
  9.       Memo1.Text := DecryptFromMemo(Memo2, APassword)
  10.     else
  11.       ShowMessage('No password? No kingdom!');
  12.   end;
  13. end;

Note that I typed all this directly and it's un-syntax-checked and untested, so there may be some (small) error(s); but it should be enough for you to get what I'm trying to show you.

HTH!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

diesel

  • Newbie
  • Posts: 5
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #4 on: February 16, 2019, 07:19:51 pm »
Thank you, it is what i was looking for. But IDK why it doesn´t work, it just add empty line im memo. I was looking for more methods, here they are in forum
https://forum.lazarus.freepascal.org/index.php?topic=33013.0, but it still just add empty line in memo
here is my code.

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, BlowFish;
  3.  
  4. type
  5.  
  6.   { TForm1 }
  7.  
  8.   TForm1 = class(TForm)
  9.     Button1: TButton;
  10.     Button2: TButton;
  11.     Button3: TButton;
  12.     Memo1: TMemo;
  13.     Memo2: TMemo;
  14.     OpenDialog1: TOpenDialog;
  15.     SaveDialog1: TSaveDialog;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure Button2Click(Sender: TObject);
  18.     procedure Button3Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.   t: textfile;
  28.   s, Keyphrase: string;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure EncryptAndWriteToFile(Filename: string; KeyPhrase: string; Value: string);
  37. var
  38.   en: TBlowFishEncryptStream;
  39.   s1: TFileStream;
  40. begin
  41.   s1 := TFileStream.Create(Filename, fmCreate or fmOpenReadWrite);
  42.   en := TBlowFishEncryptStream.Create(KeyPhrase, s1);
  43.   try
  44.     en.WriteAnsiString(Value);
  45.   finally
  46.     en.Free; // important to free en first (need for flush to s1)
  47.     s1.Free;
  48.   end;
  49. end;
  50.  
  51. function DecryptFromMemo(AMemo: TMemo; keyphrase: String): String;
  52. var
  53.   de: TBlowFishDeCryptStream;
  54.   s1: TStringStream;
  55.   Value, temp: string;
  56. begin
  57.   Result := 'error';
  58.   s1 := TStringStream.Create(AMemo.Text);
  59.   try
  60.     de := TBlowFishDecryptStream.create(Keyphrase, s1);
  61.     try
  62.       Result := de.ReadAnsiString;
  63.     finally
  64.       de.Free;
  65.     end;
  66.   finally
  67.     s1.Free;
  68.   end;
  69. end;
  70.  
  71. procedure TForm1.Button1Click(Sender: TObject);
  72. begin
  73.   memo1.clear;
  74.   memo2.clear;
  75. end;
  76.  
  77. procedure TForm1.Button2Click(Sender: TObject);
  78. begin
  79.   KeyPhrase:='123';
  80.   if SaveDialog1.Execute then
  81.     EncryptAndWriteToFile(SaveDialog1.Filename, KeyPhrase, Memo1.Lines.Text);
  82. end;
  83.  
  84. procedure TForm1.Button3Click(Sender: TObject);
  85. begin
  86.  
  87.   OpenDialog1.Filter := 'All Files|*.txt';
  88.     if opendialog1.execute then
  89.     begin
  90.      assignfile(t,opendialog1.filename);
  91.      reset(t);
  92.  
  93.      while not  eof(t) do
  94.   begin
  95.     ReadLn(t,s);
  96.     memo2.lines.add(s);
  97.   end;
  98.      KeyPhrase:='123';
  99.     Memo1.lines.add(DecryptFromMemo(Memo2, Keyphrase));
  100.  
  101. end;
  102.  
  103. end;
  104. end.
any ideas what should i do?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #5 on: February 17, 2019, 12:15:30 am »
Humm... For some reason--probably the "strange" characters that the encryption produces--you can't load the encrypted file as-is in a TMemo. The important thing, though, is that it's both possible and relatively easy to save/load encrypted text.

You'll find attached a full project that shows both: the encryoted text not  loading to memo2 and how to, nonetheless, load the decrypted text to memo1.

Hope it helps!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

diesel

  • Newbie
  • Posts: 5
Re: encrypt text and savedialog, opendialog and decrypt text file
« Reply #6 on: February 17, 2019, 11:06:05 am »
Thanks! you helped me alot.

 

TinyPortal © 2005-2018