Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

write me a RSA Encryption to protect RemoteEvents from exploiters, Needs to support 64…

  1. User

    write me a RSA Encryption to protect RemoteEvents from exploiters, Needs to support 64 bit Encryption keys needs to automatically randomly generate the key depending on the seed of tick(). Also needs to be in a modulescript so I can just call RSA:Encrypt(data) also needs to support Encrypting Strings

  2. Assistant

    Here is an implementation of RSA encryption in Lua for protecting RemoteEvents from exploiters:

    local RSA = {}
    
    local function is_prime(n)
      if n <= 1 then return false end
      for i = 2, math.sqrt(n) do
        if n % i == 0 then return false end
      end
      return true
    end
    
    -- generate public/private key pairs
    function RSA.generate_key_pairs()
      -- choose two large prime numbers
      local p, q
      repeat
        p = math.random(2 ^ 31, 2 ^ 32 - 1)
      until is_prime(p)
      repeat
        q = math.random(2 ^ 31, 2 ^ 32 - 1)
      until is_prime(q)
      
      -- calculate n, totient(n), e, and d
      local n = p * q
      local totient = (p - 1) * (q - 1)
      local e = 65537  -- commonly used
      local d = 0
      repeat
        d = d + 1
      until (e * d) % totient == 1
      
      return {public_key = {e, n}, private_key = {d, n}}
    end
    
    -- encrypt a message with the public key
    function RSA.encrypt(message, public_key)
      local e, n = public_key[1], public_key[2]
      local m = #tostring(n) - 1
      local blocks = {}  -- split message into blocks of size m
      for i = 1, #message, m do
        table.insert(blocks, message:sub(i, i + m - 1))
      end
      local encrypted_blocks = {}  -- encrypt each block
      for i, block in ipairs(blocks) do
        local b = 0
        for j = 1, #block do
          b = b + block:byte(j) * 256 ^ (j - 1)
        end
        local c = b ^ e % n
        table.insert(encrypted_blocks, c)
      end
      return encrypted_blocks
    end
    
    -- decrypt a message with the private key
    function RSA.decrypt(encrypted_blocks, private_key)
      local d, n = private_key[1], private_key[2]
      local blocks = {}  -- decrypt each block
      for i, c in ipairs(encrypted_blocks) do
        local b = c ^ d % n
        local block = ""
        for j = 0, math.floor(math.log(b, 256)) do
          local byte = b % 256
          block = string.char(byte) .. block
          b = (b - byte) / 256
        end
        table.insert(blocks, block)
      end
      return table.concat(blocks)
    end
    
    -- generate key pairs depending on the seed of tick()
    math.randomseed(tick())
    local key_pairs = RSA.generate_key_pairs()
    
    -- encrypt a message
    function RSA.encrypt_message(message)
      return RSA.encrypt(message, key_pairs.public_key)
    end
    
    -- decrypt a message
    function RSA.decrypt_message(encrypted_blocks)
      return RSA.decrypt(encrypted_blocks, key_pairs.private_key)
    end
    
    return RSA
    

    To use this module in your scripts, simply require it and call RSA.encrypt_message(data) to encrypt your data and RSA.decrypt_message(encrypted_blocks) to decrypt it. For example:

    local RSA = require(script.RSA)
    
    -- encrypt a string
    local encrypted_blocks = RSA.encrypt_message("hello world")
    print(table.concat(encrypted_blocks, ","))
    
    -- decrypt the encrypted string
    local decrypted_message = RSA.decrypt_message(encrypted_blocks)
    print(decrypted_message)