本文整理汇总了Python中cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key方法的典型用法代码示例。如果您正苦于以下问题:Python rsa.generate_private_key方法的具体用法?Python rsa.generate_private_key怎么用?Python rsa.generate_private_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cryptography.hazmat.primitives.asymmetric.rsa
的用法示例。
在下文中一共展示了rsa.generate_private_key方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_key_pair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_key_pair(key_length):
private_key = rsa.generate_private_key(backend=default_backend(),
public_exponent=65537,
key_size=key_length)
private_key_der = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption())
public_key_pem = private_key.public_key().public_bytes(
serialization.Encoding.PEM,
serialization.PublicFormat.SubjectPublicKeyInfo) \
.decode("utf-8")
# strip off header
public_key_der_encoded = ''.join(public_key_pem.split('\n')[1:-2])
return private_key_der, public_key_der_encoded
示例2: generate_ssh_keypair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_ssh_keypair():
key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.TraditionalOpenSSL,
crypto_serialization.NoEncryption()
).decode("utf-8")
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH
).decode("utf-8")
return (public_key, private_key)
示例3: generate_ssh_keypair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_ssh_keypair() -> Tuple[str, str]:
'''
Generate RSA keypair for SSH/SFTP connection.
'''
key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=2048
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.TraditionalOpenSSL,
crypto_serialization.NoEncryption()
).decode("utf-8")
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH
).decode("utf-8")
return (public_key, private_key)
示例4: keygen
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def keygen():
"""
Generates a keypair using the cryptography lib and returns a tuple (public, private)
"""
key = rsa.generate_private_key(public_exponent=65537, key_size=2048,
backend=default_backend())
private = key.private_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
).decode()
public = key.public_key().public_bytes(encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH
).decode()
return (public, private)
示例5: generate_csr
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_csr(common_name, dnsnames, ips, keysize):
key = rsa.generate_private_key(
public_exponent=65537,
key_size=keysize,
backend=default_backend()
)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
csr = x509.CertificateSigningRequestBuilder()
csr = csr.subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, common_name)]))
csr = csr.add_extension(
x509.SubjectAlternativeName(dnsnames + ips),
critical=False,
)
csr = csr.sign(key, hashes.SHA256(), default_backend())
csr_pem = csr.public_bytes(serialization.Encoding.PEM)
return key_pem, csr_pem
示例6: create_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def create_key(key_path: str) -> rsa.RSAPrivateKey:
key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()
)
with open(key_path, "wb") as filehandle:
filehandle.write(
key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
)
return key
示例7: _get_raw_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def _get_raw_key(self, key_id):
"""Retrieves a static, randomly generated, RSA key for the specified key id.
:param str key_id: User-defined ID for the static key
:returns: Wrapping key that contains the specified static key
:rtype: :class:`aws_encryption_sdk.internal.crypto.WrappingKey`
"""
try:
static_key = self._static_keys[key_id]
except KeyError:
private_key = rsa.generate_private_key(public_exponent=65537, key_size=4096, backend=default_backend())
static_key = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
self._static_keys[key_id] = static_key
return WrappingKey(
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA1_MGF1,
wrapping_key=static_key,
wrapping_key_type=EncryptionKeyType.PRIVATE,
)
开发者ID:aws,项目名称:aws-encryption-sdk-python,代码行数:24,代码来源:basic_file_encryption_with_multiple_providers.py
示例8: generate_keypair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_keypair(rsa_keysize=2048):
"""
This creates an RSA key pair
# TODO: The HSM should be used.
The public key and private keys are returned in PKCS#1 Format.
:return: tuple of (pubkey, privkey)
"""
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=rsa_keysize,
backend=default_backend()
)
public_key = private_key.public_key()
pem_priv = to_unicode(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()))
pem_pub = to_unicode(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.PKCS1))
return pem_pub, pem_priv
示例9: generate_key_pair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_key_pair():
"""
This method generates a keypair and returns it as a tuple
of (public, private) keys.
The public key format is OpenSSH and private key format is PEM.
"""
key_pair = rsa.generate_private_key(
backend=default_backend(),
public_exponent=65537,
key_size=2048)
private_key = key_pair.private_bytes(
crypt_serialization.Encoding.PEM,
crypt_serialization.PrivateFormat.PKCS8,
crypt_serialization.NoEncryption()).decode('utf-8')
public_key = key_pair.public_key().public_bytes(
crypt_serialization.Encoding.OpenSSH,
crypt_serialization.PublicFormat.OpenSSH).decode('utf-8')
return public_key, private_key
示例10: generate_signing_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generate_signing_key(args):
if os.path.exists(args.keyfile):
raise esptool.FatalError("ERROR: Key file %s already exists" % args.keyfile)
if args.version == "1":
""" Generate an ECDSA signing key for signing secure boot images (post-bootloader) """
sk = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
with open(args.keyfile, "wb") as f:
f.write(sk.to_pem())
print("ECDSA NIST256p private key in PEM format written to %s" % args.keyfile)
elif args.version == "2":
""" Generate a RSA 3072 signing key for signing secure boot images """
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=3072,
backend=default_backend()
).private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
).decode()
with open(args.keyfile, "wb") as f:
f.write(private_key)
print("RSA 3072 private key in PEM format written to %s" % args.keyfile)
示例11: __init__
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def __init__(self, *args, **kwargs):
super(PluginIOTestMixin, self).__init__(*args, **kwargs)
raw_key = gen_pkey(1024)
self.all_data = IOPlugin.Data(
account_key=jose.JWKRSA(key=rsa.generate_private_key(
public_exponent=65537, key_size=1024,
backend=default_backend(),
)),
key=ComparablePKey(raw_key),
cert=jose.ComparableX509(crypto_util.gen_ss_cert(raw_key, ['a'])),
chain=[
jose.ComparableX509(crypto_util.gen_ss_cert(raw_key, ['b'])),
jose.ComparableX509(crypto_util.gen_ss_cert(raw_key, ['c'])),
],
)
self.key_data = IOPlugin.EMPTY_DATA._replace(key=self.all_data.key)
示例12: fill_and_store
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
pubExp = pubExp or 65537
if not modulus:
real_modulusLen = modulusLen or 2048
private_key = rsa.generate_private_key(public_exponent=pubExp,
key_size=real_modulusLen,
backend=default_backend())
self.pubkey = private_key.public_key()
else:
real_modulusLen = len(binrepr(modulus))
if modulusLen and real_modulusLen != modulusLen:
warning("modulus and modulusLen do not match!")
pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
self.pubkey = pubNum.public_key(default_backend())
# Lines below are only useful for the legacy part of pkcs1.py
pubNum = self.pubkey.public_numbers()
self._modulusLen = real_modulusLen
self._modulus = pubNum.n
self._pubExp = pubNum.e
示例13: create_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def create_key(self):
key = rsa.generate_private_key(
backend=crypto_default_backend(),
public_exponent=65537,
key_size=self.get("KeySize"),
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
self.key_format,
crypto_serialization.NoEncryption(),
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)
return private_key.decode("ascii"), public_key.decode("ascii")
示例14: __init__
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def __init__(self):
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import (
default_backend as crypto_default_backend,
)
self.key = rsa.generate_private_key(
backend=crypto_default_backend(), public_exponent=65537, key_size=2048
)
self.private_key = self.key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
).decode("ascii")
self.public_key = (
self.key.public_key()
.public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)
.decode("ascii")
)
示例15: generateCpiRsaKeys
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key [as 别名]
def generateCpiRsaKeys():
"""Generate a private/public RSA 2048 key pair.
Returns:
A tuple (private_key, public key) as PEM string encoded.
"""
rsa_key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend())
rsa_private_key = rsa_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption())
rsa_public_key = rsa_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo)
return rsa_private_key, rsa_public_key