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

[Question] Encrypt a string from input?

Started by Mackan90096, 24 April 2013 - 12:16 AM
Mackan90096 #1
Posted 24 April 2013 - 02:16 AM
Hi there! I'm wondering if you can make a script where it converts a input to another thing using this "factor":

Spoiler


local a = D
local b = E
local c = F
local d = G
local e = H
local f = I
local g = J
local h = K
local i = L
local j = M
local k = N
local l = O
local m = P
local n = Q
local o = R
local p = S
local q = T
local r = U
local s = V
local t = W
local u = X
local v = Y
local w = Z
local x = A
local y = B
local z = C
local a = D
local b = E
local c = F
local d = G
local e = H
local f = I
local g = J
local h = K
local i = L
local j = M
local k = N
local l = O
local m = P
local n = Q
local o = R
local p = S
local q = T
local r = U
local s = V
local t = W
local u = X
local v = Y
local w = Z
local x = A
local y = B
local z = C

local A = d
local B = e
local C = f
local D = g
local E = h
local F = i
local G = j
local H = k
local I = l
local J = m
local K = n
local L = o
local M = p
local N = q
local O = r
local P = s
local Q = t
local R = u
local S = v
local T = w
local U = x
local V = y
local W = z
local X = a
local Y = b
local Z = c

So for example, "Hi" would be kL

If you can, how?
theoriginalbit #2
Posted 24 April 2013 - 02:38 AM
Ok its a nice and small program that I wrote up in a few seconds, so I don't feel code monkey like, so… done and commented…
http://pastebin.com/HddiLkwG

EDIT: Oh and btw, its not really an 'encryption' its an encoding. There is no key involved, it doesn't convert every character, and it preserves formatting (which would be fixed by the last point) so it cannot be classed as an encryption.
Edited on 24 April 2013 - 12:40 AM
remiX #3
Posted 24 April 2013 - 02:40 AM
They need to be in " " btw :P/>

But don't use this, it isn't secure at all…
Something like this would be better
theoriginalbit #4
Posted 24 April 2013 - 02:41 AM
Something like this would be better
Well it depends on if when OP says they wants an 'encryption' whether they actually do, or whether a hash could fill the requirements.
Mackan90096 #5
Posted 24 April 2013 - 04:04 AM
Is there a decryption using this too?
remiX #6
Posted 24 April 2013 - 04:10 AM
Is there a decryption using this too?

Yes, this would be the decryption function.
function decrypt(str)
	local temp = ""
	for i = 1, str:len() do
		local char = str:sub(i,i)
		for i, v in pairs(lookup) do
			if v == char then temp = temp .. char break end
		end
	end
	return str
end

Untested
Dlcruz129 #7
Posted 24 April 2013 - 04:12 AM
Is there a decryption using this too?

You could just swap the table values. Instead of ["a"] = "e"…
You can do
["e"] = "a"
theoriginalbit #8
Posted 24 April 2013 - 08:22 AM
You could just swap the table values. Instead of ["a"] = "e"…
You can do
["e"] = "a"
That would cause problems with initial lookup, unless you had a separate lookup table… personally I would use the function remiX posted (that btw doesn't work), with some changes.

function decode(str)
  -- create our return string
  local temp = ""
  -- loop through the input string
  for i = 1, #str do
	-- make a variable to track if a match was found (this is for non-alphabetic character support)
	local found = false
	-- get the current character
	local char = str:sub(i,i)
	-- loop through all the table entries
	for k, v in pairs(lookup) do
	  -- if the entry in the table is the same as the current character
	  if v == char then
		-- append it to the return value
		temp = temp..k
		-- set the found flag to true
		found = true
		-- stop searching the lookup table
		break
	  end
	end
	-- if we didn't find a match, it's not an alphabetic character, so append it
	if not found then
	  temp = temp..char
	end
  end
  -- return the decoded string
  return temp
end