Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Created October 1, 2024 18:14
Show Gist options
  • Save Snawoot/eb7f583893a768b53d579249a617279f to your computer and use it in GitHub Desktop.
Save Snawoot/eb7f583893a768b53d579249a617279f to your computer and use it in GitHub Desktop.
Utility which optimizes secret exponent of RSA key for faster power calculation
#!/usr/bin/env python
import sys
from Crypto.PublicKey import RSA
from fractions import gcd
if __name__ == '__main__':
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: %s <RSA privatekey file>" % sys.argv[0]
exit(2)
with open(sys.argv[1]) as f:
key = RSA.importKey(f.read())
if not key.has_private():
print >> sys.stderr, "Private key required"
exit(1)
carmichael = (key.p - 1) * (key.q - 1) / gcd(key.p - 1, key.q - 1)
key.d = key.d % carmichael
print key.exportKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment