def pem2der(pem_string):
 pem_string = pem_string.replace('\r', '')
 first_idx = (pem_string.find('-----\n') + 6)
 if (pem_string.find('-----BEGIN', first_idx) != (-1)):
  raise Exception('pem2der() expects only one PEM-encoded object')
 last_idx = pem_string.rfind('-----', 0, pem_string.rfind('-----'))
 base64_string = pem_string[first_idx:last_idx]
 base64_string.replace('\n', '')
 der_string = base64.b64decode(base64_string)
 return der_string

def pem2der ( pem_string ) : pem_string = pem_string . replace ( '\r' , '' ) first_idx = ( pem_string . find ( '-----\n' ) + 6 ) if ( pem_string . find ( '-----BEGIN' , first_idx ) != ( - 1 ) ) : raise Exception ( 'pem2der() expects only one PEM-encoded object' ) last_idx = pem_string . rfind ( '-----' , 0 , pem_string . rfind ( '-----' ) ) base64_string = pem_string [ first_idx : last_idx ] base64_string . replace ( '\n' , '' ) der_string = base64 . b64decode ( base64_string ) return der_string
encode every line between the first ----- and the 2nd-to-last ----- .