(self, block, crypt_type)
return self.final
Crypt the block of data through DES bit-manipulation
Crypt the block of data through DES bit-manipulation
Crypt the block of data through DES bit - manipulation
def __des_crypt(self, block, crypt_type):
        """Crypt the block of data through DES bit-manipulation"""
        block = self.__permutate(des.__ip, block)
        Bn = [0] * 32
        self.L = block[:32]
        self.R = block[32:]

        # Encryption starts from Kn[1] through to Kn[16]
        if crypt_type == des.ENCRYPT:
            iteration = 0
            iteration_adjustment = 1
        # Decryption starts from Kn[16] down to Kn[1]
        else:
            iteration = 15
            iteration_adjustment = -1

        i = 0
        while i < 16:
            # Make a copy of R[i-1], this will later become L[i]
            tempR = self.R[:]

            # Permutate R[i - 1] to start creating R[i]
            self.R = self.__permutate(des.__expansion_table, self.R)

            # Exclusive or R[i - 1] with K[i], create B[1] to B[8] whilst here
            self.R = map(lambda x, y: x ^ y, self.R, self.Kn[iteration])
            B = [self.R[:6], self.R[6:12], self.R[12:18], self.R[18:24], self.R[24:30], self.R[30:36], self.R[36:42], self.R[42:]]
            # Optimization: Replaced below commented code with above
            #j = 0
            #B = []
            #while j < len(self.R):
            #    self.R[j] = self.R[j] ^ self.Kn[iteration][j]
            #    j += 1
            #    if j % 6 == 0:
            #        B.append(self.R[j-6:j])

            # Permutate B[1] to B[8] using the S-Boxes
            j = 0
            pos = 0
            while j < 8:
                # Work out the offsets
                m = (B[j][0] << 1) + B[j][5]
                n = (B[j][1] << 3) + (B[j][2] << 2) + (B[j][3] << 1) + B[j][4]

                # Find the permutation value
                v = des.__sbox[j][(m << 4) + n]

                # Turn value into bits, add it to result: Bn
                Bn[pos] = (v & 8) >> 3
                Bn[pos + 1] = (v & 4) >> 2
                Bn[pos + 2] = (v & 2) >> 1
                Bn[pos + 3] = v & 1

                pos += 4
                j += 1

            # Permutate the concatination of B[1] to B[8] (Bn)
            self.R = self.__permutate(des.__p, Bn)

            # Xor with L[i - 1]
            self.R = map(lambda x, y: x ^ y, self.R, self.L)
            # Optimization: This now replaces the below commented code
            #j = 0
            #while j < len(self.R):
            #    self.R[j] = self.R[j] ^ self.L[j]
            #    j += 1

            # L[i] becomes R[i - 1]
            self.L = tempR

            i += 1
            iteration += iteration_adjustment

        # Final permutation of R[16]L[16]
        self.final = self.__permutate(des.__fp, self.R + self.L)
        return self.final
def __des_crypt ( self , block , crypt_type ) : block = self . __permutate ( des . __ip , block ) Bn = [ 0 ] * 32 self . L = block [ : 32 ] self . R = block [ 32 : ] # Encryption starts from Kn[1] through to Kn[16] if crypt_type == des . ENCRYPT : iteration = 0 iteration_adjustment = 1 # Decryption starts from Kn[16] down to Kn[1] else : iteration = 15 iteration_adjustment = - 1 i = 0 while i < 16 : # Make a copy of R[i-1], this will later become L[i] tempR = self . R [ : ] # Permutate R[i - 1] to start creating R[i] self . R = self . __permutate ( des . __expansion_table , self . R ) # Exclusive or R[i - 1] with K[i], create B[1] to B[8] whilst here self . R = map ( lambda x , y : x ^ y , self . R , self . Kn [ iteration ] ) B = [ self . R [ : 6 ] , self . R [ 6 : 12 ] , self . R [ 12 : 18 ] , self . R [ 18 : 24 ] , self . R [ 24 : 30 ] , self . R [ 30 : 36 ] , self . R [ 36 : 42 ] , self . R [ 42 : ] ] # Optimization: Replaced below commented code with above #j = 0 #B = [] #while j < len(self.R): #    self.R[j] = self.R[j] ^ self.Kn[iteration][j] #    j += 1 #    if j % 6 == 0: #        B.append(self.R[j-6:j]) # Permutate B[1] to B[8] using the S-Boxes j = 0 pos = 0 while j < 8 : # Work out the offsets m = ( B [ j ] [ 0 ] << 1 ) + B [ j ] [ 5 ] n = ( B [ j ] [ 1 ] << 3 ) + ( B [ j ] [ 2 ] << 2 ) + ( B [ j ] [ 3 ] << 1 ) + B [ j ] [ 4 ] # Find the permutation value v = des . __sbox [ j ] [ ( m << 4 ) + n ] # Turn value into bits, add it to result: Bn Bn [ pos ] = ( v & 8 ) >> 3 Bn [ pos + 1 ] = ( v & 4 ) >> 2 Bn [ pos + 2 ] = ( v & 2 ) >> 1 Bn [ pos + 3 ] = v & 1 pos += 4 j += 1 # Permutate the concatination of B[1] to B[8] (Bn) self . R = self . __permutate ( des . __p , Bn ) # Xor with L[i - 1] self . R = map ( lambda x , y : x ^ y , self . R , self . L ) # Optimization: This now replaces the below commented code #j = 0 #while j < len(self.R): #    self.R[j] = self.R[j] ^ self.L[j] #    j += 1 # L[i] becomes R[i - 1] self . L = tempR i += 1 iteration += iteration_adjustment # Final permutation of R[16]L[16] self . final = self . __permutate ( des . __fp , self . R + self . L ) return self . final