Writing RSA PrivateKey PEM to file in golang

huangapple go评论152阅读模式
英文:

Writing RSA PrivateKey PEM to file in golang

问题

我正在使用spacemonkeygo的openssl库来生成RSA私钥。该库还提供了从PEM加载私钥的方法,但我无法弄清如何将私钥转换为加密的PEM块。有人有什么想法吗?

import "github.com/spacemonkeygo/openssl"

// 生成私钥
privateKey, _ := openssl.GenerateRSAKey(2048)

password := "加密密码"
// 从私钥创建加密的PEM块?
pem := ???

// 加载PEM文件
loadedPrivateKey, _ := openssl.LoadPrivateKeyFromPEMWidthPassword(pem, password)

privateKey == loadedPrivateKey

请帮我翻译以上内容。

英文:

I'm using spacemonkeygo's openssl library to generate an RSA PrivateKey - the library also offers ways to load a private key from a PEM, but I can't figure out how to turn the private key into an encrypted PEM block - anyone got any ideas?

import "github.com/spacemonkeygo/openssl"

// Generate a private key
privateKey, _ := openssl.GenerateRSAKey(2048)

password := "an encryption password"
// Create an encrypted PEM block from the private key?
pem := ???

// Load the PEM file
loadedPrivateKey, _ := openssl.LoadPrivateKeyFromPEMWidthPassword(pem, password)

privateKey == loadedPrivateKey

答案1

得分: 1

基本上,你可以使用x509.EncryptPEMBlock来实现这个功能。

如果你的密钥被-----BEGIN RSA PRIVATE KEY----------END RSA PRIVATE KEY-----包围的前缀和后缀所包含,你可能需要在加密之前将它们移除。

英文:

Basically you can achieve this with x509.EncryptPEMBlock.

If your key is surrounded by -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- post- and prefixes you might have to remove them before encryption.

huangapple
  • 本文由 发表于 2016年1月9日 20:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/34693477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定