Recent

Author Topic: Encrypt a text (easily)  (Read 32279 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 378
    • ericksystem software
Encrypt a text (easily)
« on: June 16, 2016, 11:09:25 pm »
Hi my friends, as I can easily encrypt text and then save it as txt  :)
I love desktop software
https://www.ericksystem.com

sfeinst

  • Full Member
  • ***
  • Posts: 245
Re: Encrypt a text (easily)
« Reply #1 on: June 17, 2016, 12:04:16 am »
Install the DCPCrypt package.  The following is a very quick example to encrypt a string and then decrypt it to show the steps.  You can of course do what you want with the encrypted and decrypted values.  This is a quick example and I am not stating it is the best way to handle it.  Just an example for starters.

Code: Pascal  [Select][+][-]
  1. var
  2.   Crypt: TDCP_rijndael;
  3.   PlainText: string;
  4.   EncText: string;
  5.   DecText: string;
  6.   Pwd: string;
  7.   Msg: string;
  8. begin
  9.   PlainText := 'Example Text';
  10.   Pwd := 'MyPassword';
  11.   Crypt := TDCP_rijndael.Create(nil);
  12.   try
  13.     // encrypt string using password
  14.     Crypt.InitStr(Pwd, TDCP_sha256);
  15.     EncText := Crypt.EncryptString(PlainText);
  16.  
  17.     // now decrypt it to show it works
  18.     Crypt.InitStr(Pwd, TDCP_sha256);
  19.     DecText := Crypt.DecryptString(EncText);
  20.  
  21.     // display it all
  22.     Msg := 'Plain Text: ' + PlainText;
  23.     Msg := Msg + LineEnding + 'Encrypted: ' + EncText;
  24.     Msg := Msg + LineEnding + 'Decrypted: ' + DecText;
  25.     ShowMessage(Msg);
  26.   finally
  27.     FreeAndNil(Crypt);
  28.   end;
  29.  

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
Re: Encrypt a text (easily)
« Reply #2 on: June 17, 2016, 01:34:24 am »
OTP

Code: [Select]
var
  PlainText: string;
  Pwd: string;
  EncText: string;
  i, len: integer;
begin
  len := length(PlainText);
  if len > length(Pwd) then len := length(Pwd);
  EncText := copy(PlainText, 1, len);
  for i := 1 to len do
     EncText[i] := chr(ord(PlainText[i]) xor ord(Pwd[i]));
   
end;


Not only is it trivial, it is the world's most secure encyption.

It is completely unbreakable

Just need to take care that the password is fully random, and is only used once for encryption.

lainz

  • Hero Member
  • *****
  • Posts: 4725
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Encrypt a text (easily)
« Reply #3 on: June 17, 2016, 02:44:21 am »
Hi BeniBela, I don't fully understand that code, you or someone else can comment it?

Also I need to store the password to then decompress the file, or how I can decompress the file?

Roland57

  • Hero Member
  • *****
  • Posts: 505
    • msegui.net
Re: Encrypt a text (easily)
« Reply #4 on: June 17, 2016, 06:17:01 am »
Hi BeniBela, I don't fully understand that code, you or someone else can comment it?

Also I need to store the password to then decompress the file, or how I can decompress the file?

Hello! The same code crypts and decrypts.

Code: Pascal  [Select][+][-]
  1. function Crypt(const aText: string): string;
  2. const
  3.   PWD = 'http://forum.lazarus.freepascal.org/index.php/topic,33013.msg213192.html#msg213192';
  4. var
  5.   i, len: integer;
  6. begin
  7.   len := Length(aText);
  8.   if len > Length(PWD) then
  9.     len := Length(PWD);
  10.   SetLength(result, len);
  11.   for i := 1 to len do
  12.     result[i] := Chr(Ord(aText[i]) xor Ord(PWD[i]));
  13. end;
  14.  
  15. var
  16.   s: string;
  17.  
  18. begin
  19.   s := 'abracadabra';
  20.   s := Crypt(s);
  21.   WriteLn(s);
  22.   s := Crypt(s); // abracadabra
  23.   WriteLn(s);
  24. end.

If you want to fully understand how it works, you have to pay attention to what the xor operator does.

Did that answers your question ?

« Last Edit: June 17, 2016, 06:24:01 am by RolandC »
My projects are on Gitlab and on Codeberg.

Roland57

  • Hero Member
  • *****
  • Posts: 505
    • msegui.net
My projects are on Gitlab and on Codeberg.

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Encrypt a text (easily)
« Reply #6 on: June 17, 2016, 07:15:26 am »

Not only is it trivial, it is the world's most secure encyption.

It is completely unbreakable

Just need to take care that the password is fully random, and is only used once for encryption.

Trivial yes, completely unbreakable???

Cmon, easily broken but yes for most security encryption it would suffice.
A halo is a mere circle, when does it end?

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Encrypt a text (easily)
« Reply #7 on: June 17, 2016, 09:22:06 am »

Not only is it trivial, it is the world's most secure encyption.

It is completely unbreakable

Just need to take care that the password is fully random, and is only used once for encryption.

Trivial yes, completely unbreakable???

Cmon, easily broken but yes for most security encryption it would suffice.

Well, that guy is known to be insufficiently informed.
In theory, and provable, if the key length - only if - is the same size as the encrypted text and has sufficient entropy! XOR  encryption is almost/virtually unbreakable (rule number one of statistics: if it has a probability there is a probability) if you have no knowledge about the source text. In the case of a password, however, with the code presented, it is indeed very very easy to break by simple statistics. 
That is due to the repeating nature of the password/plaintext. When you hit a common language probability by probing length (easy) and thus determining a viable distribution for characters (which are a known) you can then attempt to decrypt with an almost guaranteed 100% success rate.

Character distribution means that you can even identify most languages if you recovered the password length. Very simple statistics. Both operations. XOR encryption over a shorter key-length has negligible entropy and is therefor unsafe. With the exception I described: key length equals plaintext length.
There's also a matter of very short texts: that scenario can be brute forced. Given the known character distribution over  a language.

Also note the time to content ratio: for how long need it to be secret? Most people forget about that. Encryption is as good as the time that it costs to break it.

XOR encryption with a password can best be described as the blurring of an image: you can still see the bigger picture but need the right lens to focus.
« Last Edit: June 17, 2016, 10:19:40 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

rvk

  • Hero Member
  • *****
  • Posts: 6802
Re: Encrypt a text (easily)
« Reply #8 on: June 17, 2016, 01:18:59 pm »
(Ok, now in the correct thread. I posted this incorrectly elsewhere)

Isn't blowfish mentioned here yet? Or is it too weak?

See the thread:
http://forum.lazarus.freepascal.org/index.php/topic,19663.msg112190.html#msg112190

Standard in Lazarus and simple example:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses classes,blowfish,windows;
  4.  
  5. var
  6.   en: TBlowFishEncryptStream;
  7.   de: TBlowFishDeCryptStream;
  8.   s1,s2: TStringStream;
  9.   key,value,temp: String;
  10. begin
  11.   key := 'testkey';
  12.   value := 'this is a string';
  13.  
  14.   s1 := TStringStream.Create('');
  15.   en := TBlowFishEncryptStream.Create(key,s1);
  16.   en.Write(value[1],Length(value));
  17.   en.Free;
  18.  
  19.  
  20.   MessageBox(0,PChar(s1.DataString),'',MB_OK);
  21.   s2 := TStringStream.Create(s1.DataString);
  22.   s1.Free;
  23.  
  24.   de := TBlowFishDeCryptStream.Create(key,s2);
  25.  
  26.   SetLength(temp,s2.Size);
  27.   de.Read(temp[1],s2.Size);
  28.  
  29.   MessageBox(0,PChar(temp),'',MB_OK);
  30.   de.Free;
  31.  
  32.   s2.Free;
  33. end.

lainz

  • Hero Member
  • *****
  • Posts: 4725
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Encrypt a text (easily)
« Reply #9 on: June 17, 2016, 04:21:19 pm »
Hi BeniBela, I don't fully understand that code, you or someone else can comment it?

Also I need to store the password to then decompress the file, or how I can decompress the file?

Hello! The same code crypts and decrypts.

... code ...

If you want to fully understand how it works, you have to pay attention to what the xor operator does.

Did that answers your question ?

Yes, thanks, now is much clear to me.

Ericktux

  • Sr. Member
  • ****
  • Posts: 378
    • ericksystem software
Re: Encrypt a text (easily)
« Reply #10 on: June 17, 2016, 05:09:58 pm »
thank for help, i test this code also:

Code: Pascal  [Select][+][-]
  1. Uses BlowFish;
  2.  
  3. function encrypt(cle:string;chaine:string):string;
  4. var
  5.   en: TBlowFishEncryptStream;
  6.   de: TBlowFishDeCryptStream;
  7.   s1,s2: TStringStream;
  8.   value,temp: String;
  9. begin
  10.     s1 := TStringStream.Create('');
  11.     en := TBlowFishEncryptStream.Create(cle,s1);
  12.     en.WriteAnsiString(chaine);
  13.     en.Free;
  14.     result:=s1.datastring;
  15. end;
  16.  
  17. function decrypt(cle:string;chaine:string):string;
  18. var
  19.   en: TBlowFishEncryptStream;
  20.   de: TBlowFishDeCryptStream;
  21.   s1,s2: TStringStream;
  22.   value,temp: String;
  23. begin
  24.     s2 := TStringStream.Create(chaine);
  25.     de := TBlowFishDecryptStream.Create(cle,s2);
  26.     temp:=de.ReadAnsiString;
  27.     result:=temp;
  28. end;

i use:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Begin
  3. Memo1.Lines.Add('casa');
  4. Memo2.Lines.Add(encrypt('123',Memo1.Text));
  5. Memo2.Lines.SaveToFile('e:\test.txt');
  6. Memo3.Lines.LoadFromFile('e:\test.txt');
  7. Memo4.Lines.Add(decrypt('123',Memo3.Text));    
  8. end;


But times fails, and I get text error.  :( :(
attached image

I love desktop software
https://www.ericksystem.com

ezlage

  • Guest
Re: Encrypt a text (easily)
« Reply #11 on: June 17, 2016, 05:17:09 pm »
Here is the problem:

Code: Pascal  [Select][+][-]
  1. Memo2.Lines.Add(encrypt('123',Memo1.Text));
  2. Memo2.Lines.SaveToFile('e:\test.txt');

Load and save to/from Memo changes the true value. Because of this, I use streams and convert to string when is needed.
« Last Edit: June 17, 2016, 05:25:36 pm by ezlage »

ezlage

  • Guest
Re: Encrypt a text (easily)
« Reply #12 on: June 17, 2016, 05:18:10 pm »
You can encode to base64 before put the string inside a Memo, and decode after, if you really need of Memo fields.
« Last Edit: June 17, 2016, 05:26:33 pm by ezlage »

rvk

  • Hero Member
  • *****
  • Posts: 6802
Re: Encrypt a text (easily)
« Reply #13 on: June 17, 2016, 05:20:46 pm »
Yikes, you're using TMemo as intermediate string-fields.

If you just use string it will work:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Encrypted: string;
  4.   Decrypted: string;
  5. begin
  6.   Memo1.Lines.Add('casa');
  7.   Encrypted := encrypt('123',Memo1.Text);
  8.   Memo2.Lines.Add(Encrypted);
  9.   Decrypted := decrypt('123',Encrypted);
  10.   Memo4.Lines.Add(Decrypted);
  11. end;

If you put encrypted characters (like #0..#32) in a TMemo it will convert to ? which in turn will fail the decryption.

So don't use TMemo as intermediate or put base64 strings in the TMemo like ezlage suggests.

Ericktux

  • Sr. Member
  • ****
  • Posts: 378
    • ericksystem software
Re: Encrypt a text (easily)
« Reply #14 on: June 17, 2016, 05:44:54 pm »
Thanks, but how to save text encrypted as txt, and reopen decrypted  :-\
I love desktop software
https://www.ericksystem.com

 

TinyPortal © 2005-2018