def main():
    keys = generate_keys("lqjxliang")

    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)
Exemplo n.º 2
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)
Exemplo n.º 3
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
Exemplo n.º 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
Exemplo n.º 5
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
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))
Exemplo n.º 7
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')
Exemplo n.º 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')
Exemplo n.º 9
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')
Exemplo n.º 10
0
def decrypt(cipher, key_text): #cipher hexadecimal dan key 8 character
	keys = generate_keys(key_text) # key dirubah ke biner
	text_bits = [] 
	ciphertext = ''
Exemplo n.º 11
0
def decrypt(temp, key_text):
	keys = generate_keys(key_text)
Exemplo n.º 12
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", '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')


if __name__ == '__main__':
    keys = generate_keys('lqjxliang')
    encode('img.bmp', keys)
    #decode('leena.bmp', keys)