def decrypt(text, cipher):
    maes = MiniAES()
    bin = BinaryStrings()
    plain = ascii_to_bin(text)
    cipher = bin_to_ascii(cipher)
    cipher = ascii_to_bin(cipher)
    table = {}
    for x in range(_sage_const_0, _sage_const_16):
        for y in range(_sage_const_0, _sage_const_16):
            for z in range(_sage_const_0, _sage_const_16):
                for q in range(_sage_const_0, _sage_const_16):
                    key = [x, y, z, q]
                    key = maes.integer_to_binary(key)
                    encryption = maes(plain, key, algorithm="encrypt")
                    table[bin_to_ascii(encryption)] = key
#    print("First Half Done !!!!!!!!!!!!!!")
    for x in range(_sage_const_0, _sage_const_16):
        for y in range(_sage_const_0, _sage_const_16):
            for z in range(_sage_const_0, _sage_const_16):
                for q in range(_sage_const_0, _sage_const_16):
                    key = [x, y, z, q]
                    key = maes.integer_to_binary(key)
                    decryption = bin_to_ascii(
                        maes(cipher, key, algorithm="decrypt"))
                    if decryption in table.keys():
                        k1 = table[decryption]
                        k2 = key
                        print("matched key1 in binary: " +
                              str(table[decryption]))
                        print("matched key2 in binary: " + str(key))
                        print("key1 in ASCII: " +
                              bin_to_ascii(table[decryption]))
                        print("key2 in ASCII: " + bin_to_ascii(key))
                        return (k1, k2)
def verify(text, cipher, k1, k2):
    maes = MiniAES()
    bin = BinaryStrings()
    plain = ascii_to_bin(text)
    cipher = bin_to_ascii(cipher)
    cipher = ascii_to_bin(cipher)
    k1 = maes.integer_to_binary(k1)
    k2 = maes.integer_to_binary(k2)
    encryption = bin_to_ascii(maes(plain, k1, algorithm="encrypt"))
    decryption = bin_to_ascii(maes(cipher, k2, algorithm="decrypt"))
    if encryption == decryption:
        return true
Ejemplo n.º 3
0
def textToBlocks():
    #lainText = raw_input("input plain text: ")
    plainText = "mytestbigmessage"
    block = []
    binString = str(ascii_to_bin(plainText))
    count = len(binString) / 128
    if len(binString) % 128:
        binString = binString + (7 * '0' + '1') * (
            (128 - len(binString) % 128) / 8)
        count += 1
    for i in range(count):
        block.append([
            int(binString[j:j + 8], 2)
            for j in range(i * 128, (i + 1) * 128, 8)
        ])
    return block, count
Ejemplo n.º 4
0
import operator
from sage.crypto.util import ascii_to_bin


# converts list of chars to string
def to_str(h):
    if (len(h) < 1):
        return ""
    return reduce(operator.add, h)


little_endian = lambda x: int(
    to_str(list(reversed(split_every(format(x, "064b"), 8)))), 2)
word_to_int = lambda x: int(str(ascii_to_bin(x)), 2)


# split list for lists of lengths of n
def split_every(list, n):
    return [list[i:i + n] for i in range(0, len(list), n)]


# in bytes
def pad_word(word, size):
    ext_len = size - len(word) % size
    if (ext_len == 1):
        word += chr(0x86)
        return word
    word += to_str([chr(0x06)] + [chr(0x0)] * (ext_len - 2) + [chr(0x80)] *
                   (ext_len - abs(ext_len - 2) - 1))
    return word
Ejemplo n.º 5
0
def i2s(list_of_blocks,block_size):
    S = ''
    for i in list_of_blocks:
        s = "{0:b}".format(int(i))
        S += '0'*(block_size-len(s)) + s
    return bin_to_ascii(S)



mess = raw_input("Input message: ")

N = input("Input size of prime numbers in bits(>8): ")
public_key, private_key = GenerateKey(N)

bin_mess = ascii_to_bin(mess)
#print bin_mess
block_size = input("Input block size in bits < N and = 0(mod 8): ")
mod = len(bin_mess)%block_size
if mod != 0:
    bin_mess = '0'*(block_size-mod) + str(bin_mess)
#print bin_mess
list_of_blocks = []

for i in range(0,len(bin_mess),block_size):
    #print (str((bin_mess[i: i + block_size])))
    list_of_blocks.append(int(str((bin_mess[i: i + block_size])), base = 2))
#print list_of_blocks
DT = ''
cipher_text =  encrypt(list_of_blocks,public_key)
#print cipher_text