Beispiel #1
0
 def encript(self, txt):
     backend = default_backend()
     cipher = None
     block_size = 0
     # Algoritmo
     if self.algoritmo == 'AES':
         alg = algorithms.AES(self.key)
         block_size = alg.block_size
     elif self.algoritmo == 'CAST5':
         alg = algorithms.CAST5(self.key)
         block_size = alg.block_size
     # Modo de encriptografar
     if self.modo == 'ECB':
         mod = modes.ECB()
     elif self.modo == 'CBC':
         mod = modes.CBC(self.iv)
     cipher = Cipher(alg, mod, backend=default_backend())
     # Sintese
     if self.sintese == 'SHA-256':
         sints = hashes.SHA256()
     elif self.sintese == 'MD5':
         sints = hashes.MD5()
     encryptor = cipher.encryptor()
     # Text
     msg = base64.b64encode(txt)
     padder = padding.PKCS7(block_size).padder()
     padded_data = padder.update(msg) + padder.finalize()
     # Encrypted text
     ct = encryptor.update(padded_data) + encryptor.finalize()
     # HMAC
     h = hmac.HMAC(self.key, sints, backend=default_backend())
     h.update(ct)
     mac = h.finalize()
     return ct, mac
Beispiel #2
0
 def decript(self, ct, mac):
     backend = default_backend()
     cipher = None
     block_size = 0
     # Algoritmo
     if self.algoritmo == 'AES':
         alg = algorithms.AES(self.key)
         block_size = alg.block_size
     elif self.algoritmo == 'CAST5':
         alg = algorithms.CAST5(self.key)
         block_size = alg.block_size
     # Modo de encriptografar
     if self.modo == 'ECB':
         mod = modes.ECB()
     elif self.modo == 'CBC':
         mod = modes.CBC(self.iv)
     cipher = Cipher(alg, mod, backend=default_backend())
     # Sintese
     if self.sintese == 'SHA-256':
         sints = hashes.SHA256()
     elif self.sintese == 'MD5':
         sints = hashes.MD5()
     # Decryptor
     decryptor = cipher.decryptor()
     # Padder
     unpadder = padding.PKCS7(block_size).unpadder()
     # Hmac
     h = hmac.HMAC(self.key, sints, backend=default_backend())
     # VERIFICAR SE O HMAC CONTINUA O MESMO
     h.update(ct)
     h.verify(mac)
     padded_data = decryptor.update(ct) + decryptor.finalize()
     data = unpadder.update(padded_data) + unpadder.finalize()
     clean_data = base64.b64decode(data)
     return clean_data
def CAST5Decrypt(ciphertext, key, iv):
    backend = default_backend()
    cipher = Cipher(algorithms.CAST5(key), modes.CBC(iv), backend=backend)
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()
    plaintext = plaintext[0:123]
    return plaintext
def encrypt(mode, key, iv, plaintext):
    cipher = base.Cipher(algorithms.CAST5(binascii.unhexlify(key)),
                         mode(binascii.unhexlify(iv)), default_backend())
    encryptor = cipher.encryptor()
    ct = encryptor.update(binascii.unhexlify(plaintext))
    ct += encryptor.finalize()
    return binascii.hexlify(ct)
def CAST5Encrypt(plaintext, key, iv):
    plaintext = pad(plaintext)
    backend = default_backend()
    cipher = Cipher(algorithms.CAST5(key), modes.CBC(iv), backend=backend)
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()
    return ciphertext
Beispiel #6
0
class TestCAST5ModeECB(object):
    test_ECB = generate_encrypt_test(
        load_nist_vectors,
        os.path.join("ciphers", "CAST5"),
        ["cast5-ecb.txt"],
        lambda key, **kwargs: algorithms.CAST5(binascii.unhexlify((key))),
        lambda **kwargs: modes.ECB(),
    )
Beispiel #7
0
class TestCAST5ModeCBC(object):
    test_cbc = generate_encrypt_test(
        load_nist_vectors,
        os.path.join("ciphers", "CAST5"),
        ["cast5-cbc.txt"],
        lambda key, **kwargs: algorithms.CAST5(binascii.unhexlify((key))),
        lambda iv, **kwargs: modes.CBC(binascii.unhexlify(iv)),
    )
Beispiel #8
0
 def _algorithm(self, secret, name='AES'):
     if not name:
         return algorithms.AES(secret)
     name = name.upper()
     if name == 'AES':
         return algorithms.AES(secret)
     if name == 'CAMELLIA':
         return algorithms.Camellia(secret)
     elif name == 'CAST5':
         return algorithms.CAST5(secret)
     elif name == 'SEED':
         return algorithms.SEED(secret)
     else:
         return algorithms.AES(secret)
Beispiel #9
0
 def get_algorithm(self, algorithm, key):
     if algorithm == SecurityAlgorithm.none:
         return None
     elif algorithm == SecurityAlgorithm.aes:
         return algorithms.AES(key)
     elif algorithm == SecurityAlgorithm.camellia:
         return algorithms.Camellia(key)
     # elif algorithm == SecurityAlgorithm.chacha20:
     #    return algorithms.ChaCha20(key)
     elif algorithm == SecurityAlgorithm.triple_des:
         return algorithms.TripleDES(key)
     elif algorithm == SecurityAlgorithm.cast5:
         return algorithms.CAST5(key)
     elif algorithm == SecurityAlgorithm.seed:
         return algorithms.SEED(key)
     else:
         logger.error("Not supportable algorithm")
Beispiel #10
0
def start_cifra(key,iv,who):
    global CSUIT
    alg,modo,dige = CSUIT[who].split("_")
    if(modo == 'CFB'):
        mode = modes.CFB(iv)
    elif(modo == 'CTR'):
        mode = modes.CTR(iv)
    elif(modo == 'OFB'):
        mode = modes.OFB(iv)
    if(alg == 'AES'):
        algorithm = algorithms.AES(key)
    elif(alg == 'SEED'):
        algorithm = algorithms.SEED(key)
    elif(alg == 'CAST5'):
        algorithm = algorithms.CAST5(key)
    elif(alg == 'TripleDES'):
        algorithm = algorithms.TripleDES(key)
    elif(alg == 'Camellia'):
        algorithm = algorithms.Camellia(key)
    cifra = Cipher(algorithm,mode)
    return cifra
 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)
Beispiel #12
0
 encryptor = cipher.encryptor()
 enctext = encryptor.update(message) + encryptor.finalize()
 enc_run = ['encrypt']
 enc_trial = Timer("encryptor.update(message) + encryptor.finalize()", setup)
 enc_run.append(enc_trial.repeat(RUN_COUNT, 1))
 algo_run.append(enc_run)
 dec_run = ['decrypt']
 dec_trial = Timer("decryptor.update(enctext) + decryptor.finalize()", setup)
 dec_run.append(dec_trial.repeat(RUN_COUNT, 1))
 algo_run.append(dec_run)
 set_run.append(algo_run)
 
 # Create cipher, create cipher text for decryption, time operations, update
 # result with each new operation
 algo_run = ['CAST5']
 cipher = Cipher(algorithms.CAST5(key), modes.CBC(iv8), backend=backend)
 encryptor = cipher.encryptor()
 enctext = encryptor.update(message) + encryptor.finalize()
 enc_run = ['encrypt']
 enc_trial = Timer("encryptor.update(message) + encryptor.finalize()", setup)
 enc_run.append(enc_trial.repeat(RUN_COUNT, 1))
 algo_run.append(enc_run)
 dec_run = ['decrypt']
 dec_trial = Timer("decryptor.update(enctext) + decryptor.finalize()", setup)
 dec_run.append(dec_trial.repeat(RUN_COUNT, 1))
 algo_run.append(dec_run)
 set_run.append(algo_run)
 
 lib_run.append(set_run)
 
 
Beispiel #13
0
import binascii
import os

import pytest

from cryptography.hazmat.backends.interfaces import CipherBackend
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.CAST5(b"\x00" * 16), modes.ECB()),
    skip_message="Does not support CAST5 ECB",
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
class TestCAST5ModeECB(object):
    test_ECB = generate_encrypt_test(
        load_nist_vectors,
        os.path.join("ciphers", "CAST5"),
        ["cast5-ecb.txt"],
        lambda key, **kwargs: algorithms.CAST5(binascii.unhexlify((key))),
        lambda **kwargs: modes.ECB(),
    )


@pytest.mark.supported(
    only_if=lambda backend: backend.cipher_supported(
def choicesS(algo):
    if (algo == 1): return algorithms.AES(key)  #block size == 128 bits
    elif (algo == 2): return algorithms.TripleDES(key)  #64 bits
    elif (algo == 3): return algorithms.Camellia(key)  #128 bits
    elif (algo == 4): return algorithms.CAST5(key)  #64 bits
    elif (algo == 5): return algorithms.SEED(key)  #128 bits
import binascii
import os

import pytest

from cryptography.hazmat.backends.interfaces import CipherBackend
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.CAST5("\x00" * 16), modes.ECB()),
    skip_message="Does not support CAST5 ECB",
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
class TestCAST5ModeECB(object):
    test_ECB = generate_encrypt_test(
        load_nist_vectors,
        os.path.join("ciphers", "CAST5"),
        ["cast5-ecb.txt"],
        lambda key, **kwargs: algorithms.CAST5(binascii.unhexlify((key))),
        lambda **kwargs: modes.ECB(),
    )


@pytest.mark.supported(
    only_if=lambda backend: backend.cipher_supported(
        CAST5_plaintext_bytes)  #This pads the data

    CAST5_padded_data += CAST5_padder.finalize(
    )  #This finalizes the padded data

    print(CAST5_padded_data, "\n\n")  #This prints the padded data

    #The padding portion of the code is done

    #The following code does the encryption

    print("You have selected CAST5\n")
    CAST5_backend = default_backend()
    CAST5_key = os.urandom(16)
    CAST5_iv = os.urandom(8)
    cipher = Cipher(algorithms.CAST5(CAST5_key),
                    modes.CFB(CAST5_iv),
                    backend=CAST5_backend)
    CAST5_encryptor = cipher.encryptor()
    cipher_text = CAST5_encryptor.update(
        CAST5_plaintext_bytes) + CAST5_encryptor.finalize()
    CAST5_decryptor = cipher.decryptor()
    plaintext_from_ciphertext = CAST5_decryptor.update(
        cipher_text) + CAST5_decryptor.finalize()

    print("This is the key that was used to encrypt:", CAST5_key, "\n")
    print("This is the value of the Initization Vector that was used:",
          CAST5_iv, "\n")
    print("This is the cipher text: ", cipher_text, "\n")

    print("\nThis is the plaintext decoded from the plaintext: ",