📌  相关文章
📜  在线将python代码转换为c++ - Python代码示例

📅  最后修改于: 2022-03-11 14:45:56.715000             🧑  作者: Mango

代码示例10
import math
 
print("RSA Encryption & Decryption")
 
#Input Prime Numbers
print("\nEnter your 'p' and 'q' values:")
p = int(input("Enter a prime number for p: "))
q = int(input("Enter a prime number for q: "))

 
def isPrime(a):
    if(a==2):
        return True
    elif((a<2) or ((a%2)==0)):
        return False
    elif(a>2):
        for i in range(2,a):
            if not(a%i):
                return false
    return True
 
check_p = isPrime(p)
check_q = isPrime(q)
while(((check_p==False)or(check_q==False))):
    p = int(input("Enter a prime number for p: "))
    q = int(input("Enter a prime number for q: "))
    check_p = isPrime(p)
    check_q = isPrime(q)
 

n = p * q

print("\nRSA Modulus(n) is:",n)
 
#Eulers Toitent
'''CALCULATION OF EULERS TOITENT 'r'.'''
r= (p-1)*(q-1)
print("Eulers Toitent(r) is:",r)

 
#GCD
'''CALCULATION OF GCD FOR 'e' CALCULATION.'''
def egcd(e,r):
    while(r!=0):
        e,r=r,e%r
    return e
 
#Euclid's Algorithm
def eugcd(e,r):
    for i in range(1,r):
        while(e!=0):
            a,b=r//e,r%e
            if(b!=0):
                print("%d = %d*(%d) + %d"%(r,a,e,b))
            r=e
            e=b
 
#Extended Euclidean Algorithm
def eea(a,b):
    if(a%b==0):
        return(b,0,1)
    else:
        gcd,s,t = eea(b,a%b)
        s = s-((a//b) * t)
        print("%d = %d*(%d) + (%d)*(%d)"%(gcd,a,t,s,b))
        return(gcd,t,s)
 
#Multiplicative Inverse
def mult_inv(e,r):
    gcd,s,_=eea(e,r)
    if(gcd!=1):
        return None
    else:
        if(s<0):
            print("s=%d. Since %d is less than 0, s = s(modr), i.e., s=%d."%(s,s,s%r))
        elif(s>0):
            print("s=%d."%(s))
        return s%r
 
#e Value Calculation
'''FINDS THE HIGHEST POSSIBLE VALUE OF 'e' BETWEEN 1 and 1000 THAT MAKES (e,r) COPRIME.'''
for i in range(1,1000):
    if(egcd(i,r)==1):
        e=i
print("The value of e is:",e)

 
#d, Private and Public Keys
'''CALCULATION OF 'd', PRIVATE KEY, AND PUBLIC KEY.'''
print("\nEuclidean Algorithm:")
eugcd(e,r)


print("\nExtended Euclidean Algorithm:")
d = mult_inv(e,r)

print("The value of d is:",d)

public = (e,n)
private = (d,n)
print("\nPrivate Key is:",private)
print("Public Key is:",public)

 
#Encryption
'''ENCRYPTION ALGORITHM.'''
def encrypt(pub_key,n_text):
    e,n=pub_key
    x=[]
    m=0
    for i in n_text:
        if(i.isupper()):
            m = ord(i)-65
            c=(m**e)%n
            x.append(c)
        elif(i.islower()):               
            m= ord(i)-97
            c=(m**e)%n
            x.append(c)
        elif(i.isspace()):
            spc=400
            x.append(400)
    return x
     
 
#Decryption
'''DECRYPTION ALGORITHM'''
def decrypt(priv_key,c_text):
    d,n=priv_key
    txt=c_text.split(',')
    x=''
    m=0
    for i in txt:
        if(i=='400'):
            x+=' '
        else:
            m=(int(i)**d)%n
            m+=65
            c=chr(m)
            x+=c
    return x
 
#Message
message = input("\nWhat would you like encrypted or decrypted? If you choose to encrpyt please write your message. If you choose to decrypt please separate numbers with ',' (Do not add a space after typing the comma)):")

 
#Choose Encrypt or Decrypt and Print
choose = input("Type 'e' for encryption and 'd' for decrytion.")
if(choose=='e'):
    enc_msg=encrypt(public,message)
    print("Your encrypted message is:",enc_msg)
elif(choose=='d'):
    print("Your decrypted message is:",decrypt(private,message))
else:
    print("You entered the wrong option.")