Example #1
0
 def test_bitlist_to_string(self):
     string = 'foo'
     bitlist = [
         0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1,
         1, 1
     ]
     self.assertEqual(utils_misc.string_to_bitlist(string), bitlist)
Example #2
0
 def create_Kn(self):
     """
     Create the 16 subkeys,from K[0] to K[15], from the given key
     """
     key = self.get_sub_list(self.PC1, utils_misc.string_to_bitlist(self.getKey()))
     self.L = key[:28]
     self.R = key[28:]
     for i in range(16):
         # Perform circular left shifts
         for j in range(self.left_rotations[i]):
             self.L.append(self.L[0])
             del self.L[0]
             self.R.append(self.R[0])
             del self.R[0]
         # Create one of the 16 subkeys through pc2 permutation
         self.Kn[i] = self.get_sub_list(self.PC2, self.L + self.R)
Example #3
0
 def create_Kn(self):
     """
     Create the 16 subkeys,from K[0] to K[15], from the given key
     """
     key = self.get_sub_list(
         self.PC1, utils_misc.string_to_bitlist(self.getKey()))
     self.L = key[:28]
     self.R = key[28:]
     for i in range(16):
         # Perform circular left shifts
         for j in range(self.left_rotations[i]):
             self.L.append(self.L[0])
             del self.L[0]
             self.R.append(self.R[0])
             del self.R[0]
         # Create one of the 16 subkeys through pc2 permutation
         self.Kn[i] = self.get_sub_list(self.PC2, self.L + self.R)
Example #4
0
    def crypt(self, data, crypt_type=0):
        """
        Crypt the data in blocks, running it through des_crypt()

        @param data: Data to be encrypted/decrypted.
        @param crypt_type: crypt type. 0 means encrypt, and 1 means decrypt.
        """

        # Split the data into list, crypting each one seperately
        i = 0
        result = []
        while i < len(data):
            # Test code for caching encryption results
            block = utils_misc.string_to_bitlist(data[i : i + 8])
            pro_block = self.des_crypt(block, crypt_type)

            # Add the resulting block to our list
            result.append(utils_misc.bitlist_to_string(pro_block))
            i += 8

        # Return the full encrypted/decrypted string
        return "".join(result)
Example #5
0
    def crypt(self, data, crypt_type=0):
        """
        Crypt the data in blocks, running it through des_crypt()

        :param data: Data to be encrypted/decrypted.
        :param crypt_type: crypt type. 0 means encrypt, and 1 means decrypt.
        """

        # Split the data into list, crypting each one separately
        i = 0
        result = []
        while i < len(data):
            # Test code for caching encryption results
            block = utils_misc.string_to_bitlist(data[i:i + 8])
            pro_block = self.des_crypt(block, crypt_type)

            # Add the resulting block to our list
            result.append(utils_misc.bitlist_to_string(pro_block))
            i += 8

        # Return the full encrypted/decrypted string
        return ''.join(result)
Example #6
0
 def test_bitlist_to_string(self):
     string = 'foo'
     bitlist = [0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
                1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1]
     self.assertEqual(utils_misc.string_to_bitlist(string), bitlist)