有没有任何python方法可以将base64编码的密钥转换为pem格式。
如何将ASCII装甲PGP公钥转换为MIME编码格式。
谢谢
发布于 2009-09-07 15:33:57
ASCII-装甲和PEM非常相似。你只需要改变开始/结束标记,去掉PGP头和校验和即可。我以前在PHP中做过这样的事情。我刚帮你把它移植到了Python,
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()https://stackoverflow.com/questions/1387867
复制相似问题