This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Coding Code

Started by mrpoopy345, 23 November 2013 - 06:23 AM
mrpoopy345 #1
Posted 23 November 2013 - 07:23 AM
Hello, unlike my other posts, I have nothing and I have pretty much no idea how to do this.
What I want to do is have a user input a word/sentence and have it take that and code into this sort of code that I am not sure what it is called but it is like A = G, B = R, etc. And save it as a variable.
That might sound a little confusing. Input and output of the ideal program:

Input:
Hello

Output:
#6rgF

If you need any extra details feel free to ask!
AgentE382 #2
Posted 23 November 2013 - 08:03 AM
It seems you want an encryption cipher of some kind. Since it looks like you're just wanting the "Caesar" mono-alphabetic substitution cipher, here's an implementation:
-- Usage:
-- local input = read()
-- local output = encrypt(input, 58)
-- local original = decrypt(output, 58)

local chars = {string.byte([[ !"#$%&amp;'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~]], 1, 95)} --'"

function encrypt( input, rotateDistance )
	local temp = {string.byte(input, 1, #input)}

	for i = 1, #temp do
	  temp[i] = chars[(temp[i] - 32 + rotateDistance) % 95 + 1]
	end

	return string.char(table.unpack(temp))
end

function decrypt( input, rotateDistance )
	local temp = {string.byte(input, 1, #input)}

	for i = 1, #temp do
	  temp[i] = chars[(temp[i] - 32 - rotateDistance) % 95 + 1]
	end

	return string.char(table.unpack(temp))
end
If you want something more cryptographically secure, you can check out the RC4 implementation in my signature, or some of the excellent algorithms in the APIs and Utilities forum / Programs forum.
Edited on 23 November 2013 - 07:14 AM