首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在写一个代码在python的RSA加密和解密字符串使用ascii代码,但解密算法的计算很慢。

我正在写一个代码在python的RSA加密和解密字符串使用ascii代码,但解密算法的计算很慢。
EN

Stack Overflow用户
提问于 2021-07-18 16:45:00
回答 1查看 178关注 0票数 0

请用简单的语言和详细的解释来解释我如何并行化解密函数循环并使循环更快。

代码语言:javascript
复制
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: ")

这是我的主代码。有没有人能帮我把decrypt函数循环并行化,让程序更快?由于decrypt函数正在计算大量数字,因此计算实际上消耗了大量的时间。尝试提供一个简单的解释,因为我是Python的新手。

EN

回答 1

Stack Overflow用户

发布于 2021-07-18 19:23:54

最重要的是,有一些方法可以加速模幂运算。目前,您先使用幂运算,然后再使用模数。这类似于通过重复地将数字添加到自身来执行乘法,但额外的缺点是,在模变为之前的结果真的很大。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68427453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档