def encrypt(mode, key, iv, plaintext): cipher = base.Cipher(algorithms.IDEA(binascii.unhexlify(key)), mode(binascii.unhexlify(iv)), backend) encryptor = cipher.encryptor() ct = encryptor.update(binascii.unhexlify(plaintext)) ct += encryptor.finalize() return binascii.hexlify(ct)
class TestIDEAModeCFB: test_cfb = generate_encrypt_test( load_nist_vectors, os.path.join("ciphers", "IDEA"), ["idea-cfb.txt"], lambda key, **kwargs: algorithms.IDEA(binascii.unhexlify((key))), lambda iv, **kwargs: modes.CFB(binascii.unhexlify(iv)), )
class TestIDEAModeECB(object): test_ecb = generate_encrypt_test( load_nist_vectors, os.path.join("ciphers", "IDEA"), ["idea-ecb.txt"], lambda key, **kwargs: algorithms.IDEA(binascii.unhexlify((key))), lambda **kwargs: modes.ECB(), )
def test_idea(msg: str) -> int: key = os.urandom(16) iv = os.urandom(8) cipher = Cipher(algorithms.IDEA(key), modes.CBC(iv)) encryptor = cipher.encryptor() ct = encryptor.update(bytes(msg, 'utf-8')) + encryptor.finalize() decryptor = cipher.decryptor() decrypted_message = decryptor.update(ct) + decryptor.finalize() return getsizeof(ct)
def DIDEA(path,key,iv): f=open(os.path.join(path+"/Segments","3.txt"),"rb") content=f.read() f.close() backend = default_backend() cipher = Cipher(algorithms.IDEA(key), modes.CBC(iv), backend=backend) decryptor = cipher.decryptor() content=decryptor.update(content) + decryptor.finalize() # open(os.path.join(path+"/Segments","3.txt"),"wb").close() f=open(os.path.join(path+"/temp/Segments","3.txt"),"wb") f.write(content) f.close()
def IDEA(key, iv): f = open(os.path.join(os.getcwd() + "\Segments", "3.txt"), "r") content = f.read() f.close() content = content.encode() b = len(content) if (b % 8 != 0): while (b % 8 != 0): content += " ".encode() b = len(content) backend = default_backend() cipher = Cipher(algorithms.IDEA(key), modes.CBC(iv), backend=backend) encryptor = cipher.encryptor() cont = encryptor.update(content) + encryptor.finalize() open(os.path.join(os.getcwd() + "\Segments", "3.txt"), "w").close() f = open(os.path.join(os.getcwd() + "\Segments", "3.txt"), "wb") f.write(cont) f.close()
def get_cipher(self): if self.method.startswith('aes'): return Cipher(algorithms.AES(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('bf'): return Cipher(algorithms.Blowfish(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('camellia'): return Cipher(algorithms.Camellia(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('cast5'): return Cipher(algorithms.CAST5(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('seed'): return Cipher(algorithms.SEED(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('idea'): return Cipher(algorithms.IDEA(self.key), modes.CFB(self.iv), default_backend()) if self.method.startswith('rc4'): return Cipher(algorithms.ARC4(self.key), None, default_backend()) raise ValueError('crypto method %s not supported!' % self.method)
import binascii import os import pytest from cryptography.hazmat.primitives.ciphers import algorithms, modes from .utils import generate_encrypt_test from ...utils import load_nist_vectors @pytest.mark.supported( only_if=lambda backend: backend.cipher_supported( algorithms.IDEA(b"\x00" * 16), modes.ECB() ), skip_message="Does not support IDEA ECB", ) class TestIDEAModeECB: test_ecb = generate_encrypt_test( load_nist_vectors, os.path.join("ciphers", "IDEA"), ["idea-ecb.txt"], lambda key, **kwargs: algorithms.IDEA(binascii.unhexlify((key))), lambda **kwargs: modes.ECB(), ) @pytest.mark.supported( only_if=lambda backend: backend.cipher_supported(
# cf. https://github.com/PyCQA/bandit/blob/b78c938c0bd03d201932570f5e054261e10c5750/examples/ciphers.py from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.ciphers import algorithms from cryptography.hazmat.primitives.ciphers import modes from cryptography.hazmat.backends import default_backend from struct import pack # ruleid:insecure-cipher-algorithm-rc4 cipher = Cipher(algorithms.ARC4(key), mode=None, backend=default_backend()) encryptor = cipher.encryptor() ct = encryptor.update(b"a secret message") # ruleid:insecure-cipher-algorithm-blowfish cipher = Cipher(algorithms.Blowfish(key), mode=None, backend=default_backend()) encryptor = cipher.encryptor() ct = encryptor.update(b"a secret message") # ruleid:insecure-cipher-algorithm-idea cipher = Cipher(algorithms.IDEA(key), mode=None, backend=default_backend()) encryptor = cipher.encryptor() ct = encryptor.update(b"a secret message") # ok cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() ct = encryptor.update(b"a secret message") + encryptor.finalize()
from __future__ import absolute_import, division, print_function import binascii import os import pytest from cryptography.hazmat.primitives.ciphers import algorithms, modes from .utils import generate_encrypt_test from ...utils import load_nist_vectors @pytest.mark.supported( only_if=lambda backend: backend.cipher_supported( algorithms.IDEA("\x00" * 16), modes.ECB()), skip_message="Does not support IDEA ECB", ) @pytest.mark.cipher class TestIDEAModeECB(object): test_ECB = generate_encrypt_test( load_nist_vectors, os.path.join("ciphers", "IDEA"), ["idea-ecb.txt"], lambda key, **kwargs: algorithms.IDEA(binascii.unhexlify((key))), lambda **kwargs: modes.ECB(), ) @pytest.mark.supported( only_if=lambda backend: backend.cipher_supported(