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
(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.
But then you need to hack it... cause calling randomize screws up predicatbility.. can you still follow me ?
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**