Wednesday, 9 January 2019

Python program to implement RSA(Rivest, Shamir, & Adleman) algorithm (CSS)

class RSA:
    def __init__(self, p=13, q=17, e=19):
        self.p = p
        self.q = q
        self.n = p*q
        self.phi_n = (p-1)*(q-1)
        self.e = e
        self.d = int(self.find_d())
        self.message = None
        self.cipher = None

    def find_d(self):
        for i in range(1, 1000):
            if ((self.phi_n*i)+1) % self.e == 0:
                return (((self.phi_n*i)+1) / self.e)

    def encrypt(self):
        self.cipher = (self.message**self.e) % self.n

    def destroy_message(self):
        self.message = None

    def decrypt(self):
        self.message = (self.cipher**self.d) % self.n


if __name__ == "__main__":
    p, q, e = input("Enter the values for p q e : ").split(' ')
    rsa = RSA(int(p), int(q), int(e))
    message = int(input("Enter Your message to be encrypted(ONLY INT): "))
    rsa.message = message
    rsa.encrypt()
    rsa.destroy_message()
    print("The encrypted value is :", rsa.cipher)
    rsa.decrypt()
    print("The decrypted value is :", rsa.message)

Output:

codept@codept-04 ~/Desktop/chan $ python3 RSA.py
Enter the values for p q e : 73 151 11
Enter Your message to be encrypted(ONLY INT): 23
The encrypted value is : 5281
The decrypted value is : 23

No comments:

Post a Comment