2009-09-07 7 views

답변

5

ASCII 기갑 및 PEM은 매우 유사하다. BEGIN/END 마커를 변경하고 PGP 헤더와 체크섬을 제거하면됩니다. 전에 PHP에서이 작업을 수행했습니다. 방금 파이썬에 이식했습니다.

import re 
import StringIO 

def pgp_pubkey_to_pem(pgp_key): 
    # Normalise newlines 
    pgp_key = re.compile('(\n|\r\n|\r)').sub('\n', pgp_key) 

    # Extract block 
    buffer = StringIO.StringIO() 
    # Write PEM header 
    buffer.write('-----BEGIN RSA PUBLIC KEY-----\n') 

    in_block = 0 
    in_body = 0 
    for line in pgp_key.split('\n'): 
     if line.startswith('-----BEGIN PGP PUBLIC KEY BLOCK-----'): 
      in_block = 1 
     elif in_block and line.strip() == '': 
      in_body = 1 
     elif in_block and line.startswith('-----END PGP PUBLIC KEY BLOCK-----'): 
      # No checksum, ignored for now 
      break 
     elif in_body and line.startswith('='): 
      # Checksum, end of the body 
      break 
     elif in_body: 
      buffer.write(line+'\n') 

    # Write PEM footer 
    buffer.write('-----END RSA PUBLIC KEY-----\n') 

    return buffer.getvalue() 
관련 문제