Skip to content

Instantly share code, notes, and snippets.

@HarryZ10
Created March 31, 2024 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryZ10/c6f253411aa1d4fa010b3380f564e334 to your computer and use it in GitHub Desktop.
Save HarryZ10/c6f253411aa1d4fa010b3380f564e334 to your computer and use it in GitHub Desktop.
import sympy
def rsa_decrypt(ciphertext, d, n):
"""
Decrypts the RSA ciphertext using the private key (d, n).
Parameters:
- ciphertext (list): The ciphertext as a list of integers.
- d (int): The private exponent.
- n (int): The modulus.
"""
# Decrypt each ciphertext block using m = c^d mod n
plaintext = [pow(char, d, n) for char in ciphertext]
return ''.join(chr(char) for char in plaintext)
# Given parameters of the problem
ciphertext = [153, 75, 309, 310, 74, 203, 208, 401, 310, 371, 363, 451, 125]
n = 485 # The modulus
e = 53 # The public exponent
p = 5 # The smaller prime factor of n
q = 97 # The larger prime factor of n
# Calculate φ(n) = (p - 1)(q - 1)
totient = (p - 1) * (q - 1)
# Calculate the private exponent d using the mod_inverse function
d = sympy.mod_inverse(e, totient)
print(rsa_decrypt(ciphertext, d, n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment