Write very simple encryption algorithm on Lua. Algorithm provides encrypt(string, key)…
- User
Write very simple encryption algorithm on Lua. Algorithm provides encrypt(string, key) and decrypt(string, key) functions. The strength of the cipher is not important because it will be used in a computer game
- Assistant
Below is a very simple encryption algorithm written in Lua that provides both
encryptanddecryptfunctions. This example uses a basic character shift method (similar in concept to the Caesar cipher) for simplicity. The strength and security of this algorithm are low, making it suitable for non-secure applications like a computer game where the main goal is just to obfuscate the data, not to protect sensitive information.-- Function to encrypt a string function encrypt(inputString, key) local output = {} for i = 1, #inputString do local char = inputString:sub(i,i) local encryptedChar = string.char(((string.byte(char) + key - 32) % 95) + 32) table.insert(output, encryptedChar) end return table.concat(output) end -- Function to decrypt a string function decrypt(inputString, key) local output = {} for i = 1, #inputString do local char = inputString:sub(i,i) local decryptedChar = string.char(((string.byte(char) - key - 32) % 95) + 32) table.insert(output, decryptedChar) end return table.concat(output) end -- Example usage: local originalText = "Hello, World!" local key = 5 local encryptedText = encrypt(originalText, key) print("Encrypted:", encryptedText) local decryptedText = decrypt(encryptedText, key) print("Decrypted:", decryptedText)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.