예제 #1
0
	def __get_stat( self, X, Y, alfa, beta ):
		bits_X = range( 4 )[::-1]
		if self.num != 3:
			bits_Y = range( 3 )[::-1]
		else:
			bits_Y = range( 2 )[::-1]
		result = 0
		for i in bits_X:
			result ^= bits_ops.get_bit( X, i ) & bits_ops.get_bit( alfa, i )
		for j in bits_Y:
			result ^= bits_ops.get_bit( Y, j ) & bits_ops.get_bit( beta, j )

		return result
예제 #2
0
def search_num_right_texts( texts, analogs, NTexts ):
	analogs_with_num = list()
	for analog in analogs:
		current_num = 0
		for text in texts:
			result = 0
			for i in analog[ 0 ]:
				result ^= bops.get_bit( text[ 0 ], cfg.BLOCK_SIZE - i ) ^ bops.get_bit( text[ 1 ], cfg.BLOCK_SIZE - i )
			if result == 0:
				current_num += 1
		if abs( ( NTexts / 2 ) - current_num ) >= 50:
			analogs_with_num.append( [ current_num, analog ] )
				
	return analogs_with_num
예제 #3
0
파일: blocks.py 프로젝트: rockosov/crypto
	def make_permutation( self, bits, mode ):
		result = 0
		direct_size = len( self.direct )
		inverse_size = len( self.inverse )
		if mode == 1:
			# используем прямую перестановку
			for i in range( direct_size ):
				result = bits_ops.set_bit( result, ( direct_size - i - 1 ), bits_ops.get_bit( bits, ( inverse_size - self.direct[ i ] ) ) )
		elif mode == -1:
			# используем обратную перестановку	
			for i in range( len( self.inverse ) ):
				result = bits_ops.set_bit( result, ( inverse_size - i - 1 ), bits_ops.get_bit( bits, ( direct_size - self.inverse[ i ][ 0 ] ) ) )
		else:
			raise ValueError, "Invalid mode of permutation"
		return result
예제 #4
0
def get_analogs( with_alpha, with_beta, key_bits, stats ):
	left1 = list()
	left2 = list()
	right = list()
	with_alpha = with_alpha[ ::-1 ]
	with_beta = with_beta[ ::-1 ]
	key_bits = key_bits[ ::-1 ]
	out = list()
	for stat in stats:
		left1 = list()
		left2 = list()
		right = list()
		for i in range( len( with_alpha ) ):
			current_bit = bos.get_bit( stat[ 0 ], i )
			if current_bit == 1:
				left1.append( with_alpha[ i ] )
				right.append( key_bits[ i ] )
		for i in range( len( with_beta ) ):
			current_bit = bos.get_bit( stat[ 1 ], i )
			if current_bit == 1:
				left2.append( with_beta[ i ] )
		result = left1, left2, right, stat
		out.append( result )
	return out
예제 #5
0
def search_valid_bits( variants, rules ):
	result = list()
	first, second, third = variants
	flag = 0
	for i in first:
		for j in second:
			for k in third:
				probable = ( i[ 0 ] << 8 ) + ( j[ 0 ] << 4 ) + ( k[ 0 ] )
				for current_bits in rules:
					if len( current_bits ) > 1:
						res = bits_ops.get_bit( probable, conf.EXT_HALF_BLOCK_SIZE - current_bits[ 0 ] )
						for bit_num in current_bits:
							tmp = bits_ops.get_bit( probable, conf.EXT_HALF_BLOCK_SIZE - bit_num )
							if tmp != res:
								res = -1
						if res < 0:
							flag = 0
							break
					flag = 1
				if flag == 1:
					result.append( i )
					result.append( j )
					result.append( k )
	return result