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


Python modes.CBC属性代码示例

本文整理汇总了Python中cryptography.hazmat.primitives.ciphers.modes.CBC属性的典型用法代码示例。如果您正苦于以下问题:Python modes.CBC属性的具体用法?Python modes.CBC怎么用?Python modes.CBC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在cryptography.hazmat.primitives.ciphers.modes的用法示例。


在下文中一共展示了modes.CBC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: aes_encrypt_b64

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def aes_encrypt_b64(key, data):
    """
    This function encrypts the data using AES-128-CBC. It generates
    and adds an IV.
    This is used for PSKC.

    :param key: Encryption key (binary format)
    :type key: bytes
    :param data: Data to encrypt
    :type data: bytes
    :return: base64 encrypted output, containing IV and encrypted data
    :rtype: str
    """
    # pad data
    padder = padding.PKCS7(algorithms.AES.block_size).padder()
    padded_data = padder.update(data) + padder.finalize()
    iv = geturandom(16)
    encdata = aes_cbc_encrypt(key, iv, padded_data)
    return b64encode_and_unicode(iv + encdata) 
开发者ID:privacyidea,项目名称:privacyidea,代码行数:21,代码来源:crypto.py

示例2: aes_decrypt_b64

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def aes_decrypt_b64(key, enc_data_b64):
    """
    This function decrypts base64 encoded data (containing the IV)
    using AES-128-CBC. Used for PSKC

    :param key: binary key
    :param enc_data_b64: base64 encoded data (IV + encdata)
    :type enc_data_b64: str
    :return: encrypted data
    """
    data_bin = base64.b64decode(enc_data_b64)
    iv = data_bin[:16]
    encdata = data_bin[16:]
    padded_data = aes_cbc_decrypt(key, iv, encdata)

    # remove padding
    unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
    output = unpadder.update(padded_data) + unpadder.finalize()

    return output


# @log_with(log) 
开发者ID:privacyidea,项目名称:privacyidea,代码行数:25,代码来源:crypto.py

示例3: decrypt

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def decrypt(self, k, a, iv, e, t):
        """ Decrypt according to the selected encryption and hashing
        functions.
        :param k: Encryption key (optional)
        :param a: Additional Authenticated Data
        :param iv: Initialization Vector
        :param e: Ciphertext
        :param t: Authentication Tag

        Returns plaintext or raises an error
        """
        hkey = k[:_inbytes(self.keysize)]
        dkey = k[_inbytes(self.keysize):]

        # verify mac
        if not constant_time.bytes_eq(t, self._mac(hkey, a, iv, e)):
            raise InvalidSignature('Failed to verify MAC')

        # decrypt
        cipher = Cipher(algorithms.AES(dkey), modes.CBC(iv),
                        backend=self.backend)
        decryptor = cipher.decryptor()
        d = decryptor.update(e) + decryptor.finalize()
        unpadder = PKCS7(self.blocksize).unpadder()
        return unpadder.update(d) + unpadder.finalize() 
开发者ID:latchset,项目名称:jwcrypto,代码行数:27,代码来源:jwa.py

示例4: _CTR_DRBG_AES128_update

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def _CTR_DRBG_AES128_update(data, key, v):
    assert len(data) == 32
    assert len(key) == 16
    assert len(v) == 16
    cipher = Cipher(algorithms.AES(key), modes.CBC(str_zero(16)),
                    backend=default_backend())

    v = str_inc(v)
    encryptor = cipher.encryptor()
    new_key = encryptor.update(v) + encryptor.finalize()

    v = str_inc(v)
    encryptor = cipher.encryptor()
    new_v = encryptor.update(v) + encryptor.finalize()
    return str_xor(new_key, data[:16]), str_xor(new_v, data[16:])


# Counter mode Deterministic Random Byte Generator
# Specialized for SPAN based on NIST 800-90A 
开发者ID:robertmuth,项目名称:PyZwaver,代码行数:21,代码来源:security.py

示例5: generate

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def generate(self, count, data=None):
        out = b""
        v = self._v
        key = self._key
        if data is not None:
            key, v = _CTR_DRBG_AES128_update(data, key, v)
        cipher = Cipher(algorithms.AES(key), modes.CBC(str_zero(16)),
                        backend=default_backend())

        while len(out) < count:
            encryptor = cipher.encryptor()
            v = str_inc(v)
            out += encryptor.update(v) + encryptor.finalize()
        if data is None:
            data = str_zero(32)
        self._key, self._v = _CTR_DRBG_AES128_update(data, key, v)
        return out[:count] 
开发者ID:robertmuth,项目名称:PyZwaver,代码行数:19,代码来源:security.py

示例6: aes_cbc_decrypt

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def aes_cbc_decrypt(key, iv, enc_data):
    """
    Decrypts the given cipherdata with AES (CBC Mode) using the key/iv.

    Attention: This function returns the decrypted data as is, without removing
    any padding. The calling function must take care of this!

    :param key: The encryption key
    :type key: bytes
    :param iv: The initialization vector
    :type iv: bytes
    :param enc_data: The cipher text
    :type enc_data: binary string
    :param mode: The AES MODE
    :return: plain text in binary data
    :rtype: bytes
    """
    backend = default_backend()
    mode = modes.CBC(iv)
    cipher = Cipher(algorithms.AES(key), mode=mode, backend=backend)
    decryptor = cipher.decryptor()
    output = decryptor.update(enc_data) + decryptor.finalize()
    return output 
开发者ID:privacyidea,项目名称:privacyidea,代码行数:25,代码来源:crypto.py

示例7: aes_cbc_encrypt

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def aes_cbc_encrypt(key, iv, data):
    """
    encrypts the given data with AES (CBC Mode) using key/iv.

    Attention: This function expects correctly padded input data (multiple of
    AES block size). The calling function must take care of this!

    :param key: The encryption key
    :type key: binary string
    :param iv: The initialization vector
    :type iv: binary string
    :param data: The cipher text
    :type data: bytes
    :param mode: The AES MODE
    :return: plain text in binary data
    :rtype: bytes
    """
    assert len(data) % (algorithms.AES.block_size // 8) == 0
    # do the encryption
    backend = default_backend()
    mode = modes.CBC(iv)
    cipher = Cipher(algorithms.AES(key), mode=mode, backend=backend)
    encryptor = cipher.encryptor()
    output = encryptor.update(data) + encryptor.finalize()
    return output 
开发者ID:privacyidea,项目名称:privacyidea,代码行数:27,代码来源:crypto.py

示例8: get_encryptor

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def get_encryptor(key, iv=None):
        algoer = algorithms.AES(key) #这里的AES算法(若要换成des算法,这里换成TripleDES,该加密库中,DES 事实上等于 TripleDES 的密钥长度为 64bit 时的加解密)
        cipher = Cipher(algoer, modes.CBC(iv), backend=default_backend()) #这里的CBC模式
        def enc(bitstring):
            padder    = padding.PKCS7(algoer.block_size).padder()
            bitstring = padder.update(bitstring) + padder.finalize()
            encryptor = cipher.encryptor()
            return encryptor.update(bitstring) + encryptor.finalize()
        def dec(bitstring):
            decryptor = cipher.decryptor()
            ddata     = decryptor.update(bitstring) + decryptor.finalize()
            unpadder  = padding.PKCS7(algoer.block_size).unpadder()
            return unpadder.update(ddata) + unpadder.finalize()
        class f:pass
        f.encrypt = enc
        f.decrypt = dec
        return f 
开发者ID:cilame,项目名称:vrequest,代码行数:19,代码来源:pyevpkdf.py

示例9: chrome_decrypt

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def chrome_decrypt(
    encrypted_value: bytes, key: bytes, init_vector: bytes
) -> str:
    """Decrypt Chrome/Chromium's encrypted cookies.

    Args:
        encrypted_value: Encrypted cookie from Chrome/Chromium's cookie file
        key: Key to decrypt encrypted_value
        init_vector: Initialization vector for decrypting encrypted_value
    Returns:
        Decrypted value of encrypted_value

    """
    # Encrypted cookies should be prefixed with 'v10' or 'v11' according to the
    # Chromium code. Strip it off.
    encrypted_value = encrypted_value[3:]

    cipher = Cipher(
        algorithm=AES(key), mode=CBC(init_vector), backend=default_backend()
    )
    decryptor = cipher.decryptor()
    decrypted = decryptor.update(encrypted_value) + decryptor.finalize()

    return clean(decrypted) 
开发者ID:n8henrie,项目名称:pycookiecheat,代码行数:26,代码来源:pycookiecheat.py

示例10: _encrypt_from_parts

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def _encrypt_from_parts(self, data, current_time, iv):
        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        padder = padding.PKCS7(algorithms.AES.block_size).padder()
        padded_data = padder.update(data) + padder.finalize()
        encryptor = Cipher(
            algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend
        ).encryptor()
        ciphertext = encryptor.update(padded_data) + encryptor.finalize()

        basic_parts = (
            b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext
        )

        h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend)
        h.update(basic_parts)
        hmac = h.finalize()
        return base64.urlsafe_b64encode(basic_parts + hmac) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:21,代码来源:fernet.py

示例11: _encrypt_from_parts

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def _encrypt_from_parts(self, data, current_time, iv):
        utils._check_bytes("data", data)

        padder = padding.PKCS7(algorithms.AES.block_size).padder()
        padded_data = padder.update(data) + padder.finalize()
        encryptor = Cipher(
            algorithms.AES(self._encryption_key), modes.CBC(iv), self._backend
        ).encryptor()
        ciphertext = encryptor.update(padded_data) + encryptor.finalize()

        basic_parts = (
            b"\x80" + struct.pack(">Q", current_time) + iv + ciphertext
        )

        h = HMAC(self._signing_key, hashes.SHA256(), backend=self._backend)
        h.update(basic_parts)
        hmac = h.finalize()
        return base64.urlsafe_b64encode(basic_parts + hmac) 
开发者ID:tp4a,项目名称:teleport,代码行数:20,代码来源:fernet.py

示例12: download_media

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def download_media(self, media_msg, force_download=False):
        if not force_download:
            try:
                if media_msg.content:
                    return BytesIO(b64decode(media_msg.content))
            except AttributeError:
                pass

        file_data = await self.download_file(media_msg.client_url)

        media_key = b64decode(media_msg.media_key)
        derivative = HKDFv3().deriveSecrets(
            media_key, binascii.unhexlify(media_msg.crypt_keys[media_msg.type]), 112
        )

        parts = ByteUtil.split(derivative, 16, 32)
        iv = parts[0]
        cipher_key = parts[1]
        e_file = file_data[:-10]

        cr_obj = Cipher(
            algorithms.AES(cipher_key), modes.CBC(iv), backend=default_backend()
        )
        decryptor = cr_obj.decryptor()
        return BytesIO(decryptor.update(e_file) + decryptor.finalize()) 
开发者ID:mukulhase,项目名称:WebWhatsapp-Wrapper,代码行数:27,代码来源:async_driver.py

示例13: _process_encrypted_session_key

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def _process_encrypted_session_key(self, message):
        log.debug("Received EncryptedSessionKey response")
        enc_sess_key = base64.b64decode(message.data.session_key)

        # strip off Win32 Crypto Blob Header and reverse the bytes
        encrypted_key = enc_sess_key[12:][::-1]
        pad_method = padding.PKCS1v15()
        decrypted_key = self._exchange_key.decrypt(encrypted_key, pad_method)

        iv = b"\x00" * 16  # PSRP doesn't use an IV
        algorithm = algorithms.AES(decrypted_key)
        mode = modes.CBC(iv)
        cipher = Cipher(algorithm, mode, default_backend())

        self._serializer.cipher = cipher
        self._key_exchanged = True
        self._exchange_key = None 
开发者ID:jborean93,项目名称:pypsrp,代码行数:19,代码来源:powershell.py

示例14: __init__

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def __init__(self, key, mode = cipherMODE.ECB, IV = None, pad = None, padMode = None):
		self.IV = IV
		#the python cryptography module sets the IV in the operational mode!!!
		if mode == cipherMODE.ECB:
			self.IV = modes.ECB()
		elif mode == cipherMODE.CBC:
			self.IV = modes.CBC(IV)
		elif mode == cipherMODE.CBC:
			self.IV = modes.CTR(IV)
		else:
			raise Exception('Unknown cipher mode!')
		
		self.key = key
		
		""" TODO padding
		if self.padMode is not None:
		"""
		
		self.encryptor = None
		self.decryptor = None
		symmetricBASE.__init__(self) 
开发者ID:skelsec,项目名称:msldap,代码行数:23,代码来源:AES.py

示例15: _crypt

# 需要导入模块: from cryptography.hazmat.primitives.ciphers import modes [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CBC [as 别名]
def _crypt(cls, op, key, iv, data):
        key = unhexlify(key)[:cls.BLOCKSIZE]
        iv = unhexlify(iv)[:cls.BLOCKSIZE]

        if not six.PY3:
            return cls._crypt_py2(op, key, iv, data)

        cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

        if op == Password.ENCRYPT:
            crypter = cipher.encryptor()
        elif op == Password.DECRYPT:
            crypter = cipher.decryptor()
        else:
            raise ValueError("Unable to perform op '%s'" % op)
        value = crypter.update(data) + crypter.finalize()
        return value 
开发者ID:candlepin,项目名称:virt-who,代码行数:19,代码来源:__init__.py


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