def EP_r(rk_r): WK_r = [] EP = bm.gen(32, 64) rk_r = tr.BinList2BinArray(tr.HexStr2BinList(tr.Int2HexStr(rk_r))) WK = tr.BinList2HexStr(tr.BinArray2BinList(bm.mlti(rk_r, EP))) for i in xrange(0, len(WK), 2): WK_r.append(WK[i] + WK[i + 1]) return WK_r
def E_r(x): s = '' for i in xrange(len(x)): A = bm.gen(4, 8) b = bm.gen(1, 8) x_i = tr.BinList2BinArray(tr.HexStr2BinList(x[i], 4)) y_i = xor(tr.BinList2HexStr(tr.BinArray2BinList(bm.mlti(x_i, A))), tr.BinList2HexStr(tr.BinArray2BinList(b))) s += y_i return s
def R(b): R = bm.gen(32, 32) return tr.BinList2HexStr( tr.BinArray2BinList( bm.mlti(tr.BinList2BinArray(tr.HexStr2BinList(b)), R)))
def wb_crypt(pt): text = split_pt(pt, BLOCK_SIZE) cipher_text = 'ct: ' plain_text = 'pt: ' for each in text: # each: 128 bit x = [trans(each[i:i + BLOCK_SIZE / 4]) for i in xrange(0, BLOCK_SIZE, BLOCK_SIZE / 4)] # x[i]: '61626364' -> 'abcd' print 'Plain text block: ' + ''.join(x) print 'Encryption start.' for r in xrange(32): X_r = xor(xor(x[1], x[2]), x[3]) for i in xrange(8): X_r = X_r[0:i] + tr.Int2HexStr(or_table[r][i][tr.HexStr2Int(X_r[i])], 1) + X_r[i + 1:] # X_r_0 = x[0] X_r_0 = xor(tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(pr_table[r][0], tr.BinList2BinArray(tr.HexStr2BinList(x[0]))))), tr.BinList2HexStr(tr.BinArray2BinList(pr_table[r][1]))) y = '00000000' for i in xrange(8): y = xor(tr.BinList2HexStr(tr.BinArray2BinList(LUTable[r][i][tr.HexStr2Int(X_r[i])])), y) new_x = xor(X_r_0, y) new_x = xor( tr.BinList2HexStr(tr.BinArray2BinList(bm.mlti(bm.inv(pr_table[r][0]), pr_table[r][1]))), tr.BinList2HexStr(tr.BinArray2BinList( bm.mlti(bm.inv(pr_table[r][0]), tr.BinList2BinArray(tr.HexStr2BinList(new_x)))))) x = x[1:] + [new_x] print 'Round ' + str(r) + ': ' + ''.join(x) cipher_text += reduce(lambda a, b: a + b, map(inverse, x[-1::-1])) x = [x[3], x[2], x[1], x[0]] print 'Cipher text block: ' + ''.join(x) print 'Decryption start.' for r in xrange(32): X_r = xor(xor(x[1], x[2]), x[3]) for i in xrange(8): X_r = X_r[0:i] + tr.Int2HexStr(or_table[31 - r][i][tr.HexStr2Int(X_r[i])], 1) + X_r[i + 1:] # X_r_0 = x[0] X_r_0 = xor(tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(pr_table[31 - r][0], tr.BinList2BinArray(tr.HexStr2BinList(x[0]))))), tr.BinList2HexStr(tr.BinArray2BinList(pr_table[31 - r][1]))) y = '00000000' for i in xrange(8): y = xor(tr.BinList2HexStr(tr.BinArray2BinList(LUTable[31 - r][i][tr.HexStr2Int(X_r[i])])), y) new_x = xor(X_r_0, y) new_x = xor( tr.BinList2HexStr(tr.BinArray2BinList(bm.mlti(bm.inv(pr_table[31 - r][0]), pr_table[31 - r][1]))), tr.BinList2HexStr(tr.BinArray2BinList( bm.mlti(bm.inv(pr_table[31 - r][0]), tr.BinList2BinArray(tr.HexStr2BinList(new_x)))))) x = x[1:] + [new_x] print 'Round ' + str(r) + ': ' + ''.join(x) print '----------------------------------------------------------------------' plain_text += reduce(lambda a, b: a + b, map(inverse, x[-1::-1])) print plain_text print cipher_text
def A_4(x): global r x = tr.BinList2BinArray(tr.HexStr2BinList(x)) return xor(tr.BinList2HexStr(tr.BinArray2BinList(bm.mlti(x, A[r][3]))), tr.BinList2HexStr(tr.BinArray2BinList(b[r][3])))
def wb_crypt(plain_text): # main function, take string as input and return encrypted text, global r text = split_pt(plain_text) cipher_text = '' for each in text: x = [trans(each[i:i + line / 4]) for i in xrange(0, line, line / 4)] print 'Plain text block: ' + ''.join(x) print 'Encryption start: ' print 'Pre-procession start: ' x[0] = xor( tr.BinList2HexStr( tr.BinArray2BinList( bm.mlti(tr.BinList2BinArray(tr.HexStr2BinList(x[0])), bm.inv(A[0][2])))), tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(b[0][2], bm.inv(A[0][2]))))) x[1] = xor( tr.BinList2HexStr( tr.BinArray2BinList( bm.mlti(tr.BinList2BinArray(tr.HexStr2BinList(x[1])), bm.inv(A[0][0])))), tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(b[0][0], bm.inv(A[0][0]))))) x[2] = xor( tr.BinList2HexStr( tr.BinArray2BinList( bm.mlti(tr.BinList2BinArray(tr.HexStr2BinList(x[2])), bm.inv(A[0][0])))), tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(b[0][0], bm.inv(A[0][0]))))) x[3] = xor( tr.BinList2HexStr( tr.BinArray2BinList( bm.mlti(tr.BinList2BinArray(tr.HexStr2BinList(x[3])), bm.inv(A[0][0])))), tr.BinList2HexStr( tr.BinArray2BinList(bm.mlti(b[0][0], bm.inv(A[0][0]))))) print 'Pre-processed text block: ' + ''.join(x) for i in xrange(32): # X^{r} = O_r (x_{r,1} ^ x_{r,2} ^ x_{r,3}) X_r = func_A(func_A(xor(xor(x[1], x[2]), x[3]), -1), 4) # print 'X_r: ' + X_r # X_{r,0} = P_r (x_{r,0}) X_r_0 = func_A(func_A(x[0], -3), 4) # print 'X_r_0: ' + X_r_0 # get y_{r,1}... y_{r,8} y = list(get_y(X_r)) # print 'y: ' + str(y) new_x = reduce(xor, y, X_r_0) x = x[1:] + [new_x] print 'Round ' + str(i) + ': ' + ''.join(x) r = r + 1 # Accumulate (x_{31,3}, x_{31,2}, x_{31,1}, x_{31,0}) cipher_text += reduce(lambda a, b: a + b, map(inverse, x[-1::-1])) r = 0 print '----------------------------------------------------------------------' print 'Cipher text: ' print cipher_text