Exemplo n.º 1
0
def main():
    if len(sys.argv) < 2 or sys.argv[1] not in args:
        print_usage()

    elif sys.argv[1] == '-k':
        if len(sys.argv) != 3:
            print_usage()
        elif not is_power_of_two(int(
                sys.argv[2])) or int(sys.argv[2]) < pow(2, 7):
            print("Modulus bit length must be power of two, 2^k with k > 6")
        else:
            bit_len = int(sys.argv[2])
            # bit_len / (# bits in a byte * 2 (each key half the total length))
            N, e, d = key_gen.key_gen(bit_len / 16)
            print_key(N, e, d, bit_len)
    elif sys.argv[1] == '-e':
        if len(sys.argv) != 5:
            print_usage()
        else:
            N = int(sys.argv[2], base=16)
            e = int(sys.argv[3], base=16)
            filename = sys.argv[4]
            algorithm.encrypt(N, e, filename)
    elif sys.argv[1] == '-d':
        if len(sys.argv) != 5:
            print_usage()
        else:
            N = int(sys.argv[2], base=16)
            d = int(sys.argv[3], base=16)
            filename = sys.argv[4]
            algorithm.decrypt(N, d, filename)
Exemplo n.º 2
0
def main():
    if len(sys.argv) < 2 or sys.argv[1] not in args:
        print_usage()

    elif sys.argv[1] == '-k':
        if len(sys.argv) != 3:
            print_usage()
        elif not is_power_of_two(int(sys.argv[2])) or int(sys.argv[2]) < pow(2,7):
            print("Modulus bit length must be power of two, 2^k with k > 6")
        else:
            bit_len = int(sys.argv[2])
            # bit_len / (# bits in a byte * 2 (each key half the total length))
            N, e, d = key_gen.key_gen(bit_len / 16)
            print_key(N, e, d, bit_len)
    elif sys.argv[1] == '-e':
        if len(sys.argv) != 5:
            print_usage()
        else:
            N = int(sys.argv[2], base=16)
            e = int(sys.argv[3], base=16)
            filename = sys.argv[4]
            algorithm.encrypt(N, e, filename)
    elif sys.argv[1] == '-d':
        if len(sys.argv) != 5:
            print_usage()
        else:
            N = int(sys.argv[2], base=16)
            d = int(sys.argv[3], base=16)
            filename = sys.argv[4]
            algorithm.decrypt(N, d, filename)
Exemplo n.º 3
0
def search_right_key( keys ):
	infoXL, infoXR, infoYL, infoYR = get_correct_texts( conf.FILENAME_COR_TEXTS )

	plain_text = ( infoXL[ 0 ][ 0 ] << conf.HALF_BLOCK_SIZE ) | ( infoXR[ 0 ][ 0 ] )
	cipher_text = ( infoYL[ 0 ][ 0 ] << conf.HALF_BLOCK_SIZE ) | ( infoYR[ 0 ][ 0 ] )
	
	result_key = 0
	for current_key in  keys:
		current_cipher_text = algorithm.encrypt( plain_text, current_key )
		if current_cipher_text == cipher_text:
			result_key = current_key
			print "result_key =", bits_ops.full_bin( result_key, conf.KEY_SIZE )
	if result_key == 0:
		print "Can't find right key!"

	return result_key
Exemplo n.º 4
0
def search_right_key( text ):
	key = 0
	possible_keys = list()
	print "Please, input key's bits:"
	for i in range( cfg.KEY_SIZE ):
		print "K" + str( i + 1 ) + ":",
		current_bit = raw_input()
		possible_keys = add_bit_in_possible_keys( current_bit, i + 1, possible_keys )
	
	plain_text, cipher_text = text

	print len( possible_keys )

	for current_key in possible_keys:
			current_cipher_text = algorithm.encrypt( plain_text, current_key )
			if current_cipher_text == cipher_text:
				key = current_key
				print "result key:", bops.full_bin( key, cfg.KEY_SIZE)
	return key