本文整理汇总了Python中Cryptodome.PublicKey.RSA.import_key方法的典型用法代码示例。如果您正苦于以下问题:Python RSA.import_key方法的具体用法?Python RSA.import_key怎么用?Python RSA.import_key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.PublicKey.RSA
的用法示例。
在下文中一共展示了RSA.import_key方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt_password
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import import_key [as 别名]
def decrypt_password(user_conf):
cipher_text = user_conf["password"]
encrypted_aes_session_key = user_conf["aes_session_key"]
cipher_aes_nonce = user_conf["cipher_aes_nonce"]
tag = user_conf["tag"]
# Read private key
with open(os.path.join(os.environ["AZTK_WORKING_DIR"], "id_rsa"), encoding="UTF-8") as f:
private_key = RSA.import_key(f.read())
# Decrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(encrypted_aes_session_key)
# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce)
password = cipher_aes.decrypt_and_verify(cipher_text, tag)
return password.decode("utf-8")
示例2: encrypt_file
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import import_key [as 别名]
def encrypt_file(public_key, src_file, dest_file):
try:
with open(src_file) as f:
rsa_key = RSA.import_key(open(public_key).read())
session_key = get_random_bytes(16)
# Encrypt session key
cipher_rsa = PKCS1_OAEP.new(rsa_key)
encrypted_session_key = cipher_rsa.encrypt(session_key)
# Encrypt data
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(f.read().encode("utf-8"))
except Exception as e:
print("Unable to encrypt file: {}".format(src_file))
raise e
try:
with open(dest_file, "wb") as f:
for x in (encrypted_session_key, cipher_aes.nonce, tag, ciphertext):
f.write(x)
except Exception as e:
print("Unable to write output file {}".format(dest_file))
raise e
示例3: encrypt_password
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import import_key [as 别名]
def encrypt_password(ssh_pub_key, password):
if not password:
return [None, None, None, None]
recipient_key = RSA.import_key(ssh_pub_key)
session_key = get_random_bytes(16)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted_aes_session_key = cipher_rsa.encrypt(session_key)
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(password.encode())
return [encrypted_aes_session_key, cipher_aes.nonce, tag, ciphertext]
示例4: decrypt_file
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import import_key [as 别名]
def decrypt_file(private_key, src_file, dest_file):
try:
with open(src_file, "rb") as f:
rsa_key = RSA.import_key(open(private_key).read())
encrypted_session_key = f.read(rsa_key.size_in_bytes())
nonce = f.read(16)
tag = f.read(16)
ciphertext = f.read(-1)
# Decrypt session key
cipher_rsa = PKCS1_OAEP.new(rsa_key)
session_key = cipher_rsa.decrypt(encrypted_session_key)
# Decrypt data
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
data = data.decode("utf-8")
except Exception as e:
print("Unable to decrypt file: {}".format(src_file))
raise e
try:
with open(dest_file, "w") as f:
f.write(data)
except Exception as e:
print("Unable to write output file: {}".format(dest_file))
raise e
示例5: test_import_key
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.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.assertFalse(key.has_private())
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
示例6: test_import_key
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.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)