Created
May 7, 2020 13:43
-
-
Save StoneLabs/7f70c1f78f90d3bcdb21aa07b1a66755 to your computer and use it in GitHub Desktop.
Decrypting RSA cipher using CRT in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
from Crypto.Util.number import long_to_bytes | |
import gmpy2 | |
# values given by private key | |
p = ... | |
q = ... | |
dp = ... | |
dq = ... | |
iq = gmpy2.invert(q, p) # or given by private key | |
# cypher | |
c = ... | |
# decryption | |
m1 = pow(c, dp, p) | |
m2 = pow(c, dq, q) | |
h = (iq * (m1 - m2)) % p | |
m = m2 + h * q | |
print(long_to_bytes(m)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment