0% found this document useful (0 votes)
26 views1 page

Rsa

Uploaded by

naosinakhter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views1 page

Rsa

Uploaded by

naosinakhter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import Crypto.Util.

number

import sys

#bits = 100
bits = int(input("Enter 'number of bits' for primes: "))

if (len(sys.argv) > 1):


bits = int(sys.argv[1])

print("No of bits in prime is ", bits)

p = Crypto.Util.number.getPrime(bits,
randfunc=Crypto.Random.get_random_bytes)
print("\nRandom",bits,"bit Prime (p): ", p)

q = Crypto.Util.number.getPrime(bits,
randfunc=Crypto.Random.get_random_bytes)
print("\nRandom",bits,"bit Prime (q): ", q)

N = p * q
print("\n\nN = p*q =", N)

PHI = (p - 1) * (q - 1)
print("\nPHI = (p-1)(q-1)=", PHI)

e = 65537
print("\n\ne= ", e)
d = Crypto.Util.number.inverse(e, PHI)
print("\nd=", d)

decText = ""
text = input("\n\n\nEnter your text here: ")
textLen = len(text)
print("\n\n=== Let's try these keys ===\n")

i = 0
while i < textLen:
M = ord(text[i])
i += 1
enc = pow(M, e, N)
print("RSA Cipher(c=M^e mod N): ", enc)
dec = pow(enc, d, N)
print("RSA Decipher (c^d mod N): ", dec)
decText = decText + chr(dec)

print("\n============================")
print("\n\n\nDecrypted plaintext: ",decText)

You might also like