Forum > Beginners
<solved>Easy encryption of text files... Code added 16052024
Sprek Skik:
I Have made a Notebook program. It makes Txt files. But it would be nice to make some sort of encryption. It gives more privacy in my opinion. I have been looking for it sometime, but my attempt to install encrypt has failed. Can someone give me a suggestion?
Added Some Code 16052024
I Made the following Code:
procedure TfrmNoteBewerker.EncryptMemo;
var
i: integer;
Cipher: TDCP_rc4;
begin
CheckEncoding;
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
for i:= 0 to Memo1.Lines.Count-1 do // decrypt the contents of the memo
begin
if Memo1.Lines <> '' then
Memo1.Lines:= Cipher.EncryptString(Memo1.Lines);
end;
Cipher.Burn;
Cipher.Free;
end;
De code is not really working as expected. So on advice from Gemini I made a procedure Check Unicode but it can not compile:
procedure TfrmNoteBewerker.CheckEncoding;
var
MemoEncoding, KeyStrEncoding: TEncoding;
begin
{
// Check Memo1 encoding
MemoEncoding := Memo1.Lines.Strings[0].Encoding; // Get encoding of the first line
// Check KeyStr encoding (assuming it's a string variable)
KeyStrEncoding := KeyStr.Encoding;
if MemoEncoding <> KeyStrEncoding then
begin
// Encodings are different, handle the conversion
WriteLn('Warning: Memo and KeyStr encodings differ!');
// Choose a common encoding (e.g., UTF-8)
TargetEncoding := TEncoding.UTF8;
Memo1.Lines.Strings[0].Encoding := TargetEncoding; // Convert first line encoding
KeyStr := TargetEncoding.GetString(KeyStr); // Convert KeyStr encoding
end;
}
end;
Handoko:
You can use DCPcrypt:
https://wiki.freepascal.org/DCPcrypt
Or maybe write your own, try this:
https://forum.lazarus.freepascal.org/index.php/topic,33013.msg213188.html#msg213188
KodeZwerg:
--- Quote from: Sprek Skik on May 14, 2024, 08:28:01 am ---my attempt to install encrypt has failed
--- End quote ---
I am unsure what that means but use what Lazarus brings at installation, compression! Its a different kind of encryption with advantage to be smaller than the input was, but that depend on input.
Next mostest easiest "encryption" = Base-Encoded, that format is often used in telecommunication to transfer binary files to ascii-encrypted.
You can also combine one with another, or simply open a browser, there are many basics about encryption explained via wiki.
Thaddy:
Oh, well,
Having some fun with randoms looks always good to me:
This code makes use of the predictability and repeatability of a PseudoRandom Number Generator. Actually any PRNG, but you do not want to ruin the system.randseed, so I use a separate random. Principle is the same:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program cryptrandom;{$mode objfpc}{$modeswitch typehelpers}{$H+}{ This is demo that you can actually use random to encrypt/decrypt. This is due to the nature of a Pseudo Random Number Generator, where given a certain random seed, the sequence is predictable. Here I use the Random Seed as a key to encrypt/decrypt a string. The same code would also work with the Default random from FreePascal, but it is better not to touch system.randseed, So I used a different Random. Have fun, Thaddy}uses { The unit is based on my https://wiki.freepascal.org/Delphi_compatible_LCG_Random but I adapted it, so will add it below}lcg_random2; function Crypt(const value:AnsiString;const Key:Cardinal):AnsiString;var i:integer;begin RandSd := Key; SetLength(Result,Length(Value)); for i := 1 to length(Value) do Result[i] := Chr(Ord(Value[i]) xor lcgrandom(255));end; var a:AnsiString;begin a:='This is the text to encrypt/decrypt.'; writeln(a); writeln('Encrypting..'); a:=Crypt(a, 12345); writeln(a); writeln('Decrypting..'); a:=Crypt(a, 12345); writeln(a);end.Of course this is not hack safe, but:
- You have to know the Random generator that is used
- You have to know the Key. The Key is a numeric value in the range of cardinal.
- It is way better than XOR!
- It is also much simpler than XOR!, although it uses XOR the key is derived from the randseed.
Have fun!
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// adapted version of my wiki entry with RandSeed factored outunit lcg_random2;// Delphi compatible LCG random number generator routines for Freepascal.// (c)2017, Thaddy de Koning. Use as you like// Algorithm, Delphi multiplier and increment taken from:// https://en.wikipedia.org/wiki/Linear_congruential_generator// The default Delphi RandomSeed is determined as zero.{$ifdef fpc}{$mode objfpc}{$endif}{$J+} interfaceconst RandSd:cardinal = 0; function LCGRandom: extended; overload;inline;function LCGRandom(const range:longint):longint;overload;inline; implementation function IM:cardinal;inline;begin RandSd := RandSd * 134775813 + 1; Result := RandSd;end; function LCGRandom: extended; overload;inline;begin Result := IM * 2.32830643653870e-10;end; function LCGRandom(const range:longint):longint;overload;inline;begin Result := IM * range shr 32;end; end.
[edit]
I added this code to the wiki too, since I think it is a good example for PRNG behavior.
Thaddy:
Technical detail:
Periodicity for this particular random encryption is 2^32 and is not vulnarable to frequency analysis, unlike xor.
Frequency analysis is also fun. Will write a simple example for that too:
Basically any natural language has a bias to certain characters. If you know that bias you can easily calculate the mean and the standard deviation to determine the likelyhood what a certain encrypted character actually is most likely. This works pretty good! And simple to do.
You can make it pretty much unbreakable with cycles:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Crypt(const value:AnsiString;const Key:Cardinal;cycles:integer = 100):AnsiString;var c,i:integer;begin RandSd := Key; SetLength(Result,Length(Value)); for c:= 1 to cycles do begin for i := 1 to length(Value) do Result[i] := Chr(Ord(Value[i]) xor lcgrandom(255)); end;end;
I just love PRNG's, as most people know.
[edit] chatGPT is working on the above, but as yet no result for weakness....
Navigation
[0] Message Index
[#] Next page