[Library] Kuey Encoding/Decoding

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Featzen
Prole
Posts: 7
Joined: Tue Jul 07, 2015 2:14 am

[Library] Kuey Encoding/Decoding

Post by Featzen »

I made this small module for fun, and decided to post here. I don't know the algorithm, but works, haha.

Example:

Code: Select all

Kuey = require("Kuey")

local Encoded = Kuey.encode("Love is life!", "love2d") -- Encode the string with "love2d" as key
print(Encoded)                                   -- Show the encoded string
print(Kuey.decode(Encoded, "anykey"))            -- Try to show a decoded string with any key
print(Kuey.decode(Encoded, "love2d"))            -- Show the decoded string with the correct key
Output:

Code: Select all

©Ìý╩R═▀ÅÔ╬	ÿ╔ì
Wps_ýT~!ic3P,
Love is life!
Available functions:

Code: Select all

Kuey.encode(string, key) -- Return a string
Kuey.decode(string, key) -- Return a string
Kuey.encodeFile(filename, key[, outputname]) -- Return a string and write a file if outputname is defined.
Kuey.decodeFile(filename, key[, outputname]) -- Return a string and write a file if outputname is defined.
I think this is all, I hope you enjoy.
(Sorry my bad english)
Kuey.lua
(2.29 KiB) Downloaded 458 times
Last edited by Featzen on Thu Jan 21, 2016 2:44 am, edited 1 time in total.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: [Library] Kuey Encoding/Decoding

Post by master both »

Great library! I could really use this. Can I see the source code?
Featzen
Prole
Posts: 7
Joined: Tue Jul 07, 2015 2:14 am

Re: [Library] Kuey Encoding/Decoding

Post by Featzen »

LoL, i made this post and forgot to post the lib, posting.
hahahaha
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [Library] Kuey Encoding/Decoding

Post by bartbes »

I'm interested in cryptography, so I couldn't pass this opportunity up to figure out what this lib did. So I figured I'd decipher the code, and the easiest way to do that is by rewriting it. Turns out, the actual "encoding" is really simple:

Code: Select all

local rekuey = {}

local function encode_char(plainc, keyc, i, j)
	plainc, keyc = plainc:byte(i), keyc:byte(j)
	return string.char((plainc + keyc)%255)
end

local function decode_char(encc, keyc, i, j)
	encc, keyc = encc:byte(i), keyc:byte(j)
	return string.char((encc - keyc)%255)
end

local function code(input, key, f)
	local output = {}
	for i = 1, #input do
		local j = (i-1) % #key + 1
		output[i] = f(input, key, i, j)
	end

	return table.concat(output)
end

function rekuey.encode(plain, key)
	return code(plain, key, encode_char)
end

function rekuey.decode(enc, key)
	return code(enc, key, decode_char)
end

return rekuey
As you can guess, this doesn't really have any nice properties, it means the key can be broken character-by-character, it's probably vulnerable to known-plaintext etc. It basically comes down to a substitution cipher.

As for speed, I measured the performance, both of kuey and "rekuey", it took my code about a second to do 200,000 decryption attempts (original code took about 1.5 seconds).

Code: Select all

package.preload.love = package.loadlib("/usr/lib/liblove.so", "luaopen_love")
require "love"
require "love.timer"

local kuey = require "kuey"
local rekuey = require "rekuey"

local iterations = 200000
local encoded = rekuey.encode("Love is life!", "love2d")

local function time(lib)
	local startTime = love.timer.getTime()
	local found = false
	for i = 1, iterations do
		if lib.decode(encoded, tostring(i)) == "Love is life!" then
			found = true
		end
	end
	local endTime = love.timer.getTime()
	print(("Found solution: %s\nTime taken: %f"):format(found, endTime-startTime))
end

time(kuey)
time(rekuey)
In short, if you're hoping to do something serious with this cipher: don't. If it's just some simple obfuscation that you don't mind getting beaten (which may be as simple as reading the decryption code...), then sure, go ahead and use it.

EDIT: Using luajit's ffi to get faster string->byte->string conversions I can easily get it up to 1,000,000 decodes per second.

EDIT2: I should probably add a disclaimer, because this might sound harsh. This is a fun way to get going with cryptography yourself, and perhaps you could use this as feedback to make something better, like making sure the results depend on the characters before it, or something of the sort. This post mostly served because I was interested in what your cipher is doing, and to warn people to not rely on it for serious encryption purposes. If your goal is to just hide some save data from casual users, go ahead.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest