gpt4 book ai didi

ssh - 将公钥编码为 OpenSSH 格式以供显示

转载 作者:IT王子 更新时间:2023-10-29 01:03:25 30 4
gpt4 key购买 nike

我有一个 rsa.PublicKey 对象(从 rsa.PrivateKey 中检索)。我正在尝试将其导出为 OpenSSH 格式,以便在网页中显示它。

我注意到 go.crypto/ssh library ,似乎是这样做的。
还有 the discussion关于它的实现(这实际上正是我需要做的)

不幸的是,我有点卡住了,因为返回的字节数组采用未知编码,我不能只将它转换为字符串来显示它。

func PublicKey(rsaKey rsa.PublicKey) string {
key, _ := ssh.NewPublicKey(&rsaKey)
marshalled := ssh.MarshalPublicKey(key)
return string(marshalled)
}

这似乎有效,因为它在字符串的开头添加了 ssh-rsa。但是,大多数字符无法识别。

这是我为 lambda 公钥检索的字节数组:

[0 0 0 7 115 115 104 45 114 115 97 0 0 0 3 1 0 1 0 0 0 65 0 178 153 15 73 196 125 250 140 212 0 174 106 77 27 138 59 106 19 100 43 35 242 139 0 59 251 151 121 10 222 154 76 200 43 139 42 129 116 125 222 192 139 98 150 229 58 8 195 49 104 126 242 92 75 244 147 107 161 192 230 4 30 157 21]

关于将此字节数组正确显示为字符串的任何提示?

最佳答案

编码 key 用于有线格式。您只需要对字节进行 base64 编码:

base64.StdEncoding.EncodeToString(marshalled) + "\n"

关于ssh - 将公钥编码为 OpenSSH 格式以供显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22046776/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com