· 6 years ago · Nov 14, 2019, 06:08 PM
1--ComputerCraft
2
3--[[
4MDCrypt 1.0
5
6MDCrypt is a simple and fast encryption/decryption API which allows data to be saved and transferred with an encryption key.
7
8Copyright (C) 2012 MetaDark
9
10This program is free software: you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation, either version 3 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program. If not, see <http://www.gnu.org/licenses/>.
22]]--
23
24local charSize = 255
25local amplitude = charSize/math.pi
26local frequency = math.pi/charSize*8
27
28-- MetaDark Offset Algorithm 1 (Not complicated but very hard to crack) --
29function MDOA1(x)
30 return math.ceil(amplitude * math.sin(frequency * x) + (amplitude*(math.cos(x) % 9)))
31end
32
33-- Convert letters into numbers --
34function stringToInt(input)
35 if type(input) == "number" then
36 return input
37 elseif type(input) == "string" then
38 output = 0
39 for x = 1, #input do
40 output = output + input:byte(x)
41 end
42
43 return output
44 else
45 return 0
46 end
47end
48
49-- Encrypt String --
50function encrypt(input, key)
51 key = stringToInt(key)
52
53 local output = ""
54 for x = 1, #input do
55 output = output .. string.char((input:byte(x) + MDOA1(x + key)) % charSize)
56 end
57
58 return output
59end
60
61-- Decrypt String --
62function decrypt(input, key)
63 key = stringToInt(key)
64
65 local output = ""
66 for x = 1, #input do
67 output = output .. string.char((input:byte(x) - MDOA1(x + key)) % charSize)
68 end
69
70 return output
71end