Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

RSA encryption and decryption

''' I have the RSA code, code works when we give input to encryption and take decryption output (plaintext and output are same), but when we encrypt plaintext separately and take its output and give it to decryption code as input separately, the output, is not the same as encryption input (plaintext and output are not same). Can someone please explain why is this happening? '''

code begins

import sympy from datetime import datetime from sage.all import ZZ, GF start_time = datetime.now() print("RSA Encryption and Decryption") print("Values of 'p' AND 'q' are as follows:") p = sympy.randprime(2, 100) #Input Prime Number print ("p = ", p) q = sympy.randprime(2, 100) #Input Prime Number print ("q = ", q) s = sympy.randprime(2, 100) #Input Prime Number print ("s = ", s)

n = p * q * s print("") print ("RSA Modulus,n = ",n) #RSA Modulus r = (p-1)(q-1)(s-1) #Eulers Toitent e = ZZ.random_element(r) while gcd(e, r) != 1: #GCD e = ZZ.random_element(r) #e Value Calculation print("") print ("Public Key = ", e) bezout = xgcd(e, r); d = Integer(mod(bezout[1], r)); #d, Private Key print("") print ("d =", d) print("") text = 'samu' print("") print ("Message, m = ", text) m = [ord(c) for c in text] c = [power_mod(i, e, n) for i in m] #Encryption print(c) cipher = [chr(a) for a in c] print ("Ciphertext, cipher =", cipher)

print('') '''

cipher = input("Enter Cipher text to be decrypted") #cipher input print('Cipher text to be decrypted :', cipher) ''' c = [ord(a) for a in cipher] #conversion of cipher text to the list of ascii values c

print('c =',c)

d = [power_mod(j, d, n) for j in c] #Decryption print('d =',d)

characters = [chr(ascii) for ascii in d] print("") print ("Decrypted text, d = ", ''.join(characters)) print("") print ("Plaintext/ Message, m = ", text) print ("Decrypted Message, d = ", ''.join(characters)) if(text == ''.join(characters)): print ("Thus we encrypted and decrypted text using RSA algorithm") else: print("Not sucessfull")

end_time = datetime.now() print('Time taken for execution of the program: {} '.format(end_time - start_time))

code ends

RSA encryption and decryption

''' I have the RSA code, code works when we give input to encryption and take decryption output (plaintext and output are same), but when we encrypt plaintext separately and take its output and give it to decryption code as input separately, the output, is not the same as encryption input (plaintext and output are not same). Can someone please explain why is this happening? '''

code begins

import sympy
from datetime import datetime
from sage.all import ZZ, GF
start_time = datetime.now()
print("RSA Encryption and Decryption")
print("Values of 'p' AND 'q' are as follows:")
p = sympy.randprime(2, 100)               #Input Prime Number
print ("p = ", p)
q = sympy.randprime(2, 100)               #Input Prime Number
print ("q = ", q)
s = sympy.randprime(2, 100)               #Input Prime Number
print ("s = ", s)

s) n = p * q * s print("") print ("RSA Modulus,n = ",n) #RSA Modulus r = (p-1)(q-1)(s-1) (p-1)*(q-1)*(s-1) #Eulers Toitent e = ZZ.random_element(r) while gcd(e, r) != 1: #GCD e = ZZ.random_element(r) #e Value Calculation print("") print ("Public Key = ", e) bezout = xgcd(e, r); d = Integer(mod(bezout[1], r)); #d, Private Key print("") print ("d =", d) print("") text = 'samu' print("") print ("Message, m = ", text) m = [ord(c) for c in text] c = [power_mod(i, e, n) for i in m] #Encryption print(c) cipher = [chr(a) for a in c] print ("Ciphertext, cipher =", cipher)

cipher) print('') '''

''' cipher = input("Enter Cipher text to be decrypted") #cipher input print('Cipher text to be decrypted :', cipher) ''' c = [ord(a) for a in cipher] #conversion of cipher text to the list of ascii values c

c print('c =',c)

=',c) d = [power_mod(j, d, n) for j in c] #Decryption print('d =',d)

=',d) characters = [chr(ascii) for ascii in d] print("") print ("Decrypted text, d = ", ''.join(characters)) print("") print ("Plaintext/ Message, m = ", text) print ("Decrypted Message, d = ", ''.join(characters)) if(text == ''.join(characters)): print ("Thus we encrypted and decrypted text using RSA algorithm") else: print("Not sucessfull")

sucessfull") end_time = datetime.now() print('Time taken for execution of the program: {} '.format(end_time - start_time))

code ends

start_time))