1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| import itertools from pyDes import *
key = [ '111000001011111001100110000100000011001011010101', '111100001011011001110110111110000010000010010101', '111001001101011001110110001000110110001010001111', '111001101101001101110110001101100011000110000011', '101011101101001101110011101001100000000101100111', '101011110101001101111011010001101010101111000010', '101011110101001111011001011101001000010101011001', '000111110101101111011001010010111001010001001010', '001111110100100111011001010010001001011111101010', '000111110110100110011101000111001101110000101001', '000111110010110110011101010010100101110001110000', '010111110010110010101101100010011110100100111000', '110110111010110010101100101000010101111000010000', '110110001010111010101110110110010000001000110110', '111100001011111000101110100101010100101010001100', '111100001011111010100110000100010010111010000100' ] __pc2 = [ 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 ] C1D1 = ['*'] * 56 for i in range(0, len(key[0])): C1D1[__pc2[i]] = key[0][i] print "".join(C1D1)[:28], "".join(C1D1)[28:]
C0 = '000000001*11111111*111*10*00' D0 = '0000111*11*1001*0000100001*0' __pc1 = [56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 ] C0D0 = C0 + D0 res = ['*'] * 64 deskey = "" for i in range(0, len(__pc1)): res[__pc1[i]] = C0D0[i] for i in res: deskey += i print deskey
def zuoyiwei(str, num): my = str[num:len(str)] my = my + str[0:num] return my
def key_change_1(str): key1_list = [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] res = "" for i in key1_list: res += str[i - 1] return res
def key_change_2(str): key2_list = [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] res = "" for i in key2_list: res += str[i - 1] return res
def key_gen(str): key_list = [] key_change_res = key_change_1(str) key_c = key_change_res[0:28] key_d = key_change_res[28:] for i in range(1, 17): if (i == 1) or (i == 2) or (i == 9) or (i == 16): key_c = zuoyiwei(key_c, 1) key_d = zuoyiwei(key_d, 1) else: key_c = zuoyiwei(key_c, 2) key_d = zuoyiwei(key_d, 2) key_yiwei = key_c + key_d key_res = key_change_2(key_yiwei) key_list.append(key_res) return key_list
asdkey = "11001101100111111001011010101100100110010011101001100101" qwekey = "11001101100110111001111010111100101110010111101011100101"
print key_gen(deskey) unknow = ['111000001011111001100110000100000011001011010101', '1j1n000010d101100m110110111k100000100000be0a0101', '11100100jm010d10011101100010001ae11000101000111b', '1d1001101101001m01110nj0001101b000k10e0110000011', '10j01110d10100110111001m10k0e1100000000b01100a11', '1010111m0101001101n110d1010001101e101011a10000k0', 'n0d0111101010011m10j10010b110k00100001010101100a', '000m1j11010110n111011001e10010a1100101000k00b010', '00n111110100m00j1d0110010k00100e10010b11111010a0', '000111110dm010011001110j0001ba00110111000010k001', '0001111j00101m0d100n1101010010100b0k11000a110e00', '0m011n1100101100j010110d10001001a1101001e0k11000', '110j10ndm010110010101100b01000010101ka100001000e', '1101n00010101110d01011m01101a0e1000000b0001k0110', '11md0000101111100010nj10100b010k0a00101e10001100', '11110000101mdj10n010011000010e010010a1k010000b00']
def check(s): look = 1 count = 0 for y in xrange(16): if s in unknow[y]: count += 1 try: assert key[y][unknow[y].index(s)] == "1" except: look = 0 break if count == 0: print x + ": Unknow" elif look == 1: print x + ": 1" else: print x + ": 0"
for x in "abcdefghijklmnop": check(x)
key = "110011c110011f111001g110101h110010i110010l111010o110010p"
for x in itertools.product("01", repeat=8): my_key = "" count = 0 my_key2 = "" for y in xrange(56): if key[y] in ["1", "0"]: my_key += key[y] else: my_key += x[count] count += 1 for y in xrange(8): my_key2 += chr(int(my_key[y * 7: y * 7 + 7], 2)) data = "\x23" * 8 k = des(my_key2) d = k.encrypt(data) z = k.decrypt(d) print z print "Over!"
|