Advertisement
Mackan90096

Untitled

Jul 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. os.loadAPI("rsa")
  2.  
  3. local function generateKeyPair()
  4. while true do
  5. local e = rsa.generateLargePrime()
  6. write("-")
  7. sleep(0.1)
  8. local p = rsa-generatePQ(e)
  9. write("-")
  10. sleep(0.1)
  11. local q = rsa.generatePQ(e)
  12. write("-")
  13. sleep(0.1)
  14.  
  15. local n = p * q
  16. local phi = (p - 1) * (q - 1)
  17. local d = rsa.modinv(e, phi)
  18.  
  19. -- 104328 is just a magic number (can be any semi-unique number)
  20. local encrypted = rsa.modexp(rsa.bigint(104328), e, n)
  21. local decrypted = rsa.modexp(encrypted, d, n)
  22.  
  23. write("+")
  24. sleep(0.1)
  25. counter = 0
  26.  
  27. if decrypted == rsa.bigint(104328) then
  28. counter = 0
  29. return {
  30. shared = tostring(n),
  31. public = tostring(e),
  32. }, {
  33. shared = tostring(n),
  34. private = tostring(d),
  35. }
  36. end
  37. end
  38. end
  39.  
  40. if not fs.exists("/public.key") then
  41. print("Generating RSA keys, this can take a few minutes.")
  42.  
  43. local start = os.clock()
  44.  
  45. local publicKey, privateKey = generateKeyPair()
  46.  
  47. local file = io.open("./public.key", "w")
  48. file:write(textutils.serialize(publicKey))
  49. file:close()
  50. file = io.open("./private.key", "w")
  51. file:write(textutils.serialize(privateKey))
  52. file:close()
  53.  
  54. print("Finished!")
  55. end
  56.  
  57. local f = io.open("/public.key", "r")
  58. local publicKey = textutils.unserialize(f:read("*a"))
  59. f:close()
  60. f = io.open("/private.key", "r")
  61. local privateKey = textutils.unserialize(f:read("*a"))
  62. f:close()
  63.  
  64. local byteSyze = 8
  65. local bits = 256
  66.  
  67. local msg = "Heil"
  68.  
  69. local startTime = os.clock()
  70.  
  71. local res = rsa.bytesToNumber(rsa.stringToBytes(msg), bits, byteSize)
  72. local encrypted = rsa.crypt(publicKey, res)
  73.  
  74. print("Took "..os.clock()-startTime.." seconds to encrypt")
  75.  
  76. print(encrypted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement