Created
October 1, 2024 18:14
-
-
Save Snawoot/eb7f583893a768b53d579249a617279f to your computer and use it in GitHub Desktop.
Utility which optimizes secret exponent of RSA key for faster power calculation
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 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