当前位置: 首页>>代码示例>>Python>>正文


Python RSA.import_key方法代码示例

本文整理汇总了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") 
开发者ID:Azure,项目名称:aztk,代码行数:19,代码来源:create_user.py

示例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 
开发者ID:mlperf,项目名称:training,代码行数:24,代码来源:crypto.py

示例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] 
开发者ID:Azure,项目名称:aztk,代码行数:16,代码来源:secure_utils.py

示例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 
开发者ID:mlperf,项目名称:training,代码行数:28,代码来源:crypto.py

示例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) 
开发者ID:haynieresearch,项目名称:jarvis,代码行数:8,代码来源:test_import_RSA.py

示例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) 
开发者ID:mchristopher,项目名称:PokemonGo-DesktopMap,代码行数:8,代码来源:test_import_RSA.py


注:本文中的Cryptodome.PublicKey.RSA.import_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。