Exemple #1
0
def encode(filename, keys):
    fi = open(filename, 'rb')
    plaintext = fi.read()
    fi.close()

    header_bits, text_bits = split_header_and_content(plaintext)
    text_bits = add_pads_if_necessary(text_bits)

    final_cipher = ''
    for i in range(0, 64):
        text_bits[i] ^= iv[i]

    len_text_bits = len(text_bits)
    for i in range(0, len_text_bits, 64):
        final_cipher += DES(text_bits, i, (i + 64), keys)
        if i < len_text_bits - 64:
            for j in range(i + 64, i + 128):
                text_bits[j] ^= int(final_cipher[j - 64])

    # conversion of binary cipher into hex-decimal form
    fo = open("encrypted_cbc.bmp", "ab")
    header_str = ""
    for each in header_bits:
        header_str += str(each)
    final_cipher = header_str + final_cipher
    i = 0
    print("The length of final_cipher")
    print(len(final_cipher))
    while i < len(final_cipher) - 8:
        val = bin_to_dec(final_cipher[i:i + 4]) * 16 + bin_to_dec(
            final_cipher[i + 4:i + 8])
        fo.write(struct.pack('B', val))
        i += 8
    fo.close()
    print('the cipher is saved in encrypted_ecb.bmp')
def main():
    keys = generate_keys('lqjxliang')

    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))
Exemple #3
0
def encrypt(plaintext, key_text, iv_bits):
	keys = generate_keys(key_text)
	text_bits = get_bits(plaintext)
	text_bits = add_pads_if_necessary(text_bits)
	results = map(int, iv_bits)

	for i in text_bits:
		text_bits[i] ^= results[i]
	final_cipher = ''
	for i in range(0, len(text_bits), 64):
		final_cipher += DES(text_bits, i, (i+64), keys)
Exemple #4
0
def encrypt(plaintext, key_text):
    keys = generate_keys(key_text)

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

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

    # conversion of binary cipher into hex-decimal form
    hex_cipher = ''
    i = 0
    while i < len(final_cipher):
        hex_cipher += bin_to_hex(final_cipher[i:i + 4])
        i = i + 4
    return hex_cipher
Exemple #5
0
def decode(filename, keys):
    fi = open(filename, "rb")
    cipher = fi.read()
    fi.close()

    text_bits = []
    ciphertext = ''
    for i in cipher:
        ciphertext += dec_to_bin(
            ord(i) // 16)  #conversion of hex-decimal form to binary form
        ciphertext += dec_to_bin(ord(i) % 16)

    header_str = ciphertext[0:432]
    ciphertext = ciphertext[432:]
    for i in ciphertext:
        text_bits.append(int(i))

    text_bits = add_pads_if_necessary(text_bits)

    keys.reverse()

    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", 'ab')
    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')
Exemple #6
0
def encrypt(plaintext, key_text):
	keys = generate_keys(key_text)

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

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

	# conversion of binary cipher into hex-decimal form
	hex_cipher = ''
	i = 0
	while i < len(final_cipher):
		hex_cipher += bin_to_hex(final_cipher[i:i+4])
		i = i+4
	return hex_cipher
Exemple #7
0
def encrypt(plaintext, iv_bits):
	key_text = "kij12345"
	keys = generate_keys(key_text)
	text_bits = get_bits(plaintext)
	text_bits = add_pads_if_necessary(text_bits)
	results = map(int, iv_bits)

	for i in text_bits:
		text_bits[i] ^= results[i]
	final_cipher = ''
	for i in range(0, len(text_bits), 64):
		final_cipher += DES(text_bits, i, (i+64), keys)

	hex_cipher = ''
	i = 0
	while i < len(final_cipher):
		hex_cipher += bin_to_hex(final_cipher[i:i+4])
		i = i+4
	return hex_cipher, final_cipher
Exemple #8
0
def decrypt(cipher, key_text):
    keys = generate_keys(key_text)

    text_bits = []
    ciphertext = ''
    for i in cipher:
        # conversion of hex-decimal form to binary form
        ciphertext += hex_to_bin(i)
    for i in ciphertext:
        text_bits.append(int(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)

    i = 0
    text_mess = ''
    while i < len(bin_mess):
        text_mess += bin_to_text(bin_mess[i:i + 8])
        i = i + 8
    return text_mess.rstrip('\x00')
Exemple #9
0
def decrypt(cipher, key_text):
	keys = generate_keys(key_text)

	text_bits = []
	ciphertext = ''
	for i in cipher:
		# conversion of hex-decimal form to binary form
		ciphertext += hex_to_bin(i)
	for i in ciphertext:
		text_bits.append(int(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)

	i = 0
	text_mess = ''
	while i < len(bin_mess):
		text_mess += bin_to_text(bin_mess[i:i+8])
		i = i+8
	return text_mess.rstrip('\x00')
def decode(filename, keys):
    fi = open(filename, "rb")
    cipher = fi.read()
    fi.close()
    text_bits = []
    ciphertext = ''

    for i in cipher:
        ciphertext += dec_to_bin(ord(i) // 16)
        ciphertext += dec_to_bin(ord(i) % 16)

    header_str = ciphertext[0:432]
    ciphertext = ciphertext[432:]

    for i in ciphertext:
        text_bits.append(int(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", 'ab')
    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 #11
0
def decrypt(cipher): #cipher hexadecimal dan key 8 character
	key_text = "kij12345"
	keys = generate_keys(key_text) # key dirubah ke biner
	text_bits = [] 
	ciphertext = ''

	ciphertemp = []
	for i in cipher: 
		# conversion of hex-decimal form to binary form
		ciphertext += hex_to_bin(i)
	ciphertemp = str(ciphertext)
	for i in ciphertemp:
		text_bits.append(i)
	xx = 0
	text_temp = []
	for xx in range(0,len(text_bits)/64):
		ho = len(text_bits)/64
		temp = []
		aa = 0
		while aa < 64:
			temp += text_bits.pop(0)
			aa = aa + 1
		temp_new = []
		for i in temp:
			temp_new.append(int(i))
		#print "keys sebelum reverse->",keys		
		
		if xx == 0:
			keys.reverse()
		bin_mess = ''
		for i in range(0, len(temp_new), 64):
			bin_mess += DES(temp_new, i, (i+64), keys)
		i = 0
		text_mess = ''
		while i < len(bin_mess):
			text_mess += bin_to_text(bin_mess[i:i+8])
			i = i+8
		# print "ini text mess bawah->", text_mess
		final_gan = []
		final_gan.append(text_mess.rstrip('\x1c'))
		#print "ini hasil iterasi ke ", xx+1, final_gan
		text_temp.append(text_mess)
		final_temp = ''.join(text_temp)
	return final_temp.rstrip('\x00')

	for i in temp:
		# conversion of hex-decimal form to binary form
		ciphertext += hex_to_bin(i)
	for i in ciphertext:
		text_bits.append(int(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)

	i = 0
	text_mess = ''
	while i < len(bin_mess):
		text_mess  += bin_to_text(bin_mess[i:i+8])
		i = i+8
	return text_mess.rstrip('\x00')
Exemple #12
0
			i = i+8
		# print "ini text mess bawah->", text_mess
		final_gan = []
		final_gan.append(text_mess.rstrip('\x1c'))
		#print "ini hasil iterasi ke ", xx+1, final_gan
		text_temp.append(text_mess)
		final_temp = ''.join(text_temp)
	return final_temp.rstrip('\x00')
=======
	for i in temp:
		# conversion of hex-decimal form to binary form
		ciphertext += hex_to_bin(i)
	for i in ciphertext:
		text_bits.append(int(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)

	i = 0
	text_mess = ''
	while i < len(bin_mess):
		text_mess  += bin_to_text(bin_mess[i:i+8])
		i = i+8
	return text_mess.rstrip('\x00')

>>>>>>> f457486e1e291560a357694f11c3fcc8df033188
def main():
    print('Encrypt = 1')