Recent

Author Topic: <solved>Easy encryption of text files... Code added 16052024  (Read 3699 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Easy encryption of text files... Help/Info needed
« Reply #15 on: May 15, 2024, 06:07:11 pm »
Interesting way of doing @Thaddy
IKR that what I now suggest would be against your code but I'll suggest to make it a class, internal work with TStream, at end or start add a fixed size hash generated from password/passkey so the class could quick abort decryption or output some other kind of data ....
I made it a advanced record, but I want to reduce (without speed loss ) the code a little.
Here's what I have now:
Code: Pascal  [Select][+][-]
  1. unit cryptrandom;
  2.  
  3. {$mode objfpc}{$modeswitch advancedrecords}
  4. {$warn 5024 off}
  5. interface
  6.  
  7. type
  8. {$if not declared(Tbytes) It may be declared in another used unit }
  9.    TBytes = Array of byte;
  10. {$ifend}
  11.  
  12.   TRandomCrypt = record
  13.   strict private
  14.     { Maintains state }
  15.     FRandSeed:Cardinal;
  16.     function IM:Cardinal;inline;
  17.   public
  18.     { ShortString encryption }
  19.     function Crypt(const Value:ShortString; Key:Cardinal;Cycles:Cardinal = 1):ShortString;inline;
  20.     { AnsiString encryption }
  21.     function Crypt(const Value:AnsiString; Key:Cardinal;Cycles:Cardinal = 1):AnsiString;inline;
  22.     { UTF8String encryption }
  23.     function Crypt(const Value:UTF8String; Key:Cardinal;Cycles:Cardinal = 1):UTF8String;inline;
  24.     { UnicodeString encryption }
  25.     function Crypt(const Value:UnicodeString; Key:Cardinal;Cycles:Cardinal = 1):UnicodeString;inline;
  26.     { RawByteString, preserves codepage }
  27.     function Crypt(const Value:RawByteString; Key:Cardinal;Cycles:Cardinal = 1):RawByteString;
  28.     { Binary encryption }
  29.     function Crypt(const Value:TBytes; Key:Cardinal;Cycles:Cardinal = 1):TBytes;inline;
  30.   end;
  31.  
  32. implementation
  33.  
  34.   function TRandomCrypt.IM:Cardinal;inline;
  35.   begin
  36.     FRandSeed := FRandSeed * 134775813  + 1;
  37.     Result := FRandSeed;
  38.   end;
  39.  
  40.   function TRandomCrypt.Crypt(const Value:ShortString; Key:Cardinal;Cycles:Cardinal = 1):ShortString;inline;
  41.   var
  42.     i,j:integer;
  43.   begin
  44.     Result :='';
  45.     FRandSeed:= Key;
  46.     Setlength(Result, Length(Value));
  47.     For i:= 1 to Cycles do
  48.       for j:= 1 to length(Value) do
  49.         Result[j] := chr(Ord(Value[j]) xor (IM * Key shr 32));
  50.   end;
  51.  
  52.   function TRandomCrypt.Crypt(const Value:AnsiString; Key:Cardinal;Cycles:Cardinal = 1):AnsiString;inline;
  53.   var
  54.     i,j:integer;
  55.   begin
  56.     Result :='';
  57.     FRandSeed:= Key;
  58.     Setlength(Result, Length(Value));
  59.     For i:= 1 to Cycles do
  60.       for j:= 1 to length(Value) do
  61.         Result[j] := chr(Ord(Value[j]) xor (IM * Key shr 32));
  62.   end;
  63.  
  64.   function TRandomCrypt.Crypt(const Value:UTF8String; Key:Cardinal;Cycles:Cardinal = 1):UTF8String;
  65.   var
  66.     i,j:integer;
  67.   begin
  68.     Result :='';
  69.     FRandSeed:= Key;
  70.     Setlength(Result, Length(Value));
  71.     For i:= 1 to Cycles do
  72.       for j:= 1 to length(Value) do
  73.         Result[j] := chr(Ord(AnsiChar(Value[j])) xor (IM * Key shr 32));
  74.   end;
  75.  
  76.   function TRandomCrypt.Crypt(const Value:UnicodeString; Key:Cardinal;Cycles:Cardinal = 1):UnicodeString;
  77.   var
  78.     i,j:integer;
  79.   begin
  80.   {$ifdef unix}
  81.   {$hint Not all Nixes always play nice with UTF16, use UTF8String instead. Crypt is OK, though }
  82.   {$endif}
  83.     Result :='';
  84.     FRandSeed:= Key;
  85.     Setlength(Result, Length(Value));
  86.     For i:= 1 to Cycles do
  87.       for j:= 1 to length(Value) do
  88.         Result[j] := chr(Ord(AnsiChar(Value[j])) xor (IM * Key shr 32));
  89.   end;
  90.  
  91.   function TRandomCrypt.Crypt(const Value:RawByteString; Key:Cardinal;Cycles:Cardinal = 1):RawByteString;
  92.   var
  93.     i,j:integer;
  94.   begin
  95.     Setlength(Result,Length(Value));
  96.     FRandSeed:= Key;
  97.     Setlength(Result, Length(Value));
  98.     For i:= 1 to Cycles do
  99.       for j:= 1 to length(Value) do
  100.         Result[j] := chr(Ord(AnsiChar(Value[j])) xor (IM * Key shr 32));
  101.   end;
  102.  
  103.   function TRandomCrypt.Crypt(const Value:TBytes; Key:Cardinal;Cycles:Cardinal = 1):TBytes;inline;
  104.   var
  105.     i,j:integer;
  106.   begin
  107.     Setlength(Result,Length(Value));
  108.     FRandSeed:= Key;
  109.     Setlength(Result, Length(Value));
  110.     For i:= 1 to Cycles do
  111.       for j:= 0 to high(Value) do
  112.         Result[j] := Value[j] xor (IM * Key shr 32);
  113.   end;
  114.  
  115. end.
example to play with:
Code: Pascal  [Select][+][-]
  1. { using a localized random based on Delphi's LCG, but you can use any PRNG }
  2. {$mode objfpc}{$modeswitch advancedrecords}
  3. {$codepage UTF8}// so we can use Unicode literal strings for testing}
  4. uses
  5.   CryptRandom;
  6.  
  7. var
  8. { change string type as required. All encrypt good,
  9.   but presentation may be confusing}
  10.  a:UnicodeString;
  11.  Crypt:TRandomCrypt;
  12.  Key:Cardinal = 12345;
  13. begin
  14.  Crypt := Default(TRandomCrypt);
  15.  a:='This is thè téxt to êncrypt/decrypt Ä.'; // based on nothing at all...
  16.  writeln(a);
  17.  writeln('Encrypting..');
  18.  a:=Crypt.Crypt(a,Key, 100);
  19.  writeln(a);
  20.  writeln('Decrypting..');
  21.  a:=Crypt.Crypt(a, Key, 100);
  22.  writeln(a);
  23. end.
I am pretty sure I will get comments about this code from people who do not know what they are talking about.
I am also pretty sure I get comments from the dick heads like me...(which are sane except not for other people)
Meaning, yes, there is repeating code that should be factored out. So don't comment on that.

This code works on most string types and binary data and is much better than simply xor.

If Key and cycles do not match, you will get garbage.
This one is pretty safe, but not cryptographically secure, (But close)

You can plug-in any PRNG you like based on this code (which uses Delphi's PRNG)
The random is now locallized to the record scope and can't interfear with the normal random.

« Last Edit: May 16, 2024, 10:21:19 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 279
  • From C# to Lazarus
Re: Easy encryption of text files... Help/Info needed
« Reply #16 on: May 15, 2024, 06:38:29 pm »
Hi,
@Sprek Skik

I use this:

Package: DCPCrypt

Code: Pascal  [Select][+][-]
  1. use
  2.         Classes, SysUtils, Dialogs,
  3.         DCPrc6, DCPsha512, base64,
  4.         zstream;  
  5.  
  6. // ************************
  7. // ***** Crypt String *****
  8. // ************************
  9. function Encrypt(SInput, SKey: String): String;
  10. var
  11.         DecodedStream: TStringStream;
  12.         EncodedStream: TStringStream;
  13.         Str: String;
  14.         Cipher: TDCP_rc6;
  15. begin
  16.         try
  17.                 DecodedStream := TStringStream.Create(SInput);
  18.                 EncodedStream := TStringStream.Create('');
  19.  
  20.                 Cipher:= TDCP_rc6.Create(nil);
  21.                 Cipher.InitStr(SKey,TDCP_sha512);              // initialize the cipher with a hash of the passphrase
  22.                 Cipher.EncryptStream(DecodedStream,EncodedStream,DecodedStream.Size); // encrypt the contents of the file
  23.  
  24.                 Str := EncodeStringBase64(EncodedStream.DataString); // Convert to Base64
  25.  
  26.                 Cipher.Burn;
  27.                 Cipher.Free;
  28.                 DecodedStream.Free;
  29.                 EncodedStream.Free;
  30.                 exit(Str);
  31.         except
  32.                         exit('');
  33.         end;
  34.         exit('');
  35. end;
  36.  
  37. // **************************
  38. // ***** Decrypt String *****
  39. // **************************
  40. function Decrypt(Sinput, SKey: String): String;
  41. var
  42.         DecodedStream: TStringStream;
  43.         EncodedStream: TStringStream;
  44.         Str: String;
  45.         Cipher: TDCP_rc6;
  46. begin
  47.         try
  48.                 EncodedStream := TStringStream.Create(DecodeStringBase64(SInput)); // Base 64 Decode the Data
  49.                 DecodedStream := TStringStream.Create('');
  50.  
  51.                 Cipher:= TDCP_rc6.Create(nil);
  52.                 Cipher.InitStr(SKey,TDCP_sha512);              // initialize the cipher with a hash of the passphrase
  53.                 Cipher.DecryptStream(EncodedStream,DecodedStream,EncodedStream.Size); // decrypt the contents of the file
  54.  
  55.                 Str := DecodedStream.DataString;
  56.                 Cipher.Burn;
  57.                 Cipher.Free;
  58.                 EncodedStream.Free;
  59.                 DecodedStream.Free;
  60.                 exit(Str)
  61.         except
  62.                 exit('');
  63.         end;
  64.         exit('');
  65. end;
  66.  
  67. // ***************************
  68. // ***** Compress String *****
  69. // ***************************
  70. function Compress(SInput: String): String;
  71. var
  72.         Compressor: TCompressionStream;
  73.         strInput, strOutput: TStringStream;
  74.         Str: String;
  75. begin
  76.  
  77.         strInput := TStringStream.Create(SInput);
  78.         strOutput := TStringStream.Create('');
  79.  
  80.         try
  81.                 Compressor := TCompressionStream.Create(clDefault, strOutput);
  82.  
  83.                 Compressor.CopyFrom(strInput, strInput.Size);
  84.                 Compressor.Free;
  85.                 Str := strOutput.DataString;
  86.  
  87.                 strOutput.Free;
  88.                 strInput.Free;
  89.  
  90.                 exit(Str);
  91.         except
  92.                 exit('');
  93.         end;
  94.         exit('');
  95. end;
  96.  
  97. // *****************************
  98. // ***** Decompress String *****
  99. // *****************************
  100. function Decompress(Sinput: String): String;
  101. var
  102.         Decompressor: TDecompressionStream;
  103.         strInput, strOutput: TStringStream;
  104.         Str: String;
  105. begin
  106.         strInput := TStringStream.Create(Sinput);
  107.         strOutput := TStringStream.Create('');
  108.  
  109.         try
  110.                 Decompressor := TDecompressionStream.Create(strInput);
  111.  
  112.                 strOutput.Position := 0;
  113.                 strOutput.Size := 0;
  114.  
  115.                 strOutput.CopyFrom(Decompressor, 0);
  116.                 Decompressor.Free;
  117.                 Str := strOutput.DataString;
  118.  
  119.                 strOutput.Free;
  120.                 strInput.Free;
  121.  
  122.                 exit(Str);
  123.         except
  124.                 exit('');
  125.         end;
  126.         exit('');
  127. end;                                            
  128.  

B->
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Easy encryption of text files... Help/Info needed
« Reply #17 on: May 15, 2024, 06:48:38 pm »
That is not the issue at hand: the issue is proving random can be used to encrypt and decrypt.
To quote myself:
Quote
I am pretty sure I will get comments about this code from people who do not know what they are talking about.
I am sure your intentions are good.

« Last Edit: May 15, 2024, 06:53:52 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Easy encryption of text files... Help/Info needed
« Reply #18 on: May 15, 2024, 07:23:44 pm »
the issue is
That nobody offered a solution for what the OP is asking for, de-/encrypt text files.
However cool the crypt logic might be, no shown method as of right now do that.
TBytes is at least a possibility to work with files when for that purpose a wrapper would be added.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fibonacci

  • Sr. Member
  • ****
  • Posts: 439
Re: Easy encryption of text files... Help/Info needed
« Reply #19 on: May 15, 2024, 07:45:12 pm »
the issue is
That nobody offered a solution for what the OP is asking for, de-/encrypt text files.

Code: Pascal  [Select][+][-]
  1. uses chacha20, Classes;
  2.  
  3. var
  4.   cc: chacha20state;
  5.   f: TFileStream;
  6.   n: integer;
  7.   buff: array[0..(1024*64)-1] of byte;
  8.  
  9. begin
  10.   // ---- encrypt ----
  11.  
  12.   chacha20_init(cc, 'key', 'nonce');
  13.  
  14.   f := TFileStream.Create('file.txt', fmOpenReadWrite);
  15.  
  16.   while f.Position < f.Size do begin
  17.     // read chunk
  18.     n := f.Read(buff, length(buff));
  19.     // encrypt or decrypt
  20.     chacha20_xor(cc, @buff[0], n);
  21.     // save
  22.     f.Position := f.Position-n;
  23.     f.Write(buff, n);
  24.   end;
  25.  
  26.   f.Free;
  27.  
  28.   writeln('file encrypted, <enter> to decrypt');
  29.   readln;
  30.  
  31.   // ---- decrypt ----
  32.  
  33.   chacha20_init(cc, 'key', 'nonce');
  34.  
  35.   f := TFileStream.Create('file.txt', fmOpenReadWrite);
  36.  
  37.   while f.Position < f.Size do begin
  38.     // read chunk
  39.     n := f.Read(buff, length(buff));
  40.     // encrypt or decrypt
  41.     chacha20_xor(cc, @buff[0], n);
  42.     // save
  43.     f.Position := f.Position-n;
  44.     f.Write(buff, n);
  45.   end;
  46.  
  47.   f.Free;
  48.  
  49.   writeln('file decrypted');
  50.   readln;
  51. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Easy encryption of text files... Help/Info needed
« Reply #20 on: May 15, 2024, 08:15:20 pm »
That is again beyond the scope .
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: Easy encryption of text files... Help/Info needed
« Reply #21 on: May 16, 2024, 12:44:19 pm »
Hi,
@Sprek Skik

I use this:

Package: DCPCrypt

Code: Pascal  [Select][+][-]
  1. use
  2.         Classes, SysUtils, Dialogs,
  3.         DCPrc6, DCPsha512, base64,
  4.         zstream;  
  5.  
  6. // ************************
  7. // ***** Crypt String *****
  8. // ************************
  9. function Encrypt(SInput, SKey: String): String;
  10. var
  11.         DecodedStream: TStringStream;
  12.         EncodedStream: TStringStream;
  13.         Str: String;
  14.         Cipher: TDCP_rc6;
  15. begin
  16.         try
  17.                 DecodedStream := TStringStream.Create(SInput);
  18.                 EncodedStream := TStringStream.Create('');
  19.  
  20.                 Cipher:= TDCP_rc6.Create(nil);
  21.                 Cipher.InitStr(SKey,TDCP_sha512);              // initialize the cipher with a hash of the passphrase
  22.                 Cipher.EncryptStream(DecodedStream,EncodedStream,DecodedStream.Size); // encrypt the contents of the file
  23.  
  24.                 Str := EncodeStringBase64(EncodedStream.DataString); // Convert to Base64
  25.  
  26.                 Cipher.Burn;
  27.                 Cipher.Free;
  28.                 DecodedStream.Free;
  29.                 EncodedStream.Free;
  30.                 exit(Str);
  31.         except
  32.                         exit('');
  33.         end;
  34.         exit('');
  35. end;
  36.  
  37. // **************************
  38. // ***** Decrypt String *****
  39. // **************************
  40. function Decrypt(Sinput, SKey: String): String;
  41. var
  42.         DecodedStream: TStringStream;
  43.         EncodedStream: TStringStream;
  44.         Str: String;
  45.         Cipher: TDCP_rc6;
  46. begin
  47.         try
  48.                 EncodedStream := TStringStream.Create(DecodeStringBase64(SInput)); // Base 64 Decode the Data
  49.                 DecodedStream := TStringStream.Create('');
  50.  
  51.                 Cipher:= TDCP_rc6.Create(nil);
  52.                 Cipher.InitStr(SKey,TDCP_sha512);              // initialize the cipher with a hash of the passphrase
  53.                 Cipher.DecryptStream(EncodedStream,DecodedStream,EncodedStream.Size); // decrypt the contents of the file
  54.  
  55.                 Str := DecodedStream.DataString;
  56.                 Cipher.Burn;
  57.                 Cipher.Free;
  58.                 EncodedStream.Free;
  59.                 DecodedStream.Free;
  60.                 exit(Str)
  61.         except
  62.                 exit('');
  63.         end;
  64.         exit('');
  65. end;
  66.  
  67. // ***************************
  68. // ***** Compress String *****
  69. // ***************************
  70. function Compress(SInput: String): String;
  71. var
  72.         Compressor: TCompressionStream;
  73.         strInput, strOutput: TStringStream;
  74.         Str: String;
  75. begin
  76.  
  77.         strInput := TStringStream.Create(SInput);
  78.         strOutput := TStringStream.Create('');
  79.  
  80.         try
  81.                 Compressor := TCompressionStream.Create(clDefault, strOutput);
  82.  
  83.                 Compressor.CopyFrom(strInput, strInput.Size);
  84.                 Compressor.Free;
  85.                 Str := strOutput.DataString;
  86.  
  87.                 strOutput.Free;
  88.                 strInput.Free;
  89.  
  90.                 exit(Str);
  91.         except
  92.                 exit('');
  93.         end;
  94.         exit('');
  95. end;
  96.  
  97. // *****************************
  98. // ***** Decompress String *****
  99. // *****************************
  100. function Decompress(Sinput: String): String;
  101. var
  102.         Decompressor: TDecompressionStream;
  103.         strInput, strOutput: TStringStream;
  104.         Str: String;
  105. begin
  106.         strInput := TStringStream.Create(Sinput);
  107.         strOutput := TStringStream.Create('');
  108.  
  109.         try
  110.                 Decompressor := TDecompressionStream.Create(strInput);
  111.  
  112.                 strOutput.Position := 0;
  113.                 strOutput.Size := 0;
  114.  
  115.                 strOutput.CopyFrom(Decompressor, 0);
  116.                 Decompressor.Free;
  117.                 Str := strOutput.DataString;
  118.  
  119.                 strOutput.Free;
  120.                 strInput.Free;
  121.  
  122.                 exit(Str);
  123.         except
  124.                 exit('');
  125.         end;
  126.         exit('');
  127. end;                                            
  128.  

B->

Looking at the code I don't know how to implement it to my code. See my added code in the original message.
thnx for the response

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Easy encryption of text files... Help/Info needed
« Reply #22 on: May 16, 2024, 01:27:08 pm »
Looking at the code I don't know how to implement it to my code. See my added code in the original message.
thnx for the response
First your should fix the encryption part, aslong that is wrong, decrypt will always fail.
Your code:
Code: Pascal  [Select][+][-]
  1.   for i:= 0 to Memo1.Lines.Count-1 do       // decrypt the contents of the memo
  2.     begin
  3.       if Memo1.Lines <> '' then
  4.         Memo1.Lines:= Cipher.EncryptString(Memo1.Lines);
  5.     end;
Fixed:
Code: Pascal  [Select][+][-]
  1. Cipher.EncryptString(Memo1.Text);
I have not looked in detail but that is a main error. Hope it helps!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Dzandaa

  • Sr. Member
  • ****
  • Posts: 279
  • From C# to Lazarus
Re: Easy encryption of text files... Code added 16052024
« Reply #23 on: May 16, 2024, 03:25:59 pm »
Hi,
@Sprek Skik

Try This:

Code: Pascal  [Select][+][-]
  1. var Str, MyKey: String;
  2. ...
  3.  MyKey := 'TestKey';
  4. // Encrypt
  5.  Str := Memo1.Text;
  6.  Str := Encrypt(Compress(Str), MyKey);  
  7.  Memo1.Clear;
  8.  Memo1.Append(Str);
  9.  
  10. // Decrypt
  11.  Str := Memo1.Text;
  12.  Str := Decompress(Decrypt(Str, MyKey));
  13.  Memo1.Clear;
  14.  Memo1.Append(Str);
  15.  

B->
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Easy encryption of text files... Code added 16052024
« Reply #24 on: May 16, 2024, 03:29:27 pm »
In my examle code it would be simply:
Code: Pascal  [Select][+][-]
  1.   Memo1.Iines.Text := Crypt.Crypt(Memo1.Lines.Text, Key);
To decrypt it, You call the same method.
It is also stronger encryped than e.g. RC4 or DES3. Especially if you use cycles.
Code: Pascal  [Select][+][-]
  1.   Memo1.Iines.Text := Crypt.Crypt(Memo1.Lines.Text, Key,10);
This is a known property of PRNG's
« Last Edit: May 16, 2024, 03:34:42 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 279
  • From C# to Lazarus
Re: Easy encryption of text files... Code added 16052024
« Reply #25 on: May 16, 2024, 03:34:29 pm »
Hi,
Compress and Decompress are optional, just for add complexity to the cryptage :)

You can also use:

Code: Pascal  [Select][+][-]
  1.  Memo1.Iines.Text := Encrypt(Memo1.Lines.Text, MyKey);
  2.  Memo1.Iines.Text := Decrypt(Memo1.Lines.Text, MyKey);
  3.  

B->
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Easy encryption of text files... Code added 16052024
« Reply #26 on: May 16, 2024, 03:36:32 pm »
With my code you can use:
Code: Pascal  [Select][+][-]
  1. {$macro on}{$define encode := crypt.crypt}{$define decode:= crypt.crypt}
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: Easy encryption of text files... Help/Info needed
« Reply #27 on: May 16, 2024, 06:35:49 pm »
Looking at the code I don't know how to implement it to my code. See my added code in the original message.
thnx for the response
First your should fix the encryption part, aslong that is wrong, decrypt will always fail.
Your code:
Code: Pascal  [Select][+][-]
  1.   for i:= 0 to Memo1.Lines.Count-1 do       // decrypt the contents of the memo
  2.     begin
  3.       if Memo1.Lines <> '' then
  4.         Memo1.Lines:= Cipher.EncryptString(Memo1.Lines);
  5.     end;
Fixed:
Code: Pascal  [Select][+][-]
  1. Cipher.EncryptString(Memo1.Text);
I have not looked in detail but that is a main error. Hope it helps!

Something was wrong so I will try it...... It was example code that I tried to fix, but everything is new for me.
thnx for the info

Sprek Skik

  • New Member
  • *
  • Posts: 21
  • Beginner Lazarus & Free Pascal
Re: Easy encryption of text files... Help/Info needed
« Reply #28 on: May 16, 2024, 07:11:14 pm »
Looking at the code I don't know how to implement it to my code. See my added code in the original message.
thnx for the response
First your should fix the encryption part, aslong that is wrong, decrypt will always fail.
Your code:
Code: Pascal  [Select][+][-]
  1.   for i:= 0 to Memo1.Lines.Count-1 do       // decrypt the contents of the memo
  2.     begin
  3.       if Memo1.Lines <> '' then
  4.         Memo1.Lines:= Cipher.EncryptString(Memo1.Lines);
  5.     end;

Fixed:
Code: Pascal  [Select][+][-]
  1. Cipher.EncryptString(Memo1.Text);
I have not looked in detail but that is a main error. Hope it helps!

I have tried your code but there is no encryption at all, but also no problems ;-)

Fibonacci

  • Sr. Member
  • ****
  • Posts: 439
Re: Easy encryption of text files... Code added 16052024
« Reply #29 on: May 16, 2024, 07:30:03 pm »
Code: Pascal  [Select][+][-]
  1. uses chacha20, base64, md5;

ChaCha20.pas here

Code: Pascal  [Select][+][-]
  1. function encrypt(data, password: string): string;
  2. var
  3.   key: string;
  4.   cc: chacha20state;
  5. begin
  6.   // md5 hash of password as key
  7.   key := MD5Print(MD5String(password));
  8.   // encrypt with ChaCha20
  9.   chacha20_init(cc, key, ''{empty nonce});
  10.   chacha20_xor(cc, @data[1], length(data));
  11.   // return base64 encoded
  12.   result := EncodeStringBase64(data);
  13. end;
  14.  
  15. function decrypt(data, password: string): string;
  16. var
  17.   key: string;
  18.   cc: chacha20state;
  19. begin
  20.   // decode base64
  21.   data := DecodeStringBase64(data);
  22.   // md5 hash of password as key
  23.   key := MD5Print(MD5String(password));
  24.   // decrypt with ChaCha20
  25.   chacha20_init(cc, key, ''{empty nonce});
  26.   chacha20_xor(cc, @data[1], length(data));
  27.   // return decrypted
  28.   result := data;
  29. end;
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33.   Memo1.Text := encrypt(Memo1.Text, 'password');
  34. end;
  35.  
  36. procedure TForm1.Button2Click(Sender: TObject);
  37. begin
  38.   Memo1.Text := decrypt(Memo1.Text, 'password');
  39. end;

 

TinyPortal © 2005-2018