首先看看OpenSSL工具的简单使用方法,我们以rsa加密算法为例
生成一个私钥:
openssl genrsa -out private.key 1024
-out指定生成私钥文件名 1024是生成密钥的长度
利用私钥生成对应的公钥:
openssl rsa -in private.key -pubout -out public.key
-in 指定的私钥,-out 指定公钥文件名
加密文件:
openssl rsautl -encrypt -in test -inkey public.key -pubin -out test_encrypt
-in 指定加密的文件,-inkey 指定公钥,-pubin表明是用纯公钥文件加密,-out为加密后的密文文件
解密文件:
openssl rsautl -decrypt -in test_encrypt -inkey private.key -out test_decrypt
-in指定加密后的密文文件,-inkey指定公钥对应的私钥,-out为解密后的文件。
然后你就可以直接diff一把原文件和test_decrypt,看看是不是一样了
不过玩过OpenSSL的兄弟一定知道,OpenSSL生成的公钥跟OpenSSH的公钥虽然来自同一个私钥,不过格式却完全不一样
这就是很多兄弟遭遇悲剧的所在,因为OpenSSH的公钥借助ssh的互信可以在authorized_key中非常容易就得到
不过这个openssh的公钥格式不一样,没法用openssl加密
openssl的公钥例子:
—–BEGIN PUBLIC KEY—–
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7vbqajDw4o6gJy8UtmIbkcpnk
O3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2
eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1
QWPdspTBKcxeFbccDwIDAQAB
—–END PUBLIC KEY—–
openssh的公钥例子:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC7vbqajDw4o6gJy8UtmIbkcpnkO3Kwc4qsEnSZp/TR+fQi62F79RHWmwKOtFmwteURgLbj7D/WGuNLGOfa/2vse3G2eHnHl5CB8ruRX9fBl/KgwCVr2JaEuUm66bBQeP5XeBotdR4cvX38uPYivCDdPjJ1QWPdspTBKcxeFbccDw==
不过还好这个早就不是什么新问题了,如果是OpenSSH v.5.6或者以上版本,可以用下面的命令生成(这个没验证过,确实找不到这么高版本的环境)
ssh-keygen -f key.pub -e -m pem
不过你的OpenSSH还没这么高版本,也不想费劲去升级依赖的一堆库文件,也有用代码实现的方式
附上python实现代码:
#!/usr/bin/env python
# with help and inspiration from
# * ASN1_generate_nconf(3) (specifically the SubjectPublicKeyInfo structure)
# * http://www.sysmic.org/dotclear/index.php?post/2010/03/24/Convert-keys-betweens-GnuPG%2C-OpenSsh-and-OpenSSL
# * http://blog.oddbit.com/2011/05/converting-openssh-public-keys.html
import sys
import base64
import struct
from pyasn1.type import univ
from pyasn1.codec.der import encoder as der_encoder, decoder as der_decoder
if len(sys.argv) != 2:
sys.stderr.write(“Usage: %s \n” % sys.argv[0])
sys.exit(1)
keyfields = open(sys.argv[1]).read().split(None)
if len(keyfields) I’, keydata[:4])[0]
# read in bytes
data, keydata = keydata[4:dlen+4], keydata[4+dlen:]
parts.append(data)
e_val = eval(‘0x’ + ”.join([‘%02X’ % struct.unpack(‘B’, x)[0] for x in parts[1]]))
n_val = eval(‘0x’ + ”.join([‘%02X’ % struct.unpack(‘B’, x)[0] for x in parts[2]]))
bitstring = univ.Sequence()
bitstring.setComponentByPosition(0, univ.Integer(n_val))
bitstring.setComponentByPosition(1, univ.Integer(e_val))
bitstring = der_encoder.encode(bitstring)
bitstring = ”.join([(‘00000000’+bin(ord(x))[2:])[-8:] for x in list(bitstring)])
bitstring = univ.BitString(“‘%s’B” % bitstring)
pubkeyid = univ.Sequence()
pubkeyid.setComponentByPosition(0, univ.ObjectIdentifier(‘1.2.840.113549.1.1.1’)) # == OID for rsaEncryption
pubkeyid.setComponentByPosition(1, univ.Null(”))
pubkey_seq = univ.Sequence()
pubkey_seq.setComponentByPosition(0, pubkeyid)
pubkey_seq.setComponentByPosition(1, bitstring)
print “—–BEGIN PUBLIC KEY—–”
if keycomment:
print “X-Comment: ” + keycomment
print
base64.MAXBINSIZE = (64//4)*3 # this actually doesn’t matter, but it helped with comparing to openssl’s output
print base64.encodestring(der_encoder.encode(pubkey_seq)),
print ‘—–END PUBLIC KEY—–‘
参考资料:http://*.com/questions/1011572/convert-pem-key-to-ssh-rsa-format