示例#1
0
def crypter(input_path, key, way):
    with open(input_path, 'rb') as f:
        data = f.read()

    crypted_part = []
    decrypted_part = []
    if way == '1':
        crypted_data = []
        temp = []
        for byte in data:
            temp.append(byte)
            if len(temp) == 16:
                crypted_part = aes128.encrypt(temp, key)
                crypted_data.extend(crypted_part)
                del temp[:]
        else:
            if 0 < len(temp) < 16:
                empty_spaces = 16 - len(temp)
                for i in range(empty_spaces - 1):
                    temp.append(0)
                temp.append(0)
                crypted_part = aes128.encrypt(temp, key)
                crypted_data.extend(crypted_part)

        out_path = os.path.dirname(
            input_path) + '/encrypted_' + os.path.basename(input_path)

        if os.path.exists(out_path):
            os.remove(out_path)
        with open(out_path, 'xb') as ff:
            ff.write(bytes(crypted_data))

    else:  # way == 2
        decrypted_data = []
        temp = []
        for byte in data:
            temp.append(byte)
            if len(temp) == 16:
                decrypted_part = aes128.decrypt(temp, key)
                decrypted_data.extend(decrypted_part)
                del temp[:]
        else:
            if 0 < len(temp) < 16:
                print("yes")
                empty_spaces = 16 - len(temp)
                for i in range(empty_spaces - 1):
                    temp.append(0)
                temp.append(1)
                decrypted_part = aes128.encrypt(temp, key)
                decrypted_data.extend(crypted_part)

        out_path = os.path.dirname(
            input_path) + '/decrypted_' + os.path.basename(input_path)

        if os.path.exists(out_path):
            os.remove(out_path)
        with open(out_path, 'xb') as ff:
            ff.write(bytes(decrypted_data))
示例#2
0
 def cbc_decrypt(ciphertext, key):
     blocks = ciphertext
     plaintext = []
     for i in range(len(blocks) - 1, -1, -1):
         block = decrypt(blocks[i], key)
         block = [(i ^ j) for (i, j) in zip(blocks[i - 1], block)]
         plaintext.append(block)
     pl = ''
     for i in range(len(plaintext) - 2, -1, -1):
         for j in range(len(plaintext[i])):
             pl += str(plaintext[i][j])
     return pl
示例#3
0
文件: m3crypto.py 项目: maximus3/AES
def decrypt(key, data='', filename=None):

    if not check_key(key):
        return None

    if filename:
        # Input data
        with open(filename, 'rb') as f:
            data = f.read()
    else:
        try:
            data = data.encode('utf-8')
        except AttributeError:
            pass

    decrypted_data = []
    temp = []
    for byte in data:
        temp.append(byte)
        if len(temp) == 16:
            decrypted_part = aes128.decrypt(temp, key)
            decrypted_data.extend(decrypted_part)
            del temp[:]
    else:
        #padding v1
        # decrypted_data.extend(temp)

        # padding v2
        if 0 < len(temp) < 16:
            empty_spaces = 16 - len(temp)
            for i in range(empty_spaces - 1):
                temp.append(0)
            temp.append(1)
            decrypted_part = aes128.encrypt(temp, key)
            decrypted_data.extend(crypted_part)

        # Delete ...000001 path
        if decrypted_data[-1] == 1:
            decrypted_data.pop()
            while len(decrypted_data) > 0 and decrypted_data[-1] == 0:
                decrypted_data.pop()

    if filename:
        out_path = os.path.join(os.path.dirname(filename),
                                'decrypted_' + os.path.basename(filename))

        # Ounput data
        with open(out_path, 'xb') as ff:
            ff.write(bytes(decrypted_data))
        return out_path
    else:
        return bytes(decrypted_data).decode('utf-8')
def decrypt(in_path,aeskey,mode):
    if len(aeskey) == 16:
        import aes128 as aes
    elif len(aeskey) == 24:
        import aes192 as aes
    elif len(aeskey) == 32:
        import aes256 as aes

    iv = make_iv()
    aeskey = aes.key_expansion(aeskey)
    input_path = os.path.abspath(in_path)


    with open(input_path, 'rb') as f:
        data = f.read()

    decrypted_data = []
    temp = []
    for byte in data:
        temp.append(byte)
        #iv2.append(byte)
        if len(temp) == 16:
            # print(iv)
            # print(temp)
            if mode == 'cbc':
                decrypted_part = aes.decrypt(temp, aeskey)
                decrypted_part = xor(decrypted_part, iv)
                iv = list(temp)
                decrypted_data.extend(decrypted_part)
                del temp[:]
            else:
                decrypted_part = aes.decrypt(temp, aeskey)
                decrypted_data.extend(decrypted_part)
                del temp[:]


    return decrypted_data
示例#5
0
def decrypt(chiper_text, key):
    decrypted_data = []
    temp = []
    for byte in chiper_text:
        temp.append(byte)
        if len(temp) == 16:
            decrypted_part = aes128.decrypt(temp, key)
            decrypted_data.extend(decrypted_part)
            del temp[:]
    else:
        #padding v1
        # decrypted_data.extend(temp)

        # padding v2
        if 0 < len(temp) < 16:
            empty_spaces = 16 - len(temp)
            for i in range(empty_spaces - 1):
                temp.append(0)
            temp.append(1)
            decrypted_part = aes128.encrypt(temp, key)
            decrypted_data.extend(crypted_part)
    return decrypted_data
def main():
    config_file_name = input('Input config data file name: ')
    data_file_name = input("Input data file name: ")
    encrypted_file_name = encrypt_name(data_file_name)
    decrypted_file_name = data_file_name.split('.')[0] + '_decrypted.' + data_file_name.split('.')[1]

    with open(config_file_name, 'rb') as config_file:
        key = read_bites(config_file)  # [int.from_bytes(i) for i in config_file.read().split()]
        config_file.close()
    with open(data_file_name, 'rb') as data_file:
        data = read_bites(data_file)  # data_file.read().split()
        # print("input_data = ", data)
        data_file.close()
    with open(encrypted_file_name, 'wb') as encrypted_file:
        encrypted = encrypt(data, key)
        # print("encrypted = ", encrypted)
        write_bytes(encrypted_file, encrypted)
        encrypted_file.close()
    with open(decrypted_file_name, 'wb') as decrypted_file:
        decrypted = decrypt(encrypted, key)
        # print('encrypted = ', decrypted)
        write_bytes(decrypted_file, decrypted)
        decrypted_file.close()
示例#7
0
                crypted_data.extend(crypted_part)

        out_path = os.path.join(os.path.dirname(input_path),
                                'crypted_' + os.path.basename(input_path))

        # Ounput data
        with open(out_path, 'xb') as ff:
            ff.write(bytes(crypted_data))

    else:  # if way == '2'
        decrypted_data = []
        temp = []
        for byte in data:
            temp.append(byte)
            if len(temp) == 16:
                decrypted_part = aes128.decrypt(temp, key)
                decrypted_data.extend(decrypted_part)
                del temp[:]
        else:
            #padding v1
            # decrypted_data.extend(temp)

            # padding v2
            if 0 < len(temp) < 16:
                empty_spaces = 16 - len(temp)
                for i in range(empty_spaces - 1):
                    temp.append(0)
                temp.append(1)
                decrypted_part = aes128.encrypt(temp, key)
                decrypted_data.extend(crypted_part)
示例#8
0
文件: main.py 项目: GreamDesu/AES
                crypted_part = aes128.encrypt(temp, key)
                crypted_data.extend(crypted_part)

        out_path = os.path.join(os.path.dirname(input_path) , 'crypted_' + os.path.basename(input_path))

        # Ounput data
        with open(out_path, 'xb') as ff:
            ff.write(bytes(crypted_data))

    else: # if way == '2'
        decrypted_data = []
        temp = []
        for byte in data:
            temp.append(byte)
            if len(temp) == 16:
                decrypted_part = aes128.decrypt(temp, key)
                decrypted_data.extend(decrypted_part)
                del temp[:] 
        else:
            #padding v1
            # decrypted_data.extend(temp)
            
            # padding v2
            if 0 < len(temp) < 16:
                empty_spaces = 16 - len(temp)
                for i in range(empty_spaces - 1):
                    temp.append(0)
                temp.append(1)
                decrypted_part = aes128.encrypt(temp, key)
                decrypted_data.extend(crypted_part) 
示例#9
0
        IV = bytearray('qwertyuiopasdfgh')
        C = IV
        C_min_1 = []
        decrypted_part = C
        temp = []
        i_dec = 1
        for byte in crypted_data:
            temp.append(byte)
	    #print str(len(temp))
            if len(temp) == 16:
                
                if i_dec < 2:
                    C_min_1.extend(temp)
                    print C_min_1
                    print key
                    print str(aes128.decrypt(temp, key))
                    decrypted_part = logical_xor(C,aes128.decrypt(temp, key))
                    #C_min_1 = aes128.decrypt(temp, key)
                    #print print_byte_of_array(C_min_1)
                    print '1'
                    print print_byte_of_array(decrypted_part)
                    print C_min_1
                    
                else:
                    print aes128.decrypt(temp, key)
                    #print 'zdy change!'
                    print C_min_1
                    decrypted_part = logical_xor(C_min_1,aes128.decrypt(temp, key))
                    print C_min_1
                    print str(C_min_1) + " " + str(aes128.decrypt(temp, key))
                    #C_min_1 = aes128.decrypt(temp, key)