def generateKey(self, key64): self.key = [] if len(key64) != 8: raise ('初始密钥长度错误。') bits = byte2bit(key64) if self.checkKey(bits) == False: return False CD = self.doPC_1(key64) CD = b2i(CD) C, D = CD >> 28, CD & 0xFFFFFFF for i in range(16): C = aCycleLeftMove(C, 28, self.SHIFT[i]) D = aCycleLeftMove(D, 28, self.SHIFT[i]) self.key.append(self.doPC_2(i2b((C << 28) | D, 56 // 8)))
def doS(self, byte6): ret = [] bits = byte2bit(byte6) ssum = 0 for i in range(0, 48, 6): if i % 12 == 0: ret.append(ssum) ssum = 0 cur = bit2int([ bits[i], bits[i + 5], bits[i + 1], bits[i + 2], bits[i + 3], bits[i + 4] ]) ssum <<= 4 ssum |= self.S[i // 6][cur] # print(self.S[i//6][cur],ssum) ret.append(ssum) # print(ret) return bytes(ret[1:])
def doP(self, byte4): bits = byte2bit(byte4) ret = [bits[self.P[i] - 1] for i in range(32)] return bit2byte(ret)
def doE(self, byte4): bits = byte2bit(byte4) ret = [bits[self.E[i] - 1] for i in range(48)] return bit2byte(ret)
def doPC_2(self, byte6): bits = byte2bit(byte6) ret = [bits[self.PC_2[i] - 1] for i in range(48)] return bit2byte(ret)
def doPC_1(self, byte8): bits = byte2bit(byte8) ret = [bits[self.PC_1[i] - 1] for i in range(56)] return bit2byte(ret)
def doIP_1(self, byte8): bits = byte2bit(byte8) ret = [bits[self.IP_1[i] - 1] for i in range(64)] return bit2byte(ret)