lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> But this
> http://codepad.org/JJcm3J5b
> (which should reproduce the sha1-rsa library using bc)  is not working
> (the decrypted message is not the one I'm expecting to see).

public, private and modulus are all numbers in hex. So you need to convert
them to bc numbers but cannot use string2bc; you need hex2bc. You also seem
to need bc2string. The complete program is attached and seems to work.
(There was a small bug in string2bc.)
local bc = require"bc"

function string2bc(s)
       local x=bc.number(0)
       for i=1,#s do
               x=256*x+s:byte(i)
       end
       return x
end

function bc2string(x)
	if x:iszero() then
		return ""
	else
		local r
		x,r=bc.divmod(x,256)
		return bc2string(x)..string.char(r:tonumber())
       end
end

function hex2bc(s)
	local x=bc.number(0)
	for i=1,#s do
		x=16*x+tonumber(s:sub(i,i),16)
	end
	return x
end

public  = "10001"
private = "816f0d36f0874f9f2a78acf5643acda3b59b9bcda66775b7720f57d8e9015536160e72"..
"8230ac529a6a3c935774ee0a2d8061ea3b11c63eed69c9f791c1f8f5145cecc722a220d2bc7516b6"..
"d05cbaf38d2ab473a3f07b82ec3fd4d04248d914626d2840b1bd337db3a5195e05828c9abf8de8da"..
"4702a7faa0e54955c3a01bf121"
modulus = "bfedeb9c79e1c6e425472a827baa66c1e89572bbfe91e84da94285ffd4c7972e1b9be3"..
"da762444516bb37573196e4bef082e5a664790a764dd546e0d167bde1856e9ce6b9dc9801e4713e3"..
"c8cb2f12459788a02d2e51ef37121a0f7b086784f0e35e76980403041c3e5e98dfa43ab9e6e85558"..
"c5dc00501b2f2a2959a11db21f"

url = "www.lua.org"
m = string2bc(url)
d = bc.number(public)
d = hex2bc(public)
e = hex2bc(private)
n = hex2bc(modulus)

print("Message as big-integer:\n"..tostring(m))
x = bc.powmod(m,e,n)
print("\nEncrypted = \n"..tostring(x))

y =bc.powmod(x,d,n) 
print("Decrypted Message = \n"..tostring(y))

print("Decrypted Message:",bc2string(y))