Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("/Primes.lua")
- os.loadAPI("/TableInt.lua")
- function GetNew(_bitLength)
- local k = {}
- k.p = Primes.GetBetween(19,2^_bitLength)
- k.q = Primes.GetBetween(19,2^_bitLength)
- k.n = k.p * k.q
- k.phi_n = (k.p-1) * (k.q-1)
- local eTable = {}
- local e = 2
- for i = 1, 4 do
- while Primes.GCD2(e,k.phi_n) ~= 1 or Primes.GCD2(e,k.n) ~= 1 do
- e = e + 1
- end
- eTable[i] = e
- end
- k.e = eTable[math.random(1,#eTable)]
- local dTable = {}
- local d = 2
- for i = 1, 4 do
- while (d * k.e) % k.phi_n ~= 1 do
- d = d + 1
- end
- dTable[i] = d
- end
- k.d = dTable[math.random(1,#dTable)]
- local pb = {k.e,k.n}
- pb.e = k.e
- pb.n = k.n
- local pv = {k.d,k.n}
- pv.d = k.d
- pv.n = k.n
- k.public = pb
- k.private = pv
- return k
- end
- function PrintKey(_key)
- print("p = ".._key.p)
- print("q = ".._key.q)
- print("n = ".._key.n)
- print("phi_n = ".._key.phi_n)
- print("e = ".._key.e)
- print("d = ".._key.d)
- end
- function Encrypt(_message,_pair)
- local cipher = ""
- for i = 1, #_message do
- cipher = cipher .. TableInt.Mod(TableInt.Pow(_message:sub(i,i):byte(),_pair[1],'s'),_pair[2]) .. ' '
- end
- return cipher
- end
- function Decrypt(_cipher,_pair)
- local message = ""
- local i = 1
- while i < #_cipher do
- local mChar = ""
- while _cipher:sub(i,i) ~= ' ' do
- mChar = mChar .. _cipher:sub(i,i)
- i = i + 1
- end
- message = message .. string.char(TableInt.Mod(TableInt.Pow(tonumber(mChar),_pair[1],'s'),_pair[2]))
- i = i + 1
- end
- return message
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement