當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。