Trying to make a key encryption/decryption?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Trying to make a key encryption/decryption?

Post by Lua Hal »

Code: Select all

table={"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","_"," "}
function index(tab,key)
	for i = 1,#tab do
		if string.lower(tab) == string.lower(key) then
		return i
		end
	end
end
function encrypt(key,value)
	str,text=key,value
	num=string.len(text)
	string=string.rep(str,math.floor(num/string.len(str))) --string.sub includes syntax
	remainder=num-string.len(string)
	longpass=string..string.sub(str,0,remainder)
	for i = 1,#string.len(text) do
		if index(table,string.sub(text,0,1))+index(table,string.sub(longpass,1,1)) ~= nil then
		text=string.sub(text,0,i-1)..table[(index(table,string.sub(text,0,1))+index(table,string.sub(longpass,1,1)))]..string.sub(text,i+1)
		else
		local temp=table[(index(table,string.sub(text,0,1))+index(table,string.sub(longpass,1,1)))-#table]
		text=string.sub(text,i-1,i-1)..temp..string.sub(text,i+1)
		end
	end
	return text
end



function love.draw()
love.graphics.print(encrypt("a","Hi"),0,0)
end
It should translate a string based on this:

Every key is replaced with the sum of that keys numerical value in the table and the numerical value in the password repeated to be the same length as the translated strings value in the table

Let;
H=1
L=2
M=3
N=4

password is "HL"
to change = "HLLH"

1. Password is lengthened to 4 characters long (repetition) ("HLHL")
2. String is translated by adding password and string

HLLH + HLHL

HLLH (1221)
HLHL (1212) +
HNMM (1433)

Sums do not cross over, but instead re-wrap around the table when they exceed #table As in:

#table is 3
Sum is 4
Sum becomes 1
(1,2,3) *loop around* (1)

Thanks in advance
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Trying to make a key encryption/decryption?

Post by Taehl »

Aah, the good old Vigenere cypher. Here's a function which does pretty much the same thing you do, except in a more optimal way. Please note that this version won't really produce human-readable results, since it uses all 256 ASCII characters (the full byte). Also, just so you know, the way you have it set up, you're re-encrypting the text every frame (you probably should encrypt once and save the result to a variable).

Code: Select all

function encrypt(key, plaintext)
	local cyphertext = ""
	for i = 1,string.len(plaintext) do
		local ki = i % string.len(key)
		if ki==0 then ki = string.len(key) end
		cyphertext = cyphertext .. string.char( (string.byte(plaintext, i,i)+string.byte(key, ki,ki)) % 256 )
	end
	return cyphertext
end

function love.load()
	result = encrypt("a","Hi")
end

function love.draw()
	love.graphics.print(result, 0,0)
end
Decryption in a Vigenere cypher, by the way, works in exactly the same way, except you subtract they key instead of adding it. Hope this helps.
Last edited by Taehl on Sat Sep 03, 2011 3:25 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: Trying to make a key encryption/decryption?

Post by Lua Hal »

Thanks!
User avatar
Codex
Party member
Posts: 106
Joined: Tue Mar 06, 2012 6:49 am

Re: Trying to make a key encryption/decryption?

Post by Codex »

What would something like this be good for in a game?
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Trying to make a key encryption/decryption?

Post by mickeyjm »

Codex wrote:What would something like this be good for in a game?
Confusion
Your screen is very zoomed in...
Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests