Example #1
0
def main():
    keys = generate_keys("guogaoyang")

    plaintext_list = []
    ciphertext_list = []
    for i in range(256):
        t = [1]
        diff = random.randint(1, 63)
        for j in range(63):
            if j == diff:
                t.append(1)
            else:
                t.append(0)

        plaintext_list.append(t)

    for plaintext in plaintext_list:
        ciphertext_list.append(DES(plaintext, 0, 64, keys))

    # Do statistics
    count = []
    rate = []
    for i in range(64):
        t = 0
        for ciphertext in ciphertext_list:
            if ciphertext[i] == "0":
                t += 1
        count.append(t)
        rate.append(t / 256.0)
    print count
    print rate
Example #2
0
def main():
    keys = generate_keys("guogaoyang")

    plaintext_list = []
    ciphertext_list = []
    for i in range(256):
        t = [1]
        diff = random.randint(1, 63)
        for j in range(63):
            if j == diff:
                t.append(1)
            else:
                t.append(0)

        plaintext_list.append(t)

    for plaintext in plaintext_list:
        ciphertext_list.append(DES(plaintext, 0, 64, keys))

    # Do statistics
    count = []
    rate = []
    for i in range(64):
        t = 0
        for ciphertext in ciphertext_list:
            if ciphertext[i] == '0':
                t += 1
        count.append(t)
        rate.append(t / 256.0)
    print count
    print rate
Example #3
0
def main():
    keys = generate_keys('guogaoyang')

    plaintext = str(raw_input('Enter the first message to be encrypted\n'))

    text_bits = get_bits(plaintext)
    text_bits = add_pads_if_necessary(text_bits)

    CIPHERS1 = []
    for i in range(0, len(text_bits), 64):
        DES(text_bits, i, (i + 64), keys, CIPHERS1)

    text_bits = []
    plaintext = str(raw_input('Enter the second message to be encrypted\n'))

    text_bits = get_bits(plaintext)
    text_bits = add_pads_if_necessary(text_bits)

    CIPHERS2 = []
    for i in range(0, len(text_bits), 64):
        DES(text_bits, i, (i + 64), keys, CIPHERS2)

    print("for plaintext one:")
    for i in range(16):
        ans = ""
        for each in CIPHERS1[i]:
            ans += str(each)
        print("The cipher after " + str(i) + " rounds is " + ans)

    print("for plaintext two:")
    for i in range(16):
        ans = ""
        for each in CIPHERS2[i]:
            ans += str(each)
        print("The cipher after " + str(i) + " rounds is " + ans)


    for i in range(16):
        count = 0
        for j in range(64):
            if not CIPHERS2[i][j] == CIPHERS1[i][j]:
                count += 1
        print("After " + str(i) + " round differnt bits is: " + str(count))
Example #4
0
def main():
    keys = generate_keys('guogaoyang')

    plaintext = str(raw_input('Enter the first message to be encrypted\n'))

    text_bits = get_bits(plaintext)
    text_bits = add_pads_if_necessary(text_bits)

    CIPHERS1 = []
    for i in range(0, len(text_bits), 64):
        DES(text_bits, i, (i + 64), keys, CIPHERS1)

    text_bits = []
    plaintext = str(raw_input('Enter the second message to be encrypted\n'))

    text_bits = get_bits(plaintext)
    text_bits = add_pads_if_necessary(text_bits)

    CIPHERS2 = []
    for i in range(0, len(text_bits), 64):
        DES(text_bits, i, (i + 64), keys, CIPHERS2)

    print("for plaintext one:")
    for i in range(16):
        ans = ""
        for each in CIPHERS1[i]:
            ans += str(each)
        print("The cipher after " + str(i) + " rounds is " + ans)

    print("for plaintext two:")
    for i in range(16):
        ans = ""
        for each in CIPHERS2[i]:
            ans += str(each)
        print("The cipher after " + str(i) + " rounds is " + ans)

    for i in range(16):
        count = 0
        for j in range(64):
            if not CIPHERS2[i][j] == CIPHERS1[i][j]:
                count += 1
        print("After " + str(i) + " round differnt bits is: " + str(count))
Example #5
0
    bin_list = []
    for i in range(0, len(text_bits), 64):
        bin_list += DES(text_bits, i, (i + 64), keys)
        if i > 0:
            for j in range(i, i + 64):
                bin_list[j] = str(int(bin_list[j]) ^ text_bits[j - 64])
        else:
            for j in range(i, i + 64):
                bin_list[j] = str(int(bin_list[j]) ^ iv[j])

    bin_mess = "".join(bin_list)
    text_mess = header_str + bin_mess

    i = 0
    fo = open("decrypted_cbc.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_cbc.bmp')


if __name__ == '__main__':
    keys = generate_keys('guogaoyang')
    encode('lena.bmp', keys)
    decode('encrypted_cbc.bmp', keys)
Example #6
0
    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')


if __name__ == '__main__':
    keys = generate_keys('guogaoyang')
    encode('lena.bmp', keys)
    decode('encrypted_ecb.bmp', keys)