Forum > Beginners
<solved>Easy encryption of text files... Code added 16052024
Thaddy:
And just that is the difference between a PRNG and true random. In the case of true random my code would not work.
Thaddy:
BTW I would not use the last example myself: as I wrote before, but implicitly, if you use the default Random, basically the crypt code claims the random generator all for itself. If you use the random generator elsewhere in your code there can be unwanted side effects. So I have to protect you.
One of the reasons I used a custom random in the first place, but to make the code really safe, the random generator needs to be inaccessible to other code.
You also can not use class methods for this. Although I demonstrated the priniciple, which is correct, to actually use it we need a stronger separation from other code.
As usual, I dreamt, literally...
Will fix the flaw. It will be either an advanced record or a class.
Khrys:
--- Quote ---XOR alone is not very good with any key length
--- End quote ---
If the key is both longer than the data and truly random, then XOR alone is indeed enough to make the encryption unbreakable, as it forms a one-time pad (OTP): https://en.wikipedia.org/wiki/One-time_pad
KodeZwerg:
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 ....
Fibonacci:
Just uploaded ChaCha20 implementation for FPC, super simple to use:
https://github.com/fibodevy/fpc-chacha20/blob/main/chacha20.pas
--- 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 example; uses chacha20; var s: string; cc: chacha20state; begin // key should be 32 bytes, nonce 12 bytes chacha20_init(cc, 'key', 'nonce'); s := 'hello'; s := copy(s, 1, 999); // make string memory writable // encrypt chacha20_xor(cc, @s[1], length(s)); writeln('encrypted = ', s); // decrypt chacha20_set_counter(cc, 0); chacha20_xor(cc, @s[1], length(s)); writeln('decrypted = ', s); readln;end.
Compatible with this PHP implementation to use on server side: https://github.com/lt/PHP-ChaCha20
Navigation
[0] Message Index
[#] Next page
[*] Previous page