Hello everyone. I spent one day looking for ways to encrypt (not hash) string in Godot. I couldn't find any build in functions or open-source script that does this. I tried writing my own ones (converting from other language to GDScript) but non of it is working.
This main reason why I want to encrypt string in Godot is because that I want to send POST HTTP request to a web server (which will most probably run on PHP). I do not want users to understand the header or anything so as to prevent cheating. I will be giving users a new key everytime so that the raw header will always change. To add another layer of defense, I'd like to encrypt those data and decrypt on my PHP side.
I read about encryption ciphers and feel that the XOR cipher is the simplest (not best) and hence best for beginners like me. So I Googled for other language (PHP) based code on XOR cipher and decided to convert it to GDScript. Below is my initial script which does not work.
func xor(string, key):
while (i < string.length()):
string[i] = (string[i] ^ key[i % key.length()]);
i += 1;
i = 0;
The above code produced some an error ("Invalid operands 'String' and 'String' in operator '^'."). I checked the Godot documentation for '^' and there is this bitwise character so it shouldn't be the cause. After much checking and tracing, I find out the string must be converted to binary before it can work with the biswise operator.
Upon checking the documentation for conversion, I only find ways to convert HEX/ASCII to String but not the other way round. So I am thinking to create a list and compare each character and set it to their corresponding binary code then do a manual XOR checking.
I am just wondering if there is any other workarounds or am I missing any build in function? Or maybe my syntax for "string[i] ^ key[i % key.length()]);" is wrong?
I see that Godot have a method for the File class that encrypt files with password. But what I am looking for is to encrypt string - and also a way to decrypt it from the server-side (I don't know how Godot encryption work for the file class).
Any help or pointing me to the right direction will be greatly appreciated.
Edit: Tried using .toascii() and .toutf8 and both give me the ASCII code. I couldn't find any other ways to convert string or ASCII to binary. I tried var2byte but the first three elements always return the same result - the fourth one is the ASCII code. So I wrote a little script myself to convert ASCII to binary.
var i = 0;
var textArray = string.to_ascii();
var binaryAdded = 0;
while (i < textArray.size()):
while (byteIncre > 0):
if ((textArray[i] - binaryAdded >= byteIncre)):
binaryAdded += byteIncre;
stringBinary = str(stringBinary) + "1";
print(str(byteIncre) + ": true");
stringBinary = str(stringBinary) + "0";
byteIncre = byteIncre / 2;
byteIncre = 128;
fullStringBinary = str(fullStringBinary) + str(stringBinary);
stringBinary = "";
binaryAdded = 0;
i += 1;
i = 0;
With that I guess all I need to do is to compare the binary like how the XOR works and then I will have the XOR encryption script ready.