duang8642 2016-09-28 23:31 采纳率: 0%
浏览 1252
已采纳

从Pem字符串Golang生成私钥

I am trying to generate private and public key objects in Golang from PEM strings heres my example

    r := strings.NewReader(privateKey)
    pemBytes, err := ioutil.ReadAll(r)
    if err != nil {
        log.Fatal(err)
    }

    block, _ := pem.Decode(pemBytes)
    if block == nil {
        log.Println(block)
    }

Heres my PEM string

 var privk = "-----BEGIN PRIVATE KEY-----"+
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDKlvdvjS7EEkYm"+
"RcMYBemMNvIbMRoJPBuwtRHph8pehi/BUXmTUsGUSDelFAl6tH2eMpHD6FpMJkiu"+
"PAm875yJYo9nxgnH8SNzS1FSR05qoFkJGSvkQwLd++V4Z17kqg9LyzQf/XGSiGGm"+
"VbvPS8rkJUdrFxgTjqhw8KIfNXGG4zLNcB6i0YgW2lZFgaFB16J3/O4KDm21bgdi"+
"qI6F5xKel/YSQl+AfF2NlFt5xI3wuZ4YhD7wrp3WWgiS/JIxu0ESDmJTYExdb3K0"+
"Dh6bmQl+wYJbkBpO2csNRDehtM2YEU83N8mmlSbIbSSinZYT3KwT9JQbd8QqJZCP"+
"/mOPFacrAgMBAAECggEAMqGWR3fWd0RF6ezHfGqF2vgke+1Cn4o5NWmbh2zbg9Iv"+
"fzYYl1w4axG9bnFaiSMwvefPjFG2t49d3MW+fUy5J5DNXFcfPKwkev0Y3uJZU8at"+
"WdvDn3Gr9sSsrfHPwoBKAFxRs6kIyGFzXjnRDVbY5zn15mrIJqMhr9BEBF6798TA"+
"qTG0FYTqkGK0D+FVfaWXvQ0u9Jw0KootS4kKHNwDmbZK2xYI2Ilt1ikeN9MMt8ZD"+
"tXV4shTnQaYPty5Atr9Dzh052FTnlwsclVo33XHF2N2dfe7TaJYTaf5uXuh7Vkj3"+
"99bKuvA8iLmEVe+i86L9K9LD5QEywO/sNcqQkNU8sQKBgQDkycMSv1dfSAgSZywU"+
"hcJqFWJ3TpAP5aoWqw8Svill3Qs6/2zatU4XC4tzRX9KW437M4ORNee/XrCi9z4L"+
"VOeOtR+gp/zn9DebBzaEdTfMlof+znPfx9fVIpBkpjezDgiFNkeeMnKTQrT/kULl"+
"Zso29pCfgO/57L9Vi3pjiqKt0wKBgQDir4EDVesZTGcBstSRLIGrUQFmvxICPaMm"+
"0PogvQhUv8UFMxx6nBl/ZB7AZWQH0TRlpVL7iqS2clO1dQOgrtgwgUV4m3Ml9Ivr"+
"vI2fgCWzFudPst3oSu9Udc9Pq995Pl2nnwV612p3P6p3wgCaFjbouyyJZxNmHQj/"+
"JPtv65nSSQKBgQDVcRnJorLLlHLbYF9yYfunZn3vWl7yRcvxy/KLBNewTZENoIAY"+
"Zm8M9ttJVjvTziheg4ep8EVdduSJlOnQPoysyXNROYeril5aBlepKYY+Gu2THV5j"+
"FpjYIZ/eFmf+Zwgx5xrXjq7vjZs4lnd3dvcOYec4t1yqqGE0WKR8uzjbuwKBgAYL"+
"0FEadY7TLtwovOqyWTMMkhD/f6d3pWZfpIxC/nnkM4kT9+p9R2DSds+C5MwglFkx"+
"s6jp5cLIAduRJ2udvj5s9EFnRAb7ItBC0zQx4s+ICNtjVe/gL8n86m6hkvBU7YKP"+
"B0JjhH9xv0Y6cnGprgU/GM0BZs8ObzL+9YXirtOhAoGACk1TGT6I5oMHLDB4yOOe"+
"a7dT+kAY6a2W2Sq2e0VN70EkVmXGC6ODpRzPcH7nojcMN5jk8QHHosWNK8DECwAj"+
"uCGYn8G/0yAlhkddzE1+y1f5nVm+GCQTrdMMuqOwJnosifdoNDbWg4oGiBRt1uwI"+
"aoxbWonlDAZaLC+8Bxe1Hss="+
"-----END PRIVATE KEY-----"

The pem.Decode function returns nil everytime what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongwu5318 2016-09-28 23:35
    关注

    There needs to be a newline after "-----BEGIN PRIVATE KEY-----" and before "-----END PRIVATE KEY-----"

    https://play.golang.org/p/T0jR2uzGp5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?