- Install
cryptography
package from the command line:
pip install cryptography
- Generate PEM-encoded PKCS#1 format RSA key pair:
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
keySize = 2048
key = rsa.generate_private_key(public_exponent=65537, key_size=keySize)
privateKey = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.TraditionalOpenSSL,
crypto_serialization.NoEncryption()
)
privateKey = privateKey.decode('utf-8')
publicKey = key.public_key().public_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PublicFormat.PKCS1
)
publicKey = publicKey.decode('utf-8')
print(privateKey)
print(publicKey)
Leave a Comment
Cancel reply