示例#1
0
def encrypt(plaintext, seed_bytes):
    ciphertext = pad(b64decode(plaintext), BLOCK_SIZE)
    seed_bytes = b64decode(seed_bytes)
    assert len(seed_bytes) >= 8
    for seed in seed_bytes:
        ciphertext = AES.new(rand_block(seed),
                             AES.MODE_ECB).encrypt(ciphertext)

    return b64encode(ciphertext)
示例#2
0
def Encrypt():
    aes = AES.new(key, AES.MODE_CBC, iv)  # This is the encryption cipher
    print('#####################'
          '                     '
          'PLEASE INPUT ONLY 16 BYTES STRING'
          '                     '
          '#####################')
    global data
    data = input()  # <- 16 bytes OR add buffer to make it 16 bytes
    encd = aes.encrypt(pad(data, 16))
    return encd
示例#3
0
def gen_chall(text):
    text = pad(text, BLOCK_SIZE)
    for i in range(128):
        text = AES.new(rand_block(), AES.MODE_ECB).encrypt(text)

    return b64encode(text)
示例#4
0
def encrypt():

    label_alert.configure(text="")

    original_message = entry_field.get()
    original_message = verify_input_msg(original_message)

    key = verify_input_keyword(entry_key.get())
    if not key and combo_cipher.get() != "One Time Pad":
        label_alert.configure(text="Preencha o campo chave")

    else:
        if combo_cipher.get() == "Cesar":
            if key.isdigit():
                key = int(key)
                crypted_message = Caesar_cipher.caesar_cipher(
                    original_message, key)
                clean_entry_field(crypted_message)
            else:
                label_alert.configure(
                    text="Escreva a penas números no campo Chave")

        elif combo_cipher.get() == "Hill":
            key = Hill_cipher.make_key(key)
            crypted_message = Hill_cipher.encrypt(original_message, key)
            clean_entry_field(crypted_message)

        elif combo_cipher.get() == "Vigenère":
            crypted_message = Vigenère_cipher.encrypt(original_message, key)
            clean_entry_field(crypted_message)

        elif combo_cipher.get() == "Playfair":

            crypted_message = Playfair_cipher.encrypt(original_message, key)
            clean_entry_field(crypted_message)

        elif combo_cipher.get() == "One Time Pad":
            crypted_message, key = One_time_pad.encrypt(original_message)
            global master_key
            master_key = ''.join(key)
            entry_field.delete(0, 1000)
            entry_key.delete(0, 1000)
            entry_field.insert(0, crypted_message)
            entry_key.insert(0, master_key)

        elif combo_cipher.get() == "DES - ECB":
            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = des.ecb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "DES - CBC":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = des.cbc(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "DES - CFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = des.cfb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "DES - OFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = des.ofb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "DES - CTR":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = des.ctr(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "3DES - ECB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = Three_des.ecb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "3DES - CBC":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = Three_des.cbc(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "3DES - CFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = Three_des.cfb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "3DES - OFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = Three_des.ofb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "3DES - CTR":

            original_message = str.encode(original_message)
            original_message = pad(original_message, BLOCK_SIZE)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = Three_des.ctr(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "AES - ECB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, 16)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = AES.ecb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "AES - CBC":
            original_message = str.encode(original_message)
            original_message = pad(original_message, 16)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = AES.cbc(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "AES - CFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, 16)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = AES.cfb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "AES - OFB":

            original_message = str.encode(original_message)
            original_message = pad(original_message, 16)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = AES.ofb(key, original_message, 1)
                clean_entry_field(crypted_message)

        elif combo_cipher.get() == "AES - CTR":

            original_message = str.encode(original_message)
            original_message = pad(original_message, 16)

            try:
                key = str.encode(key)
            except TypeError as e:
                print("TypeError while handling the key:", e)
                label_alert.configure(text="Erro relacionado ao campo Chave")
            else:
                crypted_message = AES.ctr(key, original_message, 1)
                clean_entry_field(crypted_message)
示例#5
0
import struct
from Padding import pad, unpad
sz = 2048
fsz = os.path.getsize('/Users/goofy/Desktop/test.txt')
with open('test.txt') as finput:
    while True:
        global data
        data = finput.read(sz)
        n = len(data)
        if n == 0:
            break
        # elif n % 16 != 0:
        # data += ' ' * (16 - n % 16)  # <- padded with spaces
        aes = AES.new(key, AES.MODE_CBC, iv)
        with open('encfile.txt', 'wb') as fout:
            # fout.write(iv)
            encd = aes.encrypt(pad(data, 16))
            #cd)
            #

            fout.write(encd)
            # fout.write(struct.pack('<Q', fsz))

        # with open('/Users/goofy/Desktop/encfile.txt', 'wb') as fout:
        #      fout.write(struct.pack('<Q', fsz))
        #fout.write(iv)

# if data == decd:
#     print("Working with success")
# else:
#     print("Try again")