Forum > Beginners

<solved>Easy encryption of text files... Code added 16052024

<< < (2/8) > >>

Thanks Thaddy, very 'nifty' piece of code, Me Likey  8-)
Regards Benny

I've seen professional products using XOR (encoded data is in 100Mb to 200Mb size-range, and need to be processed in real-time on a 266MHz ARM device on Windows CE):
- Key (XOR table) is 4kb in size (2D array)
- after decoding the first 4kb of data, for the next 4kb of data to be decrypted you need to use offset on the XOR key table (use 2nd array row as first, use first row at the end). Every next 4kb data block needs another offset shift.

Another encryption where I have had fun "learning it", was also XOR, but no key was used except for one byte. Encryption was based on the character position in the stream and on the value of the previous character.
At decrypting - if you do it wrong for one single char - the rest of the sequence will be wrong.
Does not work well for variable-length encoding without extra code, but works fine for ASCII.

Personally, I also use XOR with very long keys - good luck with frequency analysis here.
Note to the other developers - do not keep the keys in plain form in your binaries (hint: Xing and DeCSS)

XOR alone is not very good with any key length, but my example shows you can improve on it.
That is basically what the new FPC generator does, but instead of Xoshiro128** - only in trunk- which is of the xorshift family of prng's I use a simpler prng.
This is over-simplified. If you know the prng it is easy to hack, even with multiple rounds, but not by the average 14 year old kid  :D (who can hack XOR!)
The reason why I showed these examples is also to show PRNG's are predictable if the seed is known, so I used the seed as a key. If you don't know the key?, good luck ltitle 14 year old... If you don't know the PRNG? just as well. This scheme is only hackable by pro's, but it is hackable. Needs a lot of processing power, though.
But it is mathematically better than simple xor with any key length.
(And to show Pascal Random can be used for encryption and decription with the same function)

Hey, I just wrote it for fun and to explain something about one of my favorite passtimes: PRNG's!
PRNG's are very important for statistics. BTW the same code works with Freepascal's PRNG's, both the mersenne twister in 2.0 to 3.2.2 or the new one.
Demo screws up the rand seed, so if you rely on random, call randomize. 8-) But then you need to hack it... cause calling randomize screws up predicatbility.. can you still follow me ?  ;D ;)

--- 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 cryptrandomwithdefaults;{ using FPC's default random }{$mode objfpc}{$modeswitch typehelpers}{$H+} function Crypt(const value:AnsiString;Key:Cardinal):AnsiString;var  c,i:integer;begin  RandSeed := Key;  SetLength(Result,Length(Value));  for c:= 1 to random(255) do  begin    for i := 1 to length(Value) do      Result[i] := Chr(Ord(Value[i]) xor random(255));  end;end; var  a:AnsiString;begin a:='This is the text to encrypt/decrypt.'; writeln(a); writeln('Encrypting..'); a:=Crypt(a,54321); writeln(a); writeln('Decrypting..'); a:=Crypt(a, 54321);  writeln(a);end.Don't do that in your own code, because system.randseed is reset...
This will work with 3.2.2, the mersenne twister, ot trunk, with xorhiro128**

I hope, more in general, that the core devlopers really understand that changing the prng is not really smart, because code encrypted like this is not compatible between 3.2.2 and trunk.
This only worls - my first example - because I purposely introduced a separate PRNG.

I fully understand your point. I just wanted to say that "XORing" isn't really useless.

I do not know much about generating random numbers. The last article I've read on that was about Pentium IV's integrated random number generator, who was generating numbers according to the temperature fluctuations inside the CPU. I really do not know much about this topic.


[0] Message Index

[#] Next page

[*] Previous page

Go to full version