How to use the cryptography.hazmat.primitives.ciphers.algorithms.AES 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_cmac.py View on Github external
def test_copy_with_backend(self, backend):
        key = b"2b7e151628aed2a6abf7158809cf4f3c"
        cmac = CMAC(AES(key), backend)
        cmac.update(b"6bc1bee22e409f96e93d7e117393172a")
        copy_cmac = cmac.copy()
        assert cmac.finalize() == copy_cmac.finalize()
github inveniosoftware / invenio / invenio / ext / passlib / hash.py View on Github external
def _mysql_aes_engine(key):
    """Create MYSQL AES cipher engine."""
    return Cipher(algorithms.AES(key), modes.ECB(), default_backend())
github qtumproject / qtum-electrum / electrum / crypto.py View on Github external
def aes_encrypt_with_iv(key: bytes, iv: bytes, data: bytes) -> bytes:
    assert_bytes(key, iv, data)
    data = append_PKCS7_padding(data)
    if HAS_CRYPTODOME:
        e = CD_AES.new(key, CD_AES.MODE_CBC, iv).encrypt(data)
    elif HAS_CRYPTOGRAPHY:
        cipher = CG_Cipher(CG_algorithms.AES(key), CG_modes.CBC(iv), backend=CG_default_backend())
        encryptor = cipher.encryptor()
        e = encryptor.update(data) + encryptor.finalize()
    else:
        aes_cbc = pyaes.AESModeOfOperationCBC(key, iv=iv)
        aes = pyaes.Encrypter(aes_cbc, padding=pyaes.PADDING_NONE)
        e = aes.feed(data) + aes.feed()  # empty aes.feed() flushes buffer
    return e
github ONSdigital / eq-survey-runner / app / cryptography / jwe_decryption.py View on Github external
def _decrypt_cipher_text(cipher_text, iv, key, tag, jwe_protected_header):
        cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag), backend=backend)
        decryptor = cipher.decryptor()
        decryptor.authenticate_additional_data(jwe_protected_header.encode())
        decrypted_token = decryptor.update(cipher_text) + decryptor.finalize()
        return decrypted_token
github Syndace / python-omemo / omemo / backends / signal / doubleratchet / cbcaead.py View on Github external
def __getAES(self, key, iv):
        return Cipher(
            algorithms.AES(key),
            modes.CBC(iv),
            backend = self.__class__.CRYPTOGRAPHY_BACKEND
        )
github trakt / Plex-Trakt-Scrobbler / Trakttv.bundle / Contents / Libraries / Linux / armv7_hf / marvell-pj4 / ucs4 / cryptography / hazmat / backends / openssl / backend.py View on Github external
def _register_default_ciphers(self):
        for mode_cls in [CBC, CTR, ECB, OFB, CFB, CFB8, GCM]:
            self.register_cipher_adapter(
                AES,
                mode_cls,
                GetCipherByName("{cipher.name}-{cipher.key_size}-{mode.name}")
            )
        for mode_cls in [CBC, CTR, ECB, OFB, CFB]:
            self.register_cipher_adapter(
                Camellia,
                mode_cls,
                GetCipherByName("{cipher.name}-{cipher.key_size}-{mode.name}")
            )
        for mode_cls in [CBC, CFB, CFB8, OFB]:
            self.register_cipher_adapter(
                TripleDES,
                mode_cls,
                GetCipherByName("des-ede3-{mode.name}")
            )
        self.register_cipher_adapter(
github divio / divio-cli / divio_cli / crypto.py View on Github external
def minval(length):
    def validator(instance, attribute, value):
        if value < length:
            raise ValueError("{} has to be >= {}".format(attribute.name, length))

    return validator


class CipherMixin(object):
    version = 1

    # If anything below changes, the version also needs to be bumped and the
    # implementation adapted accordingly.
    header_format = b">B16s16s16s"
    header_length = struct.calcsize(header_format)
    algorithm = algorithms.AES
    key_size = 256
    cipher_mode = modes.CTR
    auth = hmac.HMAC
    auth_hash = hashes.SHA256

    def encode_header(self, iv, salt, auth_salt):
        return struct.pack(self.header_format, self.version, iv, salt, auth_salt)

    def decode_header(self, data):
        version, iv, salt, auth_salt = struct.unpack(self.header_format, data)
        assert version == self.version
        return iv, salt, auth_salt

    @classmethod
    def generate_key(self):
        return os.urandom(self.key_size / 8)
github terrycain / aioboto3 / aioboto3 / s3 / cse.py View on Github external
decryption_key = base64.b64decode(metadata['x-amz-key'])
        material_description = json.loads(metadata['x-amz-matdesc'])

        aes_key = await self._crypto_context.get_decryption_aes_key(decryption_key, material_description)

        # x-amz-key - Contains base64 encrypted key
        # x-amz-iv - AES IVs
        # x-amz-matdesc - JSON Description of client-side master key (used as encryption context as is)
        # x-amz-unencrypted-content-length - Unencrypted content length

        iv = base64.b64decode(metadata['x-amz-iv'])

        # TODO look at doing AES as stream

        # AES/CBC/PKCS5Padding
        aescbc = Cipher(AES(aes_key), CBC(iv), backend=self._backend).decryptor()
        padded_result = await self._loop.run_in_executor(None, lambda: (aescbc.update(file_data) + aescbc.finalize()))

        unpadder = PKCS7(AES.block_size).unpadder()
        result = await self._loop.run_in_executor(None, lambda: (unpadder.update(padded_result) + unpadder.finalize()))

        return result
github jrnl-org / jrnl / jrnl / EncryptedJournal.py View on Github external
def decrypt_journal(password):
            decryption_key = hashlib.sha256(password.encode('utf-8')).digest()
            decryptor = Cipher(algorithms.AES(decryption_key), modes.CBC(iv), default_backend()).decryptor()
            try:
                plain_padded = decryptor.update(cipher) + decryptor.finalize()
                self.password = password
                if plain_padded[-1] in (" ", 32):
                    # Ancient versions of jrnl. Do not judge me.
                    return plain_padded.decode('utf-8').rstrip(" ")
                else:
                    unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
                    plain = unpadder.update(plain_padded) + unpadder.finalize()
                    return plain.decode('utf-8')
            except ValueError:
                return None
        if self.password:
github cfbao / lastpass-vault-parser / lpparser.py View on Github external
def aes_decrypt_raw(ciphertext, key, mode):
    cipher = Cipher(algorithms.AES(key), mode, backend=_backend)
    decryptor = cipher.decryptor()
    unpadder = padding.PKCS7(128).unpadder()
    plaintext = decryptor.update(ciphertext)+decryptor.finalize()
    return unpadder.update(plaintext) + unpadder.finalize()