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

ComputerCraft Encryption - crypto API

Started by computer, 09 April 2016 - 07:57 PM
computer #1
Posted 09 April 2016 - 09:57 PM
It's pretty simple, and it works just like the name suggests.
Anywhere you see "key" is a number that is used to generate a pseudorandom sequence. To share something encrypted with friends, share the key with them.

To download it:


pastebin get cEHfermh crypto

Functions:

crypto.toBinary(text)
Converts a string into a string of binary.

crypto.fromBinary(binary)
Converts a string of binary into a string of text.

crypto.genSequence(key, length)
Generates a pseudorandom binary sequence of the desired length and based on the key.

crypto.applyShift(text, key)
Encrypts a string based on the key. Keep in mind that this function is its own inverse, so you only need one key.

The full API:
Spoiler

-- crypto API
-- Encryption software for ComputerCraft
-- Enjoy! -computer
---------------------------------------------------
--  Global Variables:

local charList =  {"A", "B", "C", "D", "E", "F",
		"G", "H", "I", "J", "K", "L", "M", "N", "O",
		"P", "Q", "R", "S", "T", "U", "V", "W", "X",
		"Y", "Z", "a", "b", "c", "d", "e", "f", "g",
		"h", "i", "j", "k", "l", "m", "n", "o", "p",
		"q", "r", "s", "t", "u", "v", "w", "x", "y",
		"z", " ", ".", ",", "!", "?", "'", "-", "_",
		"1", "2", "3", "4", "5", "6", "7", "8", "9",
		"0", "@", "#", "$", "%", "^", "&", "*", "=",
		"+", "-", "/", "(", ")", "[", "]", "{", "}",
		"<", ">", "\"", "'", ":", ";", "&frac12;", "&frac14;",
		"¡", "¿", "~", "⌂", "Ç", "ü", "é", "â", "ä",
		"à", "å", "Ç", "ê", "ë", "è", "ï", "î", "ì",
		"ä", "å", "é", "æ", "æ", "ô", "ö", "ò", "û",
		"ù", "ÿ", "ö", "ü", "£", "ñ"}

local reverseCharList = {}
for i = 1, #charList do
  reverseCharList[charList[i]] = i
end

---------------------------------------------------
-- Functions:

-- crypto.toBinary(text)
-- crypto.fromBinary(binary)
-- crypto.genSequence(key, length)
-- crypto.applyShift(text, key)

function toBinary (text)
  local binary = {}

  for i = 1, #text do
	local num = reverseCharList[text:sub(i, i)] - 1
	local curBit = 64

	for j = 1, 7 do
	  if bit.band(curBit, num) == curBit then
		binary[#binary + 1] = "1"
	  else
		binary[#binary + 1] = "0"
	  end

	  curBit = curBit / 2
	end
  end

  return table.concat(binary)
end

function fromBinary (binary)
  local text = {}

  for i = 0, #binary / 7 - 1 do
	local num = 0
	local curBit = 64

	for j = 0, 6 do
	  local stringPos = i * 7 + j + 1

	  if binary:sub(stringPos, stringPos) == "1" then
		num = num + curBit
	  end

	  curBit = curBit / 2
	end

	text[i + 1] = charList[num + 1]
  end

  return table.concat(text)
end

function genSequence (key, length)
  local sequence = {}
  local num = key

  while #sequence < length do
	local curBit = 64

	num = num ^ 2 + 1
	if string.len(tostring(num)) > 3 then
	  num = math.floor(num / 100)
	end
	while num > 127 do
	  num = num - 127
	end

	for j = 0, 6 do
	  if bit.band(curBit, num) == curBit then
		sequence[#sequence + 1] = "1"
	  else
		sequence[#sequence + 1] = "0"
	  end

	  curBit = curBit / 2
	end
  end

  return table.concat(sequence)
end

function applyShift (text, key)
  local binText = toBinary(text)
  local binOutput = {}

  for i = 1, #binText do
	local seq = genSequence(key, #binText)
	local digit

	if binText:sub(i, i) == seq:sub(i, i) then
	  digit = 0
	else
	  digit = 1
	end

	binOutput[i] = digit

	key = key + 1
  end

  return fromBinary(table.concat(binOutput))
end

Enjoy! :)/>
Edited on 17 December 2016 - 12:14 PM
randomdude999 #2
Posted 08 June 2016 - 07:27 AM
How secure would this be cryptographically? Usually it's a bad idea to make your own algorithm, you should use something that's proven secure, such as AES.
Anavrins #3
Posted 08 June 2016 - 07:08 PM
How secure would this be cryptographically?
Spoiler
Not much better than Caesar cipher.
It's all fun and cool to make toy crypto algorithms, just to learn more about this kind of stuff, but as you said, it's generally a bad idea to use those in serious applications.
computer #4
Posted 17 December 2016 - 01:03 PM
Ok - just changed the code to make it more secure - characters do not repeat anymore. (Edited OP)
But honestly, who's going to need world-class encryption on a Minecraft world?

Now, aaabbbccc in the demo program (key 387) becomes åFYiYL^'Q
Edited on 17 December 2016 - 12:12 PM
Anavrins #5
Posted 17 December 2016 - 05:24 PM
But honestly, who's going to need world-class encryption on a Minecraft world?
Simply because it is relatively simple for somebody that knows crypto to break algorithms made by somebody inexperienced in this stuff, rendering it useless.

An equally good question would be:
Why use an encryption algorithm that doesn't do its purpose properly.
Edited on 17 December 2016 - 04:30 PM