ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 10 May 2021 01:45:34 +0200RSA encryption and decryptionhttps://ask.sagemath.org/question/56913/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? '''
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))Mon, 03 May 2021 06:17:38 +0200https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/Comment by Max Alekseyev for <p>''' 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? '''</p>
<pre><code>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></pre>
https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=57048#post-id-57048Can you illustrate this discrepancy with an actual example?Mon, 10 May 2021 01:45:34 +0200https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=57048#post-id-57048Comment by moreofsaaamu for <p>''' 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? '''</p>
<pre><code>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></pre>
https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=57005#post-id-57005yes code works but if you encrypt separately, and decrypt its output separately what you get is not plaintext.Fri, 07 May 2021 08:35:05 +0200https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=57005#post-id-57005Comment by Max Alekseyev for <p>''' 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? '''</p>
<pre><code>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></pre>
https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=56948#post-id-56948The code works fine for me. What is your concern?Tue, 04 May 2021 16:11:49 +0200https://ask.sagemath.org/question/56913/rsa-encryption-and-decryption/?comment=56948#post-id-56948