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

UTF Encrypt

Started by metron80, 12 August 2015 - 03:49 PM
metron80 #1
Posted 12 August 2015 - 05:49 PM
UseTheForce Encryption

For ALL of your secrets…


So I was looking into encryption for a server based program I was making. The program has user sending passwords over rednet, so I needed an encryption. Unfortunately, the only one I found, was this one by Malte. But that one was buggy and cracked. But looking at the reason it was buggy, it gave me an idea. So I went ahead and created this. It's sort of secure, so long as nobody else knows your key. It's simple code, yet complex encryption.


--Syntax:
encrypt(string, key)
decrypt(string, key)
-- Pretty self explanatory. Examples:
x = "This is an example string. :aFj(-$%;!"
str = encrypt(x, "4bG29x")
print(str)
--Output:
-80-118-98-103-128-141-105-147-90-98-128-137-110-111-102-100-103-137-123-129-109-102-100-146-93-148-126-135-92-112-96-151-134-128-139
-------------------------------------------------------------
newStr = decrypt(str, "4bG29x")
print(newStr)
--Output:
This is an example string. :aFj(-$%;!
---------------------------------------------------------------
newStr = decrypt(str, "abc123")
print(newStr)
--Output:
Z p+~~y~h%~~44t#!~&!1!$~m~%~ /p~;~~
----------------------------------------------------------------
newStr = decrypt(str, "4bG29y")
print(newStr)
--Output:
This hs am dxampld strimg. :aEj($%!

So basically, just keep your key complex. These are currently the supported characters:

charValues = {
"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", " ", ".", ",", ")", "(", "*", "&", "%", "$", "#", "@", "!", "+", "=", ":",
";", "\"", "'", "?", "/", "~", "-"
}
You can easily add more or remove some by simply adding to or removing from this table at the top of the code. Those characters listed are the only ones you can use in text or keys; those are the only ones that this algorithm understands. Again, you could add more if you need to.

Pastebin:
http://pastebin.com/NkeDTRMB

pastebin get NkeDTRMB utfe


This also works in pure Lua; it uses no CCApi. Let me know if you find any bugs or what not. And, as always, enjoy!
Edited on 13 August 2015 - 01:11 PM
HPWebcamAble #2
Posted 13 August 2015 - 06:05 AM
Not bad, there's SquidDev's AES API, which is, as far as I know, actual AES encryption, so its really secure (but a little slow in CC)
http://www.computerc...aes-encryption/
It's currently broken, since the Bit API, which it relys on, is messed up in CC 1.74

Your program is a good alternative, since its simply yet difficult to crack.

You might be able to shrink the encrypted string by calling 'string.char' on each number.
Then, when decrypting, call 'string.byte' on each character to get the number again.
This way, you don't need to use the "-" between each number - each number gets converted to a single character.
You'll want to be careful though, there are a limited number of characters, so you can only do this conversion for a range of numbers.
Edited on 13 August 2015 - 04:05 AM
SquidDev #3
Posted 13 August 2015 - 08:16 AM
A couple of points on efficiency.
  • Appending to a table then running table.concat is quicker than repeated string concatenation for long strings - in fact it might be easier if you return the table instead - then you don't need your complicated number splitter in the encrypt section :)/>.
  • Convert charValues to a lookup table - so you can do i = charValues[character] rather than iterating through every key.
Also: Don't roll your own crypto. No one is an expert on cryptography here, but I guess I could put together some sort of decent cracker for this in a couple of hours. From what I can tell it is like a vigenere cipher, with some additional changes. Using something like this could probably let you crack it.

I'm not trying to shoot you down, I love seeing people's encryption methods, however I wouldn't market this as secure. :P/>

Not bad, there's SquidDev's AES API, which is, as far as I know, actual AES encryption, so its really secure (but a little slow in CC)
30Kb/s - that is the entire hard drive of a computer in 30 seconds :P/>. Though not exactly as fast as a decent C implementation.
metron80 #4
Posted 13 August 2015 - 03:28 PM

That was one of the reasons Malte's API didn't work too well; there is a limited range of characters, and if a character goes over the max range, I don't want to have it crash. And this:

I must look that the result stays between 32 and 126.
When I was creating an encryption program I did that too at first, but after some weird errors every now and again I took a closer look and noticed that the range of printable character is actually split in two.
Byte 96 is not printable, ergo the ranges are [32-95] and [97-126].

Edit:
Btw. with "When I was creating an encryption program" I didn't mean creating my own encryption algorithm, but just implementing an already proven, secure one.^^



I'm a little rusty with by table API, would have to take another look at the wiki :P/> And correct me if I'm wrong, but is a lookup table the pairs loop?
SquidDev #5
Posted 13 August 2015 - 03:43 PM
I'm a little rusty with by table API, would have to take another look at the wiki :P/>/> And correct me if I'm wrong, but is a lookup table the pairs loop?

Intead of a table like:

local foo = {'a', 'b', 'c' }
for k,v in pairs(foo) if v == target then return k end end

you can have

local foo = {a = 1, b = 2, c = 3}
return foo[target]

The difference is that for loop is O(n) complexity and the lookup is O(1) complexity (I think). This pretty much means that it will take the for loop takes longer as your list takes longer, but the lookup takes the same amount of time however big the list it.

You can generate this table with code a bit like:


local foo = {'a', 'b', 'c' }
for i =1,#foo do foo[foo[i]] = i end
HPWebcamAble #6
Posted 13 August 2015 - 04:47 PM
Not bad, there's SquidDev's AES API, which is, as far as I know, actual AES encryption, so its really secure (but a little slow in CC)
30Kb/s - that is the entire hard drive of a computer in 30 seconds :P/>. Though not exactly as fast as a decent C implementation.

I couldn't figure out why your computer only had 900 Kb of storage…
Then I realized you meant a CC computer :D/>
metron80 #7
Posted 14 August 2015 - 02:17 AM

If I did that, would I have to make the table like this?

local foo = { ["a"] = 1, ["b"] = 2, ["c"] = 3, ["1"] = 4 }
Because numbers cannot be variables on their own, so they would have to be a string.
Edited on 14 August 2015 - 12:18 AM
HPWebcamAble #8
Posted 14 August 2015 - 04:56 AM
If I did that, would I have to make the table like this?
Because numbers cannot be variables on their own, so they would have to be a string.

Yes, for the numbers. But the code SquidDev posted will turn your existing table into the key-value's one.

local foo = {'a', 'b', 'c' }
for i =1,#foo do
  foo[foo[i]] = i
end
In your code, foo would be charValues

You can run it every time you encrypt something, or run it once, serialize the result, remove the unwanted entries (like 'a','b','c'), then paste it in place of the existing one.
Edited on 14 August 2015 - 02:57 AM