【问题标题】:I am writing a code in python for RSA encryption and decryption for string using ascii code, but the calculation for decryption algorithm is slow我正在用python编写代码,用于使用ascii代码对字符串进行RSA加密和解密,但解密算法的计算速度很慢
【发布时间】:2021-09-26 08:20:18
【问题描述】:

请用通俗易懂的语言详细解释如何并行化解密函数循环并使循环更快。

import math

def gcd(m,n): 
    if n==0: 
        return m 
    else: 
        return gcd(n,m%n) 

def encrypt(ascii_initial, e, n):
    ascii_final = []
    for i in ascii_initial:
        C1= pow(i,e)
        C=C1%n
        print("value of C is:",C)  
        ascii_final.append(C)
    return ascii_final
def decrypt(ascii_initial, d, n):
    ascii_final = []
    for j in ascii_initial:
    #decryption
        P1=pow(j,d)
        P=P1%n
        ascii_final.append(P)
        print("value of P is:",P)  
    #returning final list
    return ascii_final   
def rsa_algorithm (direction, ascii_initial):
    p,q,r,s = 13, 17, 61, 37
    

n= p*q*r*s
print("RSA Modulus(n) is:",n)

# pub_key = int(input("Enter a starting value for public key generation "))
f_of_n = (p - 1) * (q - 1) * (r-1) * (s-1)
# print("Eulers Toitent(r) is:",f_of_n)
#gcd
for e in range(2,f_of_n): 
    if gcd(e,f_of_n)== 1: 
        break

for i in range (1,f_of_n):
    d= 1+f_of_n *i/e
    if d % e==0:
        d = int(d/e) 
        break
    
print("value of d is:",d)  

#public key generation

# decalaring empty list
# ascii_final = []
#Cypher text=Encryption
#checking whether to encode or decode

if direction =="encode":
    ascii_final =  encrypt(ascii_initial,e,n)

elif direction == "decode":
    ascii_final = decrypt(ascii_initial, d, n)

return ascii_final
cont = 'yes'
while cont.lower() == 'yes':
#checking whether user wants encoding or decoding
    u_direction=input("Type 'encode' for encrypting and 'decode' for decrypting: ")
    if u_direction == 'encode':
               
    user_entered_string = input(f"Enter the String to be {u_direction}d: ")
    user_entered_string = user_entered_string
    #converting into the ascii code dec
    ascii = []
    for letter in user_entered_string:
        ascii.append(ord(letter))
    print(ascii)
    #calling our rsa_algorithm function
    resultant_ascii = rsa_algorithm(u_direction,ascii)

    print(f"your encode ascii list is {resultant_ascii}")





if u_direction == 'decode':
    input_string = input('Enter elements of a list separated by and , space')
    print("\n")
    ascii = input_string.split(', ')
    # print list
    print('list: ', ascii)

    # convert each item to int type
    for i in range(len(ascii)):
        # convert each item to int type
        ascii[i] = int(ascii[i])

    resultant_ascii = []
    resultant_ascii = (rsa_algorithm(u_direction,ascii))
    print(resultant_ascii)
    final = resultant_ascii[0]
    s = ''.join(chr(i) for i in resultant_ascii)
    print(s)
cont = input("Do you want to continue type 'yes' to continue or 'no' to stop: ")

这是我的主要代码。有人可以帮助并行化解密函数循环并使程序更快吗?由于解密函数正在计算大量数字,因此计算确实非常耗时。尝试提供一个简单的解释,因为我是 Python 新手。

【问题讨论】:

  • 为什么要再次“重新发明轮子”?最好使用像 Pycryptodome 这样的库(他们确实有“开箱即用”的好例子):pypi.org/project/pycryptodome

标签: python performance concurrency parallel-processing rsa


【解决方案1】:

最重要的是,有一些方法可以加快模幂运算。目前,您首先使用取幂,然后使用取模。这类似于通过重复将一个数字添加到自身来执行乘法,另外一个缺点是模数之前的结果变得非常很大。

请参阅here 如何在 Python 中执行模幂运算。

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 2021-11-26
    • 2013-04-17
    • 1970-01-01
    • 2021-03-07
    • 2014-09-14
    • 2018-03-10
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多