英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论