import math
import random

def chkprime(a):
    if a%2==0 & a!=2:
        return False
    for i in range(3,a):
        if a%i==0:
            return False
    return True
        
    
def gen_prime(a,b):
    while True:
        x=random.randint(a,b)
        if chkprime(x):
            return x
def gcd(a,b):
    if a%b==0:
        return b
    else:
        return gcd(b,a%b)

def iscoprime(a,b):
    if gcd(a,b)==1:
        return True
    return False


def inv_mod(a,m):
    x=int(m/a)+1
    while True:
        if (a*x)%m==1:
            return x
        else:
            x=x+1

def key(a,b):
    p=gen_prime(a,b)
    while True:
        q=gen_prime(a,b)
        if p!=q:
            break

    n=p*q
    t=(p-1)*(q-1)

    while True:
        e=random.randint(2,t)
        if iscoprime(t,e):
            break

    d=inv_mod(e,t)
    return n,e,d
    
def encrypt(x,n,e):
    return (x**e)%n

def decrypt(x,d,n):
    return (x**d)%n

def funct():
    n,e,d=key(50,70)

    c=encrypt(86,n,e)                       #encrypt(msg,n,e)...here 86 as example..replace with ascii
    d=decrypt(c,d,n)

    print c,d


funct()

@@ -1,12 +1,120 @@
+#NITK-IEEE SaS: Cryptography project%0A#Natasha Y Jeppu%0A#Implementation of simple RSA cryptosystem in python%0A%0A
 import math%0A
@@ -559,16 +559,76 @@
  False%0A%0A
+#function computes inverse modulus: returns pvt key exponent
 %0Adef inv
@@ -748,16 +748,53 @@
  x=x+1%0A%0A
+#to generate public and private keys%0A
 def key(
@@ -892,24 +892,88 @@
       break%0A
+    %0A    #product of euler-totient of primes to serve as modulus
 %0A    n=p*q%0A 
@@ -1097,24 +1097,86 @@
 nv_mod(e,t)%0A
+    %0A    #e is public key exponent, d is private key exponent%0A
     return n
@@ -1263,24 +1263,79 @@
 n (x**d)%25n%0A%0A
+%0A#Function to implement rsa encryption and decryption %0A
 def funct():
@@ -1331,16 +1331,20 @@
 ef funct
+_rsa
 ():%0A    
@@ -1361,16 +1361,141 @@
 50,70)%0A%0A
+    #encrypt(ascii of plaintext,modulus for the keys,public key exponent)%0A    #taking 86 as an example to chk implementation%0A
     c=en
@@ -1518,28 +1518,25 @@
             
-    
+%0A
     #encrypt
@@ -1540,79 +1540,117 @@
 ypt(
-msg,n,e)...here 86 as example..replace with ascii%0A    d=decrypt(c,d,n)%0A
+ciphertext,private key exponent,modulus for the keys)%0A    d=decrypt(c,d,n)%0A%0A    #display cipher and plaintext
 %0A   
@@ -1662,16 +1662,21 @@
 t c,d%0A%0A%0A
+%0A
 funct
+_rsa
 ()%0A

Update natasha_rsa.py