duanraa1984 2014-02-26 16:03
浏览 248
已采纳

将公钥整理为OpenSSH格式以进行显示

I have an rsa.PublicKey object (retrieved from an rsa.PrivateKey). And I'm trying to export it into the OpenSSH format, to display it in a web page.

I've noticed the go.crypto/ssh library, which seems to be doing this.
And there's the discussion about it's implementation (it's actually exactly what I need to do)

Unfortunately, I'm getting a bit stuck, as the byte array returned is in an unknown encoding and I can't just transform it to a string to display it.

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

This seems to work as it adds the ssh-rsa at the beginning of the string. However, most characters aren't recognized.

Here's the bytes array I'm retrieving for a lambda public key:

[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]

Any hint on properly displaying this bytes array as a string?

  • 写回答

1条回答 默认 最新

  • donglei2022 2014-02-26 16:51
    关注

    Marshaling a key is for the wire format. You just need to base64 encode the bytes:

    base64.StdEncoding.EncodeToString(marshalled) + "
    "
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题