How would I make my own encryption algorithm?

I’ve seen few developers creating their own encryption algorithm in their game. I was quite curious about how to make them. Any thoughts or theories? Examples are also welcome.

2 Likes

Encryption is mainly used on Data, and I would suppose people would go through the lengths to send encrypted data through remote events. If you’re receiving on the server end, you must also be able to decrpyt. I would presume it all has to do with sub-siding chracters with others and lots of string formatting, for now all I tell you is that you definetly need a charset correspending to each letter in the alphabet that you’d replace.

Another thing you could do is use HTTPService:GenerateGUID(), the function returns a unique identifier and this could come in handy if you’re not receiving anything on other ends.

1 Like
local Charset = {
    §, ^, +, *,
}

local Alphabet = {
    a, b, c , d
}

local function encrypt(StringInput)
    local Encrypted = ""
    for letter in string.gmatch("%w") do
        Encrypted..Charset[table.find(Alphabet, string.lower(letter))]
    end
end

This is most probably how it would look like, of course, this is just an example, so don’t rely heavily on this to work!

2 Likes

I agree with @Afrxzo. You can make a simple replacement for all characters, which is fun to make an also works, unless we are dealing with sensitive data. We know two main types of encryption:

  • Symmetric key / private key encryption → each side (sender and receiver) has a specific same key they used to encrypt the data. Both parties need to have that key, which is a big disadvantage when it comes to regular interaction.

  • Asymmetric key / public key encryption → Each party has generates two random keys - a public and a private one, which are linked to each other through very complicated mathematical operations. Because of such complexity, nobody who knows the public key (which anyone can access) can discover the private key, which only the reciever has.

In a nutshell, if encryption is symmetric, both parties have to share the same key, which increases the risk of a third party gaining access to it. When asymmetric encription is used, reciever sends the sender a so called public key, which anyone can access, but nobody can decrypt, even the sender. Only the reciever has a private key, which is linked to public key through very complex mathematical operations. The attacker can try to steal the data from both parties, which is much harder, but there is almost no way for them to decrypt the encrypted document.

Decryption without a key may be achiveable via brute force, which may take nano seconds with weak encrypting algorithms, but millions of years with strong ones and with the use of currently available computers.

Now, creating a custom yet secure encryption algorithm is a very complex process. It’s probably better if you don’t attempt it in case you intend to use the algorithm for any serious encrypting. You may write a good algorithm for practice, but it almost certainly won’t be secure enough for sensitive data. In Roblox environment, it may be sufficient, but again, not for encrypting any sensitive data.

Encryption is a fort in everyone’s plain sight. Don’t protect yourself with a weak one.

You probably don’t need encryption for client-server communication, however. I didn’t need it in the past except in some specific cases. It’s much more useful as part of the gameplay.



@Afrxzo I’m just pasting a little further improved code you pasted.

I’d avoid gradual string concatenation, because of the way strings are combined in lua. Each time you concatenate a particular string, a new one is hashed in memory (!), meaning encryption of 50 characters long message actually produces around 50 strings! Table concatenation is a more performant option.


Simple encryptor and decryptor here!
local key = {["a"] = 1; ["b"] = 2, ["c"] = 3; ["d"] = 4; [" "] = 5;}
local reverse_key = {}

for i, v in pairs(key) do
	reverse_key[v] = i
end

local function decrpyt(message)
	local decrypted = {}
	for char in message:gmatch("%w") do
		decrypted[#decrypted +1] = reverse_key[tonumber(char)]
	end
	decrypted = table.concat(decrypted)
	print(decrypted)
end

local function encrypt(message)
	local encrypted = {}
	for letter in message:gmatch("%w") do
		encrypted[#encrypted +1] = key[letter]
	end
	encrypted = table.concat(encrypted)
	print(encrypted)
	decrpyt(encrypted)
end

encrypt("abcd ")



Here is an interesting module by @dollychun (RSA encryption):

Read more about encryption here: Encryption - Wikipedia (2021-04-06)

About one of the strongest and most common algorithms here: RSA (cryptosystem) - Wikipedia (2021-04-06)

And watch this awesome video that will explain keys:

7 Likes