0

Please explain in easy language and with detailed explanation how can I parallelize the decrypt function loop and make the loop faster.

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: ")

This is my main code. Can someone please help in parallelizing the decrypt function loop and make the program faster? Since the decrypt function is calculating large numbers the calculation is really consuming a lot of time. Try to provide an easy explanation as I am a newbie in Python.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Why trying to "reinvent the wheel" again? Better use a library like Pycryptodome (they do have good examples that run "out of the box"): https://pypi.org/project/pycryptodome/ – Michael Fehr Jul 18 '21 at 10:14

1 Answers1

0

Most importantly, there are ways of speeding up modular exponentiation. Currently you are using exponentiation first and modulus later. This is similar to performing multiplication by adding a number to itself repeatedly, with the additional disadvantage that the result before the modulus becomes really big.

See here how to perform modular exponentiation in Python.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263