def set_key(self, key): ''' Will set the crypting key for this object. Either 16 or 24 bytes long. ''' self.key_size = 24 # Use DES-EDE3 mode if len(key) != self.key_size: if len(key) == 16: # Use DES-EDE2 mode self.key_size = 16 else: raise ValueError('Key must be either 16 or 24 bytes long.') if self.get_mode() == CBC: if not self.get_iv(): # Use the first 8 bytes of the key self._iv = key[:self.block_size] if len(self.get_iv()) != self.block_size: raise ValueError('Invalid IV, must be 8 bytes in length') self.__key1 = Des1(key[:8], self._mode, self._iv, self._padding, self._padmode) self.__key2 = Des1(key[8:16], self._mode, self._iv, self._padding, self._padmode) if self.key_size == 16: self.__key3 = self.__key1 else: self.__key3 = Des1(key[16:], self._mode, self._iv, self._padding, self._padmode) DesBase.set_key(self, key) return
def set_key(self, key): ''' Will set the crypting key for this object. Must be 8 bytes. ''' DesBase.set_key(self, key) self.__create_sub_keys() return
def set_padding(self, pad): ''' set_padding() -> bytes of length 1. Padding character. ''' DesBase.set_padding(self, pad) for key in (self.__key1, self.__key2, self.__key3): key.set_padding(pad) return
def set_mode(self, mode): ''' Sets the type of crypting mode, pyDes.ECB or pyDes.CBC ''' DesBase.set_mode(self, mode) for key in (self.__key1, self.__key2, self.__key3): key.set_mode(mode) return
def set_iv(self, IV): ''' Will set the Initial Value, used in conjunction with CBC mode ''' DesBase.set_iv(self, IV) for key in (self.__key1, self.__key2, self.__key3): key.set_iv(IV) return
def set_pad_mode(self, mode): ''' Sets the type of padding mode, pyDes.PAD_NORMAL or pyDes.PAD_PKCS5 ''' DesBase.set_pad_mode(self, mode) for key in (self.__key1, self.__key2, self.__key3): key.set_pad_mode(mode) return
def __init__(self, key, mode=ECB, IV=None, pad=None, padmode=PAD_NORMAL): # Sanity checking of arguments. if(len(key) != 8): raise ValueError('Invalid DES key size.') DesBase.__init__(self, mode, IV, pad, padmode) self.key_size = 8 self.L = [] self.R = [] self.Kn = [[0] * 48] * 16 # 16 48-bit keys (K1 - K16) self.final = [] self.set_key(key)
def __init__(self, key, mode=ECB, IV=None, pad=None, padmode=PAD_NORMAL): DesBase.__init__(self, mode, IV, pad, padmode) self.set_key(key)