Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("rsa")
- local function generateKeyPair()
- while true do
- local e = rsa.generateLargePrime()
- write("-")
- sleep(0.1)
- local p = rsa-generatePQ(e)
- write("-")
- sleep(0.1)
- local q = rsa.generatePQ(e)
- write("-")
- sleep(0.1)
- local n = p * q
- local phi = (p - 1) * (q - 1)
- local d = rsa.modinv(e, phi)
- -- 104328 is just a magic number (can be any semi-unique number)
- local encrypted = rsa.modexp(rsa.bigint(104328), e, n)
- local decrypted = rsa.modexp(encrypted, d, n)
- write("+")
- sleep(0.1)
- counter = 0
- if decrypted == rsa.bigint(104328) then
- counter = 0
- return {
- shared = tostring(n),
- public = tostring(e),
- }, {
- shared = tostring(n),
- private = tostring(d),
- }
- end
- end
- end
- if not fs.exists("/public.key") then
- print("Generating RSA keys, this can take a few minutes.")
- local start = os.clock()
- local publicKey, privateKey = generateKeyPair()
- local file = io.open("./public.key", "w")
- file:write(textutils.serialize(publicKey))
- file:close()
- file = io.open("./private.key", "w")
- file:write(textutils.serialize(privateKey))
- file:close()
- print("Finished!")
- end
- local f = io.open("/public.key", "r")
- local publicKey = textutils.unserialize(f:read("*a"))
- f:close()
- f = io.open("/private.key", "r")
- local privateKey = textutils.unserialize(f:read("*a"))
- f:close()
- local byteSyze = 8
- local bits = 256
- local msg = "Heil"
- local startTime = os.clock()
- local res = rsa.bytesToNumber(rsa.stringToBytes(msg), bits, byteSize)
- local encrypted = rsa.crypt(publicKey, res)
- print("Took "..os.clock()-startTime.." seconds to encrypt")
- print(encrypted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement