def adjust_key_parity(key_in):
 def parity_byte(key_byte):
  parity = 1
  for i in xrange(1, 8):
   parity ^= ((key_byte >> i) & 1)
  return ((key_byte & 254) | parity)
 if (len(key_in) not in key_size):
  raise ValueError('Not a valid TDES key')
 key_out = b('').join([bchr(parity_byte(bord(x))) for x in key_in])
 if ((key_out[:8] == key_out[8:16]) or (key_out[(-16):(-8)] == key_out[(-8):])):
  raise ValueError('Triple DES key degenerates to single DES')
 return key_out

def adjust_key_parity ( key_in ) : def parity_byte ( key_byte ) : parity = 1 for i in xrange ( 1 , 8 ) : parity ^= ( ( key_byte >> i ) & 1 ) return ( ( key_byte & 254 ) | parity ) if ( len ( key_in ) not in key_size ) : raise ValueError ( 'Not a valid TDES key' ) key_out = b ( '' ) . join ( [ bchr ( parity_byte ( bord ( x ) ) ) for x in key_in ] ) if ( ( key_out [ : 8 ] == key_out [ 8 : 16 ] ) or ( key_out [ ( - 16 ) : ( - 8 ) ] == key_out [ ( - 8 ) : ] ) ) : raise ValueError ( 'Triple DES key degenerates to single DES' ) return key_out
return the tdes key with parity bits correctly set .