如何把OpenSSH公鑰轉換成OpenSSL格式

天府雲創發表於2017-08-02

首先看看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://stackoverflow.com/questions/1011572/convert-pem-key-to-ssh-rsa-format

相關文章