コード例 #1
0
ファイル: spncipher.py プロジェクト: kevinuvic/DiffCryptoSPN
    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
コード例 #2
0
ファイル: spncipher.py プロジェクト: kevinuvic/DiffCryptoSPN
    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
コード例 #3
0
ファイル: spncipher.py プロジェクト: kevinuvic/DiffCryptoSPN
    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
コード例 #4
0
ファイル: spncipher.py プロジェクト: kevinuvic/DiffCryptoSPN
 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')
コード例 #5
0
ファイル: util.py プロジェクト: kevinuvic/DiffCryptoSPN
 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
コード例 #6
0
    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')
コード例 #7
0
    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
コード例 #8
0
ファイル: util.py プロジェクト: kevinuvic/DiffCryptoSPN
 def getCopy(bset, totalBits):
     newset = bst.Bitset(0, totalBits)
     for i in range(totalBits):
         newset[i] = bset[i]
     return newset
コード例 #9
0
ファイル: spncipher.py プロジェクト: kevinuvic/DiffCryptoSPN
    def permuteBits(self, bits16):
        bSetPerm = bst.Bitset(0, 16)
        for i in range(16):
            bSetPerm[self.PermuteTbl[i]] = bits16[i]

        return bSetPerm