Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.78 KB | None | 0 0
  1. require 'prime'
  2.  
  3. class RSA
  4.     def initialize(n, e, d)
  5.         @n = n
  6.         @e = e
  7.         @d = d
  8.  
  9.     end
  10.    
  11.     def n
  12.         #returns the value of 'n' passed in the initialize
  13.         return n
  14.     end
  15.    
  16.     def e
  17.         #returns the value of 'e' passed in the initialize
  18.         return e
  19.     end
  20.    
  21.     def d
  22.         #returns the value of 'd' passed in the initialize
  23.         return d
  24.     end
  25.    
  26.     def gcd (e, euler)
  27.    
  28.         if e > euler
  29.             gcd = e
  30.         else
  31.             gcd = euler
  32.         end
  33.  
  34.         while gcd != 1 do
  35.             if(e%gcd == 0 && euler%gcd == 0)
  36.                 return gcd
  37.             else
  38.                 gcd = gcd - 1
  39.             end
  40.         end
  41.     end
  42.    
  43.     def new_key
  44.     #generates a new 'n','e' and 'd' values following the RSA algorithm. Returns a new array of three elements where the first element is 'n', the second is 'e' and the third is 'd'. Each time it     is called a new key must be returned.
  45.         q = Random.new
  46.         q.rand(10...99)
  47.         while Prime.prime(q) == true do
  48.             q.rand(10...99)
  49.         end
  50.                
  51.         p = Random.new
  52.         p.rand(10...99)
  53.         while Prime.prime(p) == true do
  54.             p.rand(10...99)
  55.         end
  56.         n = p*q
  57.         euler = (p-1).lcm(q-1)
  58.         e.rand(2...euler)
  59.         while gcd(e,euler) != 1 do
  60.             e.rand(2..euler)
  61.         end
  62.         d = Random.new
  63.         d.rand(1...1000)
  64.         while d*e%euler!=1 do
  65.             d.rand(1...1000)
  66.         end
  67.         array = Array.new(3)
  68.         array.push(n)
  69.         array.push(e)
  70.         array.push(d)
  71.         @n = n
  72.         @e = e
  73.         @d = d
  74.         return array
  75.        
  76.        
  77.     end
  78.    
  79.     def encrypt message
  80.     #encrypts the message passed. The message is of type string. Encrypts each symbol of this string by using its ASCII number representation and returns the encrypted message.
  81.        
  82.     end
  83.    
  84. def decrypt message
  85. #decrypts the message passed. The message is of type string. Decrypts each symbol of this string Encrypts each symbol of this string by using its ASCII number representationand returns the decrypted message.
  86. end
  87.  
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement