Exemple #1
0
def generate_keys(key_text):
    key = []
    for i in key_text:
        key.extend(to_binary(ord(i)))

    C = []
    D = []
    r = []
    for i in range(28):
        C.append(key[PC1_C[i]])
    for i in range(28):
        D.append(key[PC1_D[i]])
    for i in range(0, 16):
        if i in [0, 1, 8, 15]:
            C = left_shift(C, 1)
            D = left_shift(D, 1)
        else:
            C = left_shift(C, 2)
            D = left_shift(D, 2)
        CD = []
        CD.extend(C)
        CD.extend(D)
        dummy = []
        for i in range(48):
            dummy.append(CD[PC2[i]])
        r.append(dummy)
    return r
Exemple #2
0
def linear_s_box():
    s_box = [[0 for col in range(16)] for row in range(4)]
    bin = []
    for i in range(64):
        bin = to_binary(i)
        s_box[bin[2] * 2 + b[7]][b[3] * 8 + b[4] * 4 + b[5] * 2 +
                                 b[6]] = b[3] * 8 + b[4] * 4 + b[5] * 2 + b[6]
Exemple #3
0
def decode(filename, keys):
    fi = open(filename, "rb")
    cipher = fi.read()
    fi.close()
    cipher_bits = []
    header_str = ''

    for i in cipher:
        cipher_bits.extend(to_binary(ord(i)))

    header_bits = cipher_bits[0:HEADER_LENGTH]
    text_bits = cipher_bits[HEADER_LENGTH:]
    for i in header_bits:
        header_str += str(i)

    text_bits = add_pads_if_necessary(text_bits)

    keys.reverse()
    bin_mess = ''
    for i in range(0, len(text_bits), 64):
        bin_mess += DES(text_bits, i, (i + 64), keys)

    text_mess = header_str + bin_mess
    i = 0
    fo = open("decrypted_ecb.bmp", 'wb')
    print("The length of final_cipher")
    print(len(text_mess))
    while i < len(text_mess) - 8:
        val = bin_to_dec(text_mess[i:i + 4]) * 16 + bin_to_dec(text_mess[i + 4:i + 8])
        fo.write(struct.pack('B', val))
        i += 8
    fo.close()

    print('the original image has been saved in decrypted_ecb.bmp')
Exemple #4
0
def generate_keys(key_text):
    key = []
    for i in key_text:
        key.extend(to_binary(ord(i)))

    C = []
    D = []
    r = []
    for i in range(28):
        C.append(key[PC1_C[i]])
    for i in range(28):
        D.append(key[PC1_D[i]])
    for i in range(0, 16):
        if i in [0, 1, 8, 15]:
            C = left_shift(C, 1)
            D = left_shift(D, 1)
        else:
            C = left_shift(C, 2)
            D = left_shift(D, 2)
        CD = []
        CD.extend(C)
        CD.extend(D)
        dummy = []
        for i in range(48):
            dummy.append(CD[PC2[i]])
        r.append(dummy)
    return r
Exemple #5
0
def decode(filename, keys):
    fi = open(filename, "rb")
    cipher = fi.read()
    fi.close()
    cipher_bits = []
    header_str = ''

    for i in cipher:
        cipher_bits.extend(to_binary(ord(i)))

    header_bits = cipher_bits[0:HEADER_LENGTH]
    text_bits = cipher_bits[HEADER_LENGTH:]
    for i in header_bits:
        header_str += str(i)

    text_bits = add_pads_if_necessary(text_bits)

    keys.reverse()
    bin_mess = ''
    for i in range(0, len(text_bits), 64):
        bin_mess += DES(text_bits, i, (i + 64), keys)

    text_mess = header_str + bin_mess
    i = 0
    fo = open("decrypted_ecb.bmp", 'wb')
    print("The length of final_cipher")
    print(len(text_mess))
    while i < len(text_mess) - 8:
        val = bin_to_dec(text_mess[i:i + 4]) * 16 + bin_to_dec(
            text_mess[i + 4:i + 8])
        fo.write(struct.pack('B', val))
        i += 8
    fo.close()

    print('the original image has been saved in decrypted_ecb.bmp')
Exemple #6
0
def non_linear_s_box():
    s_box = []
    for i in range(64):
        s_box.append(0)
    for i in range(64):
        b = to_binary(i)
        s_box[(b[2] * 2 + b[7]) * 16 + b[3] * 8 + b[4] * 4 + b[5] * 2 + b[6]] = b[3] ** 4 + b[4] ** 3 + b[5] ** 2 + b[6]
    return s_box
Exemple #7
0
def non_linear_s_box():
    s_box = []
    for i in range(64):
        s_box.append(0)
    for i in range(64):
        b = to_binary(i)
        s_box[(b[2] * 2 + b[7]) * 16 + b[3] * 8 + b[4] * 4 + b[5] * 2 +
              b[6]] = b[3]**4 + b[4]**3 + b[5]**2 + b[6]
    return s_box
Exemple #8
0
def split_header_and_content(img):
    """plain text to binary, split header and content for image
    """
    cipher_bits = []
    for i in img:
        cipher_bits.extend(to_binary(ord(i)))
    header_bits = cipher_bits[0:HEADER_LENGTH]
    text_bits = cipher_bits[HEADER_LENGTH:]
    return header_bits, text_bits
Exemple #9
0
def split_header_and_content(img):
    """plain text to binary, split header and content for image
    """
    cipher_bits = []
    for i in img:
        cipher_bits.extend(to_binary(ord(i)))
    header_bits = cipher_bits[0: HEADER_LENGTH]
    text_bits = cipher_bits[HEADER_LENGTH:]
    return header_bits, text_bits
Exemple #10
0
def s_box(block):
    """s-box function to reduce 48 bit block to 32 bit block
    """
    for i in range(0, 8):
        #获得行数
        row = str(block[i][0]) + str(block[i][-1])
        column = ''
        for j in range(1, 5):
            column += str(block[i][j])
        a = 16 * get_row(row)
        #获得列数
        a += get_column(column)
        block.pop(i)
        block.insert(i, to_binary(ord(chr(s[i][a]))))
    r = []
    for i in block:
        r.extend(i[4:8])
    return r
Exemple #11
0
def s_box(block):
    """s-box function to reduce 48 bit block to 32 bit block
    """
    for i in range(0, 8):
        #获得行数
        row = str(block[i][0]) + str(block[i][-1])
        column = ''
        for j in range(1, 5):
            column += str(block[i][j])
        a = 16 * get_row(row)
        #获得列数
        a += get_column(column)
        block.pop(i)
        block.insert(i, to_binary(ord(chr(s[i][a]))))
    r = []
    for i in block:
        r.extend(i[4:8])
    return r
Exemple #12
0
def linear_s_box():
    s_box = [[0 for col in range(16)] for row in range(4)]
    bin = []
    for i in range(64):
        bin = to_binary(i)
        s_box[bin[2] * 2 + b[7]][b[3] * 8 + b[4] * 4 + b[5] * 2 + b[6]] = b[3] * 8 + b[4] * 4 + b[5] * 2 + b[6]