Last active
February 15, 2021 07:53
-
-
Save przemos/2c7842a5679931ba9ff33c45b14a36cb to your computer and use it in GitHub Desktop.
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
# Ensure the libraries below | |
# pip install cryptography | |
# pip install pycrypto | |
from Crypto.PublicKey import RSA | |
import base64 | |
from binascii import unhexlify | |
import sys | |
def long_to_bytes (val): | |
""" | |
Use :ref:`string formatting` and :func:`~binascii.unhexlify` to | |
convert ``val``, a :func:`long`, to a byte :func:`str`. | |
:param long val: The value to pack | |
Using :ref:`string formatting` lets us use Python's C innards. | |
""" | |
# one (1) hex digit per four (4) bits | |
width = val.bit_length() | |
# unhexlify wants an even multiple of eight (8) bits, but we don't | |
# want more digits than we need (hence the ternary-ish 'or') | |
width += 8 - ((width % 8) or 8) | |
# format width specifier: four (4) bits per hex digit | |
fmt = '%%0%dx' % (width // 4) | |
# prepend zero (0) to the width, to zero-pad the output | |
s = unhexlify(fmt % val) | |
return s | |
key_encoded = "".join(sys.stdin.readlines()) | |
pubkey = RSA.importKey(key_encoded) | |
print("Modulus:") | |
print(base64.b64encode(long_to_bytes(pubkey.n)).decode()) | |
print("Exponent:") | |
print(base64.b64encode(long_to_bytes(pubkey.e)).decode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment