本文整理汇总了Python中cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicNumbers方法的典型用法代码示例。如果您正苦于以下问题:Python rsa.RSAPublicNumbers方法的具体用法?Python rsa.RSAPublicNumbers怎么用?Python rsa.RSAPublicNumbers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cryptography.hazmat.primitives.asymmetric.rsa
的用法示例。
在下文中一共展示了rsa.RSAPublicNumbers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fill_and_store
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [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
示例2: gen_rsa_keypair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def gen_rsa_keypair(public_exponent, key_size, backend):
# for a pasing test against mocked results, we need the same key
# for each run
# private key numbers
d = 21617696977064831737076881102083866512588021127782856037972563073160418492784722157229048881139551797965254106994729989171004895438848876105403145818526588448619012753150859908505079457128264842711189718538434996465875355972895683414261775421352553376878242391757860294122032733780201193753125763293334069627997366690578378643268496972715558088736335242616446993490560907175346811363459760219154246765368454269083276034064771683561116425318584728938420495690895295417261302673227504350398337950587166908640861984752151206832712112769733009288056773792011545187027621461752928443722277452545442134981026071169761569129
dmp1 = 129490813503009185789974379255770561425157574567257502592607367628590256916193790244618544180042259447193585640234142495635548617466553819050739804131538251753363069954266652888546939369520788464015077722316584767752481491358983245279072645160828476778216654224017198857901235711822454725809127201111535934753
dmq1 = 48648800397834409354931458481260042874431001732145738308246185682766008281764239801132482123299506611633669689538582946696791307965087683313609603490735622965494394760749284568774366017328170855872209094830510839953297302962573968041079091285470051311421587284299491265676844471173504726139132522742725727613
iqmp = 39631089624266744377721024140775526581242717587318543319284349361749959043577498327792652579346928867008103676271384098405811933888254575054903735904994321302658687548878487654303384553639708412577672820397125886631056454780398875240593971733447420281054575014889376749144359968455995345232769795875325417317
p = 174331130742537243408330955079815843963035167715989214203198624738597363753422194561659111132445142920926993058833709875440980890363712769908141629896643845627776407469038077862709547359496382776626050712560846670587394813048350142683947903572416102765283173128172045224943986952459084128294017448217886823253
q = 141639112913055250372907285405879139487409354087944702421480687298597773645578986399236760591500655922107321654521995138945695777437048045372013886449237695348272152351631535441304559114954968164429007883070066144736528002854651716726263900736377199146896337887655964419309789253989248300176824884184363216819
# public key numbers
e = 65537
n = 24692106711502830011203227021058444318027801046612842012663747949974978593541529463344548800446917862266219189049856550539417324579114258210080798360122994007305091566363663241781504651372764226027210216355916383975880112316706422502404691353489765771310270171473497918954906308690817673196552680498374521519566949221302301125182104198985782657283395055909134373469597836420671163965867038455758131344733786842283328454828820406016508955409107145350345035248825315933976893356673777910251028486191789752627573225093968284278302684745743589192378470115772764709506475265246795419324395050366115533203601201395969892207
public_numbers = rsa.RSAPublicNumbers(e, n)
numbers = rsa.RSAPrivateNumbers(p, q, d, dmp1, dmq1, iqmp,
public_numbers)
key = default_backend().load_rsa_private_numbers(numbers)
return key
示例3: get_rsa_public_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def get_rsa_public_key(n, e):
"""
Retrieve an RSA public key based on a module and exponent as provided by the JWKS format.
:param n:
:param e:
:return: a RSA Public Key in PEM format
"""
n = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(n, "utf-8"))), 16)
e = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(e, "utf-8"))), 16)
pub = RSAPublicNumbers(e, n).public_key(default_backend())
return pub.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
示例4: __init__
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def __init__(self, msg=None, data=None, filename=None, password=None, key=None, file_obj=None):
self.key = None
if file_obj is not None:
self._from_private_key(file_obj, password)
return
if filename is not None:
self._from_private_key_file(filename, password)
return
if (msg is None) and (data is not None):
msg = Message(data)
if key is not None:
self.key = key
else:
if msg is None:
raise SSHException('Key object may not be empty')
if msg.get_text() != 'ssh-rsa':
raise SSHException('Invalid key')
self.key = rsa.RSAPublicNumbers(
e=msg.get_mpint(), n=msg.get_mpint()
).public_key(default_backend())
示例5: verify
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def verify(signed, public_key):
data = base642bytes(signed.data)
signature = base642bytes(signed.signature)
key = RSAPublicNumbers(
public_key.e,
base642int(public_key.n)
).public_key(BACKEND)
key.verify(
signature=signature,
data=data,
padding=PADDING.get(signed.padding, PSS),
algorithm=ALGORITHM.get(signed.algorithm, SHA256),
)
return json2value(data.decode("utf8"))
示例6: _get_rsa_parameters
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _get_rsa_parameters(server_info: ServerConnectivityInfo, openssl_cipher_string: str) -> Optional[RSAPublicNumbers]:
ssl_connection = server_info.get_preconfigured_tls_connection()
ssl_connection.ssl_client.set_cipher_list(openssl_cipher_string)
parsed_cert = None
try:
# Perform the SSL handshake
ssl_connection.connect()
cert_as_pem = ssl_connection.ssl_client.get_received_chain()[0]
parsed_cert = load_pem_x509_certificate(cert_as_pem.encode("ascii"), backend=default_backend())
except ServerRejectedTlsHandshake:
# Server does not support RSA cipher suites?
pass
except ClientCertificateRequested:
# AD: The server asked for a client cert. We could still retrieve the server certificate, but it is unclear
# to me if the ROBOT check is supposed to work even if we do not provide a client cert. My guess is that
# it should not work since it requires completing a full handshake, which we can't without a client cert.
# Hence, propagate the error to make the check fail.
raise
finally:
ssl_connection.close()
if parsed_cert:
public_key = parsed_cert.public_key()
if isinstance(public_key, RSAPublicKey):
return public_key.public_numbers()
else:
return None
else:
return None
示例7: _rsa_pub
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _rsa_pub(self, k):
return rsa.RSAPublicNumbers(self._decode_int(k['e']),
self._decode_int(k['n']))
示例8: _rsa_construct
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _rsa_construct(exp, mod):
return RSAPublicNumbers(exp, mod).public_key(default_backend())
示例9: _load_ssh_rsa_public_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _load_ssh_rsa_public_key(key_type, decoded_data, backend):
e, rest = _read_next_mpint(decoded_data)
n, rest = _read_next_mpint(rest)
if rest:
raise ValueError('Key body contains extra bytes.')
return rsa.RSAPublicNumbers(e, n).public_key(backend)
示例10: default_classic
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def default_classic(self, o):
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers
if isinstance(o, set):
return list(o)
elif isinstance(o, RSAPublicNumbers):
return {'n': o.n, 'e': o.e}
else:
return super(AutoJSONEncoder, self).default(o)
示例11: _load_ssh_rsa_public_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _load_ssh_rsa_public_key(key_type, decoded_data, backend):
e, rest = _ssh_read_next_mpint(decoded_data)
n, rest = _ssh_read_next_mpint(rest)
if rest:
raise ValueError('Key body contains extra bytes.')
return rsa.RSAPublicNumbers(e, n).public_key(backend)
示例12: create_pubkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def create_pubkey(pubkey_info):
"""
<Purpose>
Create and return an RSAPublicKey object from the passed pubkey_info
using pyca/cryptography.
<Arguments>
pubkey_info:
The RSA pubkey info dictionary as specified by
securesystemslib.formats.GPG_RSA_PUBKEY_SCHEMA
<Exceptions>
securesystemslib.exceptions.FormatError if
pubkey_info does not match securesystemslib.formats.GPG_RSA_PUBKEY_SCHEMA
securesystemslib.exceptions.UnsupportedLibraryError if
the cryptography module is unavailable
<Returns>
A cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey based on the
passed pubkey_info.
"""
if not CRYPTO: # pragma: no cover
raise securesystemslib.exceptions.UnsupportedLibraryError(NO_CRYPTO_MSG)
securesystemslib.formats.GPG_RSA_PUBKEY_SCHEMA.check_match(pubkey_info)
e = int(pubkey_info['keyval']['public']['e'], 16)
n = int(pubkey_info['keyval']['public']['n'], 16)
pubkey = rsa.RSAPublicNumbers(e, n).public_key(backends.default_backend())
return pubkey
示例13: _process_ssh_rsa
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def _process_ssh_rsa(self, data):
"""Parses ssh-rsa public keys."""
current_position, raw_e = self._unpack_by_int(data, 0)
current_position, raw_n = self._unpack_by_int(data, current_position)
unpacked_e = self._parse_long(raw_e)
unpacked_n = self._parse_long(raw_n)
self.rsa = RSAPublicNumbers(unpacked_e, unpacked_n).public_key(default_backend())
self.bits = self.rsa.key_size
if self.strict_mode:
min_length = self.RSA_MIN_LENGTH_STRICT
max_length = self.RSA_MAX_LENGTH_STRICT
else:
min_length = self.RSA_MIN_LENGTH_LOOSE
max_length = self.RSA_MAX_LENGTH_LOOSE
if self.bits < min_length:
raise TooShortKeyError(
"%s key data can not be shorter than %s bits (was %s)" % (self.key_type.decode(), min_length, self.bits)
)
if self.bits > max_length:
raise TooLongKeyError(
"%s key data can not be longer than %s bits (was %s)" % (self.key_type.decode(), max_length, self.bits)
)
return current_position
示例14: test_verify_signature_invalid_signature
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def test_verify_signature_invalid_signature(self):
"""
Test that verifying an invalid signature returns the right value.
"""
engine = crypto.CryptographyEngine()
backend = backends.default_backend()
public_key_numbers = rsa.RSAPublicNumbers(
int('010001', 16),
int(
'ac13d9fdae7b7335b69cd98567e9647d99bf373a9e05ce3435d66465f328'
'b7f7334b792aee7efa044ebc4c7a30b21a5d7a89cdb3a30dfcd9fee9995e'
'09415edc0bf9e5b4c3f74ff53fb4d29441bf1b7ed6cbdd4a47f9252269e1'
'646f6c1aee0514e93f6cb9df71d06c060a2104b47b7260ac37c106861dc7'
'8ca5a25faa9cb2e3',
16)
)
public_key = public_key_numbers.public_key(backend)
public_bytes = public_key.public_bytes(
serialization.Encoding.PEM,
serialization.PublicFormat.PKCS1
)
args = (
public_bytes,
b'',
b'',
enums.PaddingMethod.PSS
)
kwargs = {
'signing_algorithm': enums.CryptographicAlgorithm.RSA,
'hashing_algorithm': enums.HashingAlgorithm.SHA_1,
'digital_signature_algorithm': None
}
self.assertFalse(
engine.verify_signature(*args, **kwargs)
)
示例15: test_verify_signature
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import rsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers [as 别名]
def test_verify_signature(signature_parameters):
"""
Test that various signature verification methods and settings can be used
to correctly verify signatures.
"""
engine = crypto.CryptographyEngine()
backend = backends.default_backend()
public_key_numbers = rsa.RSAPublicNumbers(
signature_parameters.get('public_key').get('e'),
signature_parameters.get('public_key').get('n')
)
public_key = public_key_numbers.public_key(backend)
public_bytes = public_key.public_bytes(
signature_parameters.get('encoding'),
serialization.PublicFormat.PKCS1
)
result = engine.verify_signature(
signing_key=public_bytes,
message=signature_parameters.get('message'),
signature=signature_parameters.get('signature'),
padding_method=signature_parameters.get('padding_method'),
signing_algorithm=signature_parameters.get('signing_algorithm'),
hashing_algorithm=signature_parameters.get('hashing_algorithm'),
digital_signature_algorithm=signature_parameters.get(
'digital_signature_algorithm'
)
)
assert result