Advertisement
Guest User

Untitled

a guest
Aug 10th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.47 KB | None | 0 0
  1. using Primes, StatsBase
  2.  
  3. """
  4. modular_inverse(a::T, n::T) where T <: Integer
  5.  
  6. Finds the modular inverse of `a` mod `n`
  7. """
  8. function modular_inverse(a::T, n::T) where T <: Integer
  9.     mod(gcdx(a, n)[2], n)
  10. end # function
  11.  
  12. """
  13. πœ‘(p::T, q::T) where T <: Integer
  14.  
  15. Returns Euler's totient function
  16. """
  17. function πœ‘(p::T, q::T) where T <: Integer
  18.     (p-1)*(q-1)
  19. end # function
  20.  
  21. function square(x::T) where T <: Integer
  22.     x * x
  23. end
  24.  
  25. """
  26. is_legal_public_exponent(e::T, p::T, q::T) where T <: Integer
  27.  
  28. Returns wether the given exponent is a valid one: 1 < e < πœ‘(p,q) && gcd(e, πœ‘(p,q)) == 1
  29. """
  30. function is_legal_public_exponent(e::T, p::T, q::T) where T <: Integer
  31.     return one(T) < e && e < πœ‘(p, q) && gcd(e, πœ‘(p,q)) == one(T)
  32. end # function
  33.  
  34. """
  35. private_exponent(e::T, p::T, q::T) where T <: Integer
  36.  
  37. The private exponent is the modular inverse of the public one
  38. """
  39.  
  40. function private_exponent(e::T, p::T, q::T) where T <: Integer
  41.     if is_legal_public_exponent(e, p, q)
  42.         return invmod(e, πœ‘(p, q))
  43.     else
  44.         error("Not a legal public exponent for that modulus")
  45.     end
  46. end
  47.  
  48. """
  49. encrypt(m::T, e::T, n::T) where T <: Integer
  50.  
  51. Encrypts the given message with the given public key
  52. """
  53. function encrypt(m::T, e::T, n::T) where T <: Integer
  54.     if m > n
  55.         error("The modulo is too small to encrypt the message")
  56.     else
  57.         powermod(m, e, n)
  58.     end # if
  59. end # function
  60.  
  61. """
  62. decrypt(c::T, d::T, n::T) where T <: Integer
  63.  
  64. Decrypts the given message with the given private key
  65. """
  66. function decrypt(c::T, d::T, n::T) where T <: Integer
  67.     return powermod(c, d, n)
  68. end # function
  69.  
  70.  
  71. # Generating efficiently primes
  72. pr = primes(4000, 5000)
  73.  
  74. # Randmoly choosing two prime numbers
  75. p = BigInt(sample(pr))
  76. q = BigInt(sample(pr))
  77.  
  78. # Defining the needed values for RSA
  79. n = p * q
  80.  
  81. # Choosing an e such that is coprime with πœ‘
  82. e = BigInt(sample(primes(Int(πœ‘(p, q)))))
  83. while !is_legal_public_exponent(e, p, q)
  84.     global e = BigInt(sample(primes(Int(πœ‘(p, q)))))
  85. end # while
  86.  
  87. d = private_exponent(e, p, q)
  88.  
  89. @time begin
  90.     # Message to be sent
  91.     message = collect("Ciao come va? Tutto bene, grazie, e tu?")
  92.     # @show message
  93.  
  94.     l = length(message)
  95.  
  96.     char_message = Array{BigInt}(undef, l)
  97.     # Convert the message in an array of integers. Note: no UNICODE support
  98.     for i in eachindex(message)
  99.         char_message[i] = BigInt(Int64(message[i]))
  100.     end # for
  101.     # @show char_message
  102.  
  103.     char_encrypted = Array{BigInt}(undef, l)
  104.     # Encrypting the message, char by char, with the formula c ≑ m^e mod n
  105.     for i in eachindex(char_message)
  106.         char_encrypted[i] = encrypt(char_message[i], e, n)
  107.     end # for
  108.     # @show char_encrypted
  109.  
  110.     char_decrypted = Array{BigInt}(undef, l)
  111.     # Decrypting the message, char by char, with the formula m ≑ c^d mod n
  112.     for i in eachindex(char_encrypted)
  113.         char_decrypted[i] = decrypt(char_encrypted[i], d, n)
  114.     end # for
  115.     # @show char_decrypted
  116.  
  117.     message_after = Array{Char}(undef, l)
  118.     for i in eachindex(char_decrypted)
  119.         message_after[i] = Char(char_decrypted[i])
  120.     end # for
  121.     # @show message_after
  122. end
  123.  
  124. @time decrypt(encrypt(BigInt(4000002), e, n), d, n)
  125.  
  126. # First Compilation:
  127. # 0.101560 seconds (25.54 k allocations: 1.236 MiB)
  128. # 0.000056 seconds (15 allocations: 360 bytes)
  129.  
  130. # Second Compilation:
  131. # 0.005980 seconds (3.33 k allocations: 152.551 KiB)
  132. # 0.000017 seconds (15 allocations: 360 bytes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement