(self, data, crypt_type)
Crypt the data in blocks, running it through des_crypt()
Crypt the data in blocks, running it through des_crypt()
Crypt the data in blocks running it through des_crypt ()
def crypt(self, data, crypt_type):
        """Crypt the data in blocks, running it through des_crypt()"""

        # Error check the data
        if not data:
            return ''
        if len(data) % self.block_size != 0:
            if crypt_type == des.DECRYPT: # Decryption must work on 8 byte blocks
                raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n.")
            if not self.getPadding():
                raise ValueError("Invalid data length, data must be a multiple of " + str(self.block_size) + " bytes\n. Try setting the optional padding character")
            else:
                data += (self.block_size - (len(data) % self.block_size)) * self.getPadding()
            # print "Len of data: %f" % (len(data) / self.block_size)

        if self.getMode() == CBC:
            if self.getIV():
                iv = self.__String_to_BitList(self.getIV())
            else:
                raise ValueError("For CBC mode, you must supply the Initial Value (IV) for ciphering")

        # Split the data into blocks, crypting each one seperately
        i = 0
        dict = {}
        result = []
        #cached = 0
        #lines = 0
        while i < len(data):
            # Test code for caching encryption results
            #lines += 1
            #if dict.has_key(data[i:i+8]):
                #print "Cached result for: %s" % data[i:i+8]
            #    cached += 1
            #    result.append(dict[data[i:i+8]])
            #    i += 8
            #    continue

            block = self.__String_to_BitList(data[i:i+8])

            # Xor with IV if using CBC mode
            if self.getMode() == CBC:
                if crypt_type == des.ENCRYPT:
                    block = map(lambda x, y: x ^ y, block, iv)
                    #j = 0
                    #while j < len(block):
                    #    block[j] = block[j] ^ iv[j]
                    #    j += 1

                processed_block = self.__des_crypt(block, crypt_type)

                if crypt_type == des.DECRYPT:
                    processed_block = map(lambda x, y: x ^ y, processed_block, iv)
                    #j = 0
                    #while j < len(processed_block):
                    #    processed_block[j] = processed_block[j] ^ iv[j]
                    #    j += 1
                    iv = block
                else:
                    iv = processed_block
            else:
                processed_block = self.__des_crypt(block, crypt_type)


            # Add the resulting crypted block to our list
            #d = self.__BitList_to_String(processed_block)
            #result.append(d)
            result.append(self.__BitList_to_String(processed_block))
            #dict[data[i:i+8]] = d
            i += 8

        # print "Lines: %d, cached: %d" % (lines, cached)

        # Return the full crypted string
        if _pythonMajorVersion < 3:
            return ''.join(result)
        else:
            return bytes.fromhex('').join(result)
def crypt ( self , data , crypt_type ) : # Error check the data if not data : return '' if len ( data ) % self . block_size != 0 : if crypt_type == des . DECRYPT : # Decryption must work on 8 byte blocks raise ValueError ( "Invalid data length, data must be a multiple of " + str ( self . block_size ) + " bytes\n." ) if not self . getPadding ( ) : raise ValueError ( "Invalid data length, data must be a multiple of " + str ( self . block_size ) + " bytes\n. Try setting the optional padding character" ) else : data += ( self . block_size - ( len ( data ) % self . block_size ) ) * self . getPadding ( ) # print "Len of data: %f" % (len(data) / self.block_size) if self . getMode ( ) == CBC : if self . getIV ( ) : iv = self . __String_to_BitList ( self . getIV ( ) ) else : raise ValueError ( "For CBC mode, you must supply the Initial Value (IV) for ciphering" ) # Split the data into blocks, crypting each one seperately i = 0 dict = { } result = [ ] #cached = 0 #lines = 0 while i < len ( data ) : # Test code for caching encryption results #lines += 1 #if dict.has_key(data[i:i+8]): #print "Cached result for: %s" % data[i:i+8] #    cached += 1 #    result.append(dict[data[i:i+8]]) #    i += 8 #    continue block = self . __String_to_BitList ( data [ i : i + 8 ] ) # Xor with IV if using CBC mode if self . getMode ( ) == CBC : if crypt_type == des . ENCRYPT : block = map ( lambda x , y : x ^ y , block , iv ) #j = 0 #while j < len(block): #    block[j] = block[j] ^ iv[j] #    j += 1 processed_block = self . __des_crypt ( block , crypt_type ) if crypt_type == des . DECRYPT : processed_block = map ( lambda x , y : x ^ y , processed_block , iv ) #j = 0 #while j < len(processed_block): #    processed_block[j] = processed_block[j] ^ iv[j] #    j += 1 iv = block else : iv = processed_block else : processed_block = self . __des_crypt ( block , crypt_type ) # Add the resulting crypted block to our list #d = self.__BitList_to_String(processed_block) #result.append(d) result . append ( self . __BitList_to_String ( processed_block ) ) #dict[data[i:i+8]] = d i += 8 # print "Lines: %d, cached: %d" % (lines, cached) # Return the full crypted string if _pythonMajorVersion < 3 : return '' . join ( result ) else : return bytes . fromhex ( '' ) . join ( result )