Ways to encrypt string in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By inTech
:warning: Old Version Published before Godot 3 was released.

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()]);
        pass;
        i += 1;
    i = 0;
    return string;

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 .to_ascii() and .to_utf8 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.

func ascii2binary(string):
    var i = 0;
    var textArray = string.to_ascii();
    var stringBinary;
    var fullStringBinary;
    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");
                    else:
                            stringBinary = str(stringBinary) + "0";
                            
                    byteIncre = byteIncre / 2;
                    
            byteIncre = 128;
            fullStringBinary = str(fullStringBinary) + str(stringBinary);
            stringBinary = "";
            binaryAdded = 0;
            i += 1;
            
    i = 0;
    return fullStringBinary;

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.

Cryptography for arrays and strings. by Catprog · Pull Request #5868 · godotengine/godot · GitHub
There is pending PR for encrypting string.
just for your information.

volzhs | 2017-05-17 19:08

Ahh. Thank you for the link. I will be waiting for the PR to be accepted and added to Godot core. Meanwhile I think I managed to create a very simple (XOR) encryption with Godot script already.

inTech | 2017-05-18 09:00

:bust_in_silhouette: Reply From: mollusca

You can convert a String to an array of bytes (RawArray or PoolByteArray) with .to_utf8() or .to_ascii(). The latter assumes the String contains only ASCII characters. You can use the functions get_string_from_utf8() or get_string_from_ascii() in RawArray/PoolByteArray to convert the byte array back to a String.

Thank you. However I tried both methods and the output is ASCII code and not binary code. I manage to create a script to convert ASCII code to binary with the help of .to_ascii function though. Thanks for the help.

inTech | 2017-05-17 11:40