How to use the cryptography.hazmat.primitives.ciphers.Cipher function in cryptography

To help you get started, we’ve selected a few cryptography examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyca / cryptography / tests / hazmat / primitives / test_block.py View on Github external
def test_incorrectly_padded(self, backend):
        cipher = Cipher(
            algorithms.AES(b"\x00" * 16),
            modes.CBC(b"\x00" * 16),
            backend
        )
        encryptor = cipher.encryptor()
        encryptor.update(b"1")
        with pytest.raises(ValueError):
            encryptor.finalize()

        decryptor = cipher.decryptor()
        decryptor.update(b"1")
        with pytest.raises(ValueError):
            decryptor.finalize()
github pyca / cryptography / tests / hazmat / primitives / test_block.py View on Github external
def test_creates_decryptor(self, backend):
        cipher = Cipher(
            algorithms.AES(binascii.unhexlify(b"0" * 32)),
            modes.CBC(binascii.unhexlify(b"0" * 32)),
            backend
        )
        assert isinstance(cipher.decryptor(), base.CipherContext)
github mjg59 / python-broadlink / broadlink / __init__.py View on Github external
def update_aes(self, key):
        self.aes = Cipher(algorithms.AES(key), modes.CBC(self.iv),
                          backend=default_backend())
github mozilla / sops / sops / __init__.py View on Github external
def decrypt(value, key, stash=None):
    """
    Return a decrypted value
    """
    # extract fields using a regex
    res = re.match(r'^ENC\[AES256_GCM,data:(.+),iv:(.+),aad:(.+),tag:(.+)\]$',
                   value)
    # if the value isn't in encrypted form, return it as is
    if res is None:
        return value
    enc_value = b64decode(res.group(1))
    iv = b64decode(res.group(2))
    aad = b64decode(res.group(3))
    tag = b64decode(res.group(4))
    decryptor = Cipher(algorithms.AES(key),
                       modes.GCM(iv, tag),
                       default_backend()
                       ).decryptor()
    decryptor.authenticate_additional_data(aad)
    cleartext = decryptor.update(enc_value) + decryptor.finalize()
    if stash:
        # save the values for later if we need to reencrypt
        stash['iv'] = iv
        stash['aad'] = aad
        stash['cleartext'] = cleartext
    return cleartext
github OpenXbox / xbox-smartglass-core-python / xbox / sg / crypto.py View on Github external
def _crypt(key, iv, encrypt, data):
        if not iv:
            iv = b'\x00' * 16
        cipher = Cipher(
            algorithms.AES(key), modes.CBC(iv), backend=Crypto._backend
        )
        if encrypt:
            cryptor = cipher.encryptor()
        else:
            cryptor = cipher.decryptor()

        return cryptor.update(data) + cryptor.finalize()
github offu / WeRoBot / werobot / crypto / __init__.py View on Github external
def __init__(self, key):
        key = to_binary(key)
        self.cipher = Cipher(
            algorithms.AES(key),
            modes.CBC(key[:16]),
            backend=default_backend()
        )
github Grass-CLP / NXSpider / NXSpider / common / encrypt.py View on Github external
def aes_ecb(text, sec_key):
    backend = default_backend()
    text = text.encode('utf-8')
    pad = 16 - len(text) % 16
    text_t = text + (b'\0' * pad)
    cipher = Cipher(
        algorithms.AES(sec_key.encode('utf-8')),
        modes.ECB(),
        backend=backend
    )
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(text_t) + encryptor.finalize()
    ciphertext = base64.b64encode(ciphertext)
    return ciphertext
github SecureAuthCorp / pysap / pysap / SAPCredv2.py View on Github external
"""
            digest = Hash(SHA256(), backend=default_backend())
            digest.update(key)
            digest.update(header)
            digest.update(salt)
            digest.update(xor(username, ord(salt[0])))
            digest.update("" * 0x20)
            hashed = digest.finalize()
            derived_key = xor(hashed, ord(salt[1]))
            return derived_key

        # Derive the key using SAP's algorithm
        key = derive_key(cred_key_fmt, blob[0:4], header.salt, username)

        # Decrypt the cipher text with the derived key and IV
        decryptor = Cipher(algorithm(key), modes.CBC(header.iv), backend=default_backend()).decryptor()
        plain = decryptor.update(header.cipher_text) + decryptor.finalize()

        # Perform a final xor over the decrypted content with a fixed key
        plain = xor(plain, 0x64FB914E)

        return SAPCredv2_Cred_Plain(plain)
github espressif / ESP8266_RTOS_SDK / components / nvs_flash / nvs_partition_generator / nvs_partition_gen.py View on Github external
def encrypt_entry(self, data_arr, tweak_arr, encr_key):
        # Encrypt 32 bytes of data using AES-XTS encryption
        backend = default_backend()
        plain_text = codecs.decode(data_arr, 'hex')
        tweak = codecs.decode(tweak_arr, 'hex')

        cipher = Cipher(algorithms.AES(encr_key), modes.XTS(tweak), backend=backend)
        encryptor = cipher.encryptor()
        encrypted_data = encryptor.update(plain_text)

        return encrypted_data