def get_round_keys(): """Get 64-bit key for each round.""" main_key = get_key_from_json() key_perm = [ 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 ] key_compression = [ 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 ] main_key = helper.permute(main_key, key_perm) # get 56-bit key left_key = main_key[:28] right_key = main_key[28:] round_keys = [] for i in range(16): shift_amount = 2 if i < 2 or i == 8 or i == 15: shift_amount = 1 left_key = helper.circular_left_shift(left_key, shift_amount) right_key = helper.circular_left_shift(right_key, shift_amount) full_key = left_key + right_key round_keys.append(helper.permute(full_key, key_compression)) # get 48-bit subkey return round_keys[::-1]
def straight_perm(message): perm = [ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 ] message = helper.permute(message, perm) return message
def testv1(): # This is testing code. Currently it only tests 2 methods, but ideally, there should be a test # for each method. Many IDEs provide a test framework that makes this a lot easier, # but you can always create your own n = 3 p = UF1problem([],n) # ------ TESTING getActions() -------------------- answer = permute( [ i for i in range(1,n+1) ] ) input = [ [], [[1,2,3]]] for i in input: a = p.getActions(i) msg = 'getActions('+str(i)+') ='+str(a)+' Expected '+str(answer) if not a == answer: msg = '**** FAIL **** : '+msg else: msg = ' pass : '******'applyActions('+str(i[0])+','+str(i[1])+') = '+str(a)+' Expected '+str(i[2]) if not a == i[2]: msg = '**** FAIL **** : '+msg else: msg = ' pass : '+msg print(msg)
def expand_to_48_bits(message): perm = [ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 ] message = helper.permute(message, perm) return message
def perform_initial_perm(message): perm = [ 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 ] message = helper.permute(message, perm) return message
def __init__(self, initial, size, goal=None): # These values are specific to the problem you are solving (i.e. sudoku, nQueens, ...) self.size = size self.initial = initial self.goal = goal # For this example, an action is the assignment of an entire row # thus, the set of legal actions for all states is all permutations of viable numbers # Example: [[1,2,3],[1,3,2],[2,1,3],...] self.actions = permute( [ i for i in range(1,size+1) ] )
import helper as hp def isdivis(l): for i,x in zip(range(1,8),div): k = int(''.join(map(str,l[i:i+3]))) if k%x != 0: return False return True r = [] l = list(range(10)) div = [2,3,5,7,11,13,17] p = hp.permute(l) for x in p: if isdivis(list(str(x))): print x r.append(x)