The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
RSA Code
By Guest on 20th January 2023 09:38:51 AM | Syntax: PYTHON | Views: 9



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. import math
  2.  
  3. p = 3
  4. q = 11
  5.  
  6. def prime_check(a):
  7.     if(a==2):
  8.         return True
  9.     elif((a<2) or ((a%2)==0)):
  10.         return False
  11.     elif(a>2):
  12.         for i in range(2,a):
  13.             if not(a%i):
  14.                 return false
  15.     return True
  16.  
  17. check_p = prime_check(p)
  18. check_q = prime_check(q)
  19. while(((check_p==False)or(check_q==False))):
  20.     check_p = prime_check(p)
  21.     check_q = prime_check(q)
  22.  
  23. n = p * q
  24. r= (p-1)*(q-1)
  25.  
  26. #GCD
  27. def egcd(e,r):
  28.     while(r!=0):
  29.         e,r=r,e%r
  30.     return e
  31.  
  32. #Euclid's Algorithm
  33. def eugcd(e,r):
  34.     for i in range(1,r):
  35.         while(e!=0):
  36.             a,b=r//e,r%e
  37.             r=e
  38.             e=b
  39.  
  40. #Extended Euclidean Algorithm
  41. def eea(a,b):
  42.     if(a%b==0):
  43.         return(b,0,1)
  44.     else:
  45.         gcd,s,t = eea(b,a%b)
  46.         s = s-((a//b) * t)
  47.         return(gcd,t,s)
  48.  
  49. #Multiplicative Inverse
  50. def mult_inv(e,r):
  51.     gcd,s,_=eea(e,r)
  52.     if(gcd!=1):
  53.         return None
  54.     else:
  55.         return s%r
  56.  
  57. #e Value Calculation
  58. for i in range(1,1000):
  59.     if(egcd(i,r)==1):
  60.         e=i
  61. eugcd(e,r)
  62. d = mult_inv(e,r)
  63.  
  64. public = (e,n)
  65. private = (d,n)
  66.  
  67. #Encryption
  68. def encrypt(pub_key,n_text):
  69.     e,n=pub_key
  70.     x=[]
  71.     m=0
  72.     for i in n_text:
  73.         if(i.isupper()):
  74.             m = ord(i)-65
  75.             c=(m**e)%n
  76.             x.append(c)
  77.         elif(i.islower()):              
  78.             m= ord(i)-97
  79.             c=(m**e)%n
  80.             x.append(c)
  81.         elif(i.isspace()):
  82.             spc=400
  83.             x.append(400)
  84.     return x
  85.      
  86. #Decryption
  87. def decrypt(priv_key,c_text):
  88.     d,n=priv_key
  89.     txt=c_text.split(',')
  90.     x=''
  91.     m=0
  92.     for i in txt:
  93.         if(i=='400'):
  94.             x+=' '
  95.         else:
  96.             m=(int(i)**d)%n
  97.             m+=65
  98.             c=chr(m)
  99.             x+=c
  100.     return x
  101.  
  102. #Message
  103. message = input("What would you like encrypted or decrypted?(Separate numbers with ',' for decryption):")
  104. choose = input("Type '1' for encryption and '2' for decrytion.")
  105. if(choose=='1'):
  106.     enc_msg=encrypt(public,message)
  107. elif(choose=='2'):
  108.     print("Your decrypted message is: ",decrypt(private,message))
  109. else:
  110.     print("You entered the wrong option.")



  • Recent Pastes
  • Questions
    13 hours 53 mins 37 secs ago
  • Adrs
    13 hours 58 mins 47 secs ago
  • Docs
    13 hours 59 mins 31 secs ago
  • Servs
    14 hours 14 secs ago
  • Srvs
    14 hours 5 mins 3 secs ago