當前位置: 首頁>>代碼示例>>Python>>正文


Python AES.new方法代碼示例

本文整理匯總了Python中Crypto.Cipher.AES.new方法的典型用法代碼示例。如果您正苦於以下問題:Python AES.new方法的具體用法?Python AES.new怎麽用?Python AES.new使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Crypto.Cipher.AES的用法示例。


在下文中一共展示了AES.new方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: encrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def encrypt(self, text, appid):
        """對明文進行加密
        @param text: 需要加密的明文
        @return: 加密得到的字符串
        """
        # 16位隨機字符串添加到明文開頭
        len_str = struct.pack("I", socket.htonl(len(text.encode())))
        # text = self.get_random_str() + binascii.b2a_hex(len_str).decode() + text + appid
        text = self.get_random_str() + len_str + text.encode() + appid
        # 使用自定義的填充方式對明文進行補位填充
        pkcs7 = PKCS7Encoder()
        text = pkcs7.encode(text)
        # 加密
        cryptor = AES.new(self.key, self.mode, self.key[:16])
        try:
            ciphertext = cryptor.encrypt(text)
            # 使用BASE64對加密後的字符串進行編碼
            return ierror.WXBizMsgCrypt_OK, base64.b64encode(ciphertext).decode('utf8')
        except Exception as e:
            return ierror.WXBizMsgCrypt_EncryptAES_Error, None 
開發者ID:EvilPsyCHo,項目名稱:TaskBot,代碼行數:22,代碼來源:WXBizMsgCrypt_py3.py

示例2: decrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def decrypt(ciphertext, key, blockSize):
	#print "ciphertext: " + base64.b64encode(ciphertext)
	iv = ciphertext[0:blockSize]
	#print "iv: " + base64.b64encode(iv)
	#print "ciphertext: " + base64.b64encode(ciphertext)
	rivCiphertext = ciphertext[blockSize:]
	#print "rivCiphertext: " + base64.b64encode(rivCiphertext)
	rivCiphertext = str(rivCiphertext)
	#print "rivCiphertext: " + base64.b64encode(rivCiphertext)
	cipher = AES.new(key, AES.MODE_CBC, IV=iv)
	rivPlaintext = cipher.decrypt(rivCiphertext)
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext)
	rivPlaintext = str(rivPlaintext)
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext)
	rivPlaintext = unpad(rivPlaintext)
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext)
	#rivPlaintext = str(cipher.decrypt(str(rivCiphertext)))
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext)
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext[blockSize:])
	return rivPlaintext[blockSize:]
	#return rivPlaintext 
開發者ID:nccgroup,項目名稱:ABPTTS,代碼行數:23,代碼來源:abpttsclient.py

示例3: unlock

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def unlock(vault_path, key):
    """
        Unlock legacy vault and retrieve content
    """

    f = open(vault_path, "rb")
    try:
        nonce, tag, ciphertext = [f.read(x) for x in (16, 16, -1)]
    finally:
        f.close()

    # Unlock Vault with key
    cipher = AES.new(get_hash(key), AES.MODE_EAX, nonce)
    data = cipher.decrypt_and_verify(ciphertext, tag)

    # Set vault content to class level var
    return json.loads(data.decode("utf-8")) 
開發者ID:gabfl,項目名稱:vault,代碼行數:19,代碼來源:migration.py

示例4: prepare_items

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def prepare_items(secrets, categories):
    """
        Prepare all secrets to the new import format
    """

    out = []
    for secret in secrets:
        out.append({
            'name': secret.get('name'),
            'url': None,  # Not supported in legacy database
            'login': secret.get('login'),
            'password': secret.get('password'),
            'notes': secret.get('notes'),
            'category': get_category_name(secret.get('category'), categories),
        })

    return out 
開發者ID:gabfl,項目名稱:vault,代碼行數:19,代碼來源:migration.py

示例5: decrypt_secret

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def decrypt_secret(data, key):
    if not data:
        return None

    aeskey = ""
    sha256 = SHA256.new()
    sha256.update(key)
    for i in range(1000):
        sha256.update(data[28:60])
    aeskey = sha256.digest()

    secret = ""
    aes = AES.new(aeskey)
    for key_offset in range(0, len(data) - 60, 16):
        if (key_offset + 16) <= len(data) - 60:
            secret = secret + aes.decrypt(data[60 + key_offset:60 + key_offset + 16])

    return secret 
開發者ID:HarmJ0y,項目名稱:ImpDump,代碼行數:20,代碼來源:lsasecretsw2k8.py

示例6: decrypt_file

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def decrypt_file(key, filename, chunk_size=24*1024):
        
    output_filename = os.path.splitext(filename)[0]

    with open(filename, 'rb') as infile:
        origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
        iv = infile.read(16)
        decryptor = AES.new(key, AES.MODE_CBC, iv)

        with open(output_filename, 'wb') as outfile:
            while True:
                chunk = infile.read(chunk_size)
                if len(chunk) == 0:
                    break
                outfile.write(decryptor.decrypt(chunk))

            outfile.truncate(origsize) 
開發者ID:PacktPublishing,項目名稱:Effective-Python-Penetration-Testing,代碼行數:19,代碼來源:aes-file-decrypt.py

示例7: decrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def decrypt(self, ciphertext: bytes) -> bytes:
        """Return plaintext for given ciphertext."""

        # Split out the nonce, tag, and encrypted data.
        nonce = ciphertext[:12]
        if len(nonce) != 12:
            raise DataIntegrityError("Cipher text is damaged: invalid nonce length")

        tag = ciphertext[12:28]
        if len(tag) != 16:
            raise DataIntegrityError("Cipher text is damaged: invalid tag length")

        encrypted = ciphertext[28:]

        # Construct AES cipher, with old nonce.
        cipher = AES.new(self.cipher_key, AES.MODE_GCM, nonce)

        # Decrypt and verify.
        try:
            plaintext = cipher.decrypt_and_verify(encrypted, tag)  # type: ignore
        except ValueError as e:
            raise DataIntegrityError("Cipher text is damaged: {}".format(e))
        return plaintext 
開發者ID:johnbywater,項目名稱:eventsourcing,代碼行數:25,代碼來源:aes.py

示例8: __init__

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def __init__(self, key, iv=None):
        iv = iv or key[:16]
        super().__init__(AES.new(key, AES.MODE_CBC, iv)) 
開發者ID:wechatpy,項目名稱:wechatpy,代碼行數:5,代碼來源:pycrypto.py

示例9: _aes_decrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def _aes_decrypt(aes_key, aes_text) -> Text:
    """
    使用密鑰解密文本信息,將會自動填充空白字符
    :param aes_key: 解密密鑰
    :param aes_text: 需要解密的文本
    :return: 經過解密的數據
    """
    # 初始化解碼器
    cipher = AES.new(_fill_16(aes_key), AES.MODE_ECB)
    # 優先逆向解密十六進製為bytes
    converted = a2b_base64(aes_text.replace('-', '/').replace("%3D", "=").encode())
    # 使用aes解密密文
    decrypt_text = str(cipher.decrypt(converted), encoding='utf-8').replace('\0', '')
    # 返回執行結果
    return decrypt_text.strip() 
開發者ID:everyclass,項目名稱:everyclass-server,代碼行數:17,代碼來源:encryption.py

示例10: _aes_encrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def _aes_encrypt(aes_key, aes_text) -> Text:
    """
    使用密鑰加密文本信息,將會自動填充空白字符
    :param aes_key: 加密密鑰
    :param aes_text: 需要加密的文本
    :return: 經過加密的數據
    """
    # 初始化加密器
    cipher = AES.new(_fill_16(aes_key), AES.MODE_ECB)
    # 先進行aes加密
    aes_encrypted = cipher.encrypt(_fill_16(aes_text))
    # 使用十六進製轉成字符串形式
    encrypt_text = b2a_base64(aes_encrypted).decode().replace('/', '-').strip()
    # 返回執行結果
    return encrypt_text 
開發者ID:everyclass,項目名稱:everyclass-server,代碼行數:17,代碼來源:encryption.py

示例11: decrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def decrypt(self, text, appid):
        """對解密後的明文進行補位刪除
        @param text: 密文
        @return: 刪除填充補位後的明文
        """
        try:
            cryptor = AES.new(self.key, self.mode, self.key[:16])
            # 使用BASE64對密文進行解碼,然後AES-CBC解密
            plain_text = cryptor.decrypt(base64.b64decode(text))
        except Exception as e:
            print(e)
            return ierror.WXBizMsgCrypt_DecryptAES_Error, None
        try:
            # pad = ord(plain_text[-1])
            pad = plain_text[-1]
            # 去掉補位字符串
            # pkcs7 = PKCS7Encoder()
            # plain_text = pkcs7.encode(plain_text)
            # 去除16位隨機字符串
            content = plain_text[16:-pad]
            xml_len = socket.ntohl(struct.unpack("I", content[: 4])[0])
            xml_content = content[4: xml_len + 4]
            from_appid = content[xml_len + 4:]
        except Exception as e:
            return ierror.WXBizMsgCrypt_IllegalBuffer, None
        if from_appid != appid:
            return ierror.WXBizMsgCrypt_ValidateAppid_Error, None
        return 0, xml_content.decode() 
開發者ID:EvilPsyCHo,項目名稱:TaskBot,代碼行數:30,代碼來源:WXBizMsgCrypt_py3.py

示例12: encrypt

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def encrypt(plaintext, key, blockSize):
	iv = bytearray(os.urandom(blockSize))
	iv = str(iv)
	reIV = bytearray(os.urandom(blockSize))
	reIV = str(reIV)
	rivPlaintext = pad(reIV + str(plaintext), blockSize)
	#print "rivPlaintext: " + base64.b64encode(rivPlaintext)
	cipher = AES.new(key, AES.MODE_CBC, IV=iv)
	return iv + str(cipher.encrypt(rivPlaintext)) 
開發者ID:nccgroup,項目名稱:ABPTTS,代碼行數:11,代碼來源:abpttsclient.py

示例13: get_hash

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def get_hash(key):
    """
        Returns a 32 bytes hash for a given master key
    """

    h = SHA256.new()
    for i in range(1, 10000):
        h.update(str.encode(
            str(i) + config.salt + key))
    return base64.b64decode(str.encode(h.hexdigest()[:32])) 
開發者ID:gabfl,項目名稱:vault,代碼行數:12,代碼來源:migration.py

示例14: digest_key

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def digest_key(self):
        """
            Use SHA-256 over our key to get a proper-sized AES key
        """

        # Add optional salt to key
        key = self.key
        if self.salted_key:
            key = self.salted_key

        return SHA256.new(key).digest() 
開發者ID:gabfl,項目名稱:vault,代碼行數:13,代碼來源:Encryption.py

示例15: get_aes

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import new [as 別名]
def get_aes(self, IV):
        """
            AES instance
        """

        return AES.new(self.digest_key(), AES.MODE_CBC, IV) 
開發者ID:gabfl,項目名稱:vault,代碼行數:8,代碼來源:Encryption.py


注:本文中的Crypto.Cipher.AES.new方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。