def test3(self): spnKey = [ bst.Bitset(0x1234, 16), bst.Bitset(0x5678, 16), bst.Bitset(0x9ABC, 16), bst.Bitset(0xDEF0, 16), bst.Bitset(0x1357, 16) ] # process of encrypting #deltaX = utl.toBitSet(0x0b00, 16) deltaX = utl.toBitSet(0x1234, 16) for i in range(4): deltaY = self.partialEncrypt(deltaX, spnKey[i], i) print('y', i + 1, '=', hex(int(deltaY))) deltaX = deltaY deltaY ^= spnKey[4] print('y5 = ', hex(int(deltaY))) # process of encrypting deltaY ^= spnKey[4] print('x4 = ', hex(int(deltaY))) for i in range(3, -1, -1): deltaX = self.partialDecrypt(deltaY, spnKey[i], i) print('x', i, '=', hex(int(deltaX))) deltaY = deltaX
def getSBoxInvGroup(self, bits16): result = bst.Bitset(0, 16) for i in range(4): toSBox = bst.Bitset(0, 4) for j in range(4): toSBox[j] = bits16[i * 4 + j] fromSBox = self.getSBoxInv(toSBox) for j in range(4): result[i * 4 + j] = fromSBox[j] return result
def getSBoxGroup(self, bits16): result = bst.Bitset(0, 16) for i in range(4): toSBox = bst.Bitset(0, 4) for j in range(4): # toSBox.set(j, bSet.get(i * 4 + j)) toSBox[j] = bits16[i * 4 + j] fromSBox = self.getSBox(toSBox) for j in range(4): result[i * 4 + j] = fromSBox[j] return result
def test2(self): spnKey = [ bst.Bitset(0x1234, 16), bst.Bitset(0x5678, 16), bst.Bitset(0x9ABC, 16), bst.Bitset(0xDEF0, 16), bst.Bitset(0x1357, 16) ] pt = 0x1234 ctenc = self.Encrypt(utl.toBitSet(pt, 16), spnKey) #ctenc = utl.toBitSet(0x52C0, 16) ptdec = self.Decrypt(ctenc, spnKey) print('Step1: Encrypt(0x1234) = ', hex(int(ctenc)), '\nStep2: Decrypt(', hex(int(ctenc)), ') = ', hex(int(ptdec)), '\nStep3: The original plaintext = ', hex(pt), ', it equals to the result of Step2')
def toBitSet(value, totalBits): idx = totalBits - 1 result = bst.Bitset(0, totalBits) while value > 0: #result.set(idx, value % 2 == 1) result[idx] = (value % 2 == 1) value //= 2 idx -= 1 return result
def __init__(self, pcPair=5000, keys=[0x1234, 0x5678, 0x9ABC, 0xDEF0, 0xABCD]): self.spnKey = [] for i in range(5): self.spnKey.append(bst.Bitset(keys[i], 16)) key5 = self.spnKey print('\n\n5 keys = ', hex(int(key5[0])), hex(int(key5[1])), hex(int(key5[2])), hex(int(key5[3])), hex(int(key5[4]))) self.noPair = pcPair print('Plain/Cipher text pair = ', pcPair, '\n')
def ComputeDiffCharacteristic(self, seed=0x0b00): SBox = spn.SpnCipher() rst = bst.Bitset(seed, 16) print('S1.DC.X = ', hex(int(rst))) rst = SBox.getSBoxGroup(rst) print('S1.DC.Y = ', hex(int(rst))) rst = SBox.permuteBits(rst) print('S2.DC.X = ', hex(int(rst))) rst = SBox.getSBoxGroup(rst) print('S2.DC.Y = ', hex(int(rst))) rst = SBox.permuteBits(rst) rst = SBox.permuteBits(rst) print('S3.DC.X = ', hex(int(rst))) rst = SBox.getSBoxGroup(rst) print('S3.DC.Y = ', hex(int(rst))) rst = SBox.permuteBits(rst) print('S4.DC.X = ', hex(int(rst))) return
def getCopy(bset, totalBits): newset = bst.Bitset(0, totalBits) for i in range(totalBits): newset[i] = bset[i] return newset
def permuteBits(self, bits16): bSetPerm = bst.Bitset(0, 16) for i in range(16): bSetPerm[self.PermuteTbl[i]] = bits16[i] return bSetPerm