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:
Functions:
The full API:
Enjoy! :)/>
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", "@", "#", "$", "%", "^", "&", "*", "=",
"+", "-", "/", "(", ")", "[", "]", "{", "}",
"<", ">", "\"", "'", ":", ";", "½", "¼",
"¡", "¿", "~", "⌂", "Ç", "ü", "é", "â", "ä",
"à", "å", "Ç", "ê", "ë", "è", "ï", "î", "ì",
"ä", "å", "é", "æ", "æ", "ô", "ö", "ò", "û",
"ù", "ÿ", "ö", "ü", "£", "ñ"}
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