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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?