Skip to content

Instantly share code, notes, and snippets.

@StoneLabs
Created May 7, 2020 13:43
Show Gist options
  • Save StoneLabs/7f70c1f78f90d3bcdb21aa07b1a66755 to your computer and use it in GitHub Desktop.
Save StoneLabs/7f70c1f78f90d3bcdb21aa07b1a66755 to your computer and use it in GitHub Desktop.
Decrypting RSA cipher using CRT in python
#!/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