如何使用 Go 编程语言从 PEM 文件读取的 RSA 私钥进行加密?
如何在 go 中执行与以下 C++ 代码等效的操作?
RSA *key = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
std::vector<CK_BYTE> out(128);
RSA_private_encrypt(in.size(), &in[0], &out[0], key, RSA_PKCS1_PADDING)
我查看了 Go rsa 包。看起来 EncryptPKCS1v15() 可能相当于 RSA_private_encrypt()。但除了使用GenerateKey()之外,我不知道如何创建PrivateKey对象,这(可以通过查看来源)使用随机素数生成一个。
我是否需要弄清楚如何解码 PEM 文件以便提取 PrivateKey 字段的值?
更新: Python 中与上述 C++ 代码等效的是:
from M2Crypto import RSA
rsa_private_key = RSA.load_key('privkey.pem')
encrypted = rsa_private_key.private_encrypt(digest, RSA.pkcs1_padding)
Go 中是否存在等效的现有代码?
How do I do the equivalent of the following C++ code in go?
RSA *key = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
std::vector<CK_BYTE> out(128);
RSA_private_encrypt(in.size(), &in[0], &out[0], key, RSA_PKCS1_PADDING)
I've looked at the Go rsa package. It looks like EncryptPKCS1v15() may be the equivalent of RSA_private_encrypt(). But I don't see how to create a PrivateKey object other than with GenerateKey(), which (one can confirm by looking at the source) generates one using random prime numbers.
Do I need to figure out how to decode a PEM file so pull out the PrivateKey fields' values?
Update: The equivalent to the above C++ code in Python is:
from M2Crypto import RSA
rsa_private_key = RSA.load_key('privkey.pem')
encrypted = rsa_private_key.private_encrypt(digest, RSA.pkcs1_padding)
Is there an existing equivalent in Go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
等效函数似乎是
SignPKCS1v15
。ParsePKCS1PrivateKey
中的函数http://golang.org/pkg/crypto/x509/" rel="nofollow noreferrer">crypto/x509 包似乎最接近您需要在现有私钥中读取的内容,但我'我不确定 PEM 格式是否完全兼容,但必须兼容才能正常工作。The equivalent function appears to be
SignPKCS1v15
. The functionParsePKCS1PrivateKey
in the crypto/x509 package appears to be the closest to what you need to read in your existing private key, but I'm not sure the PEM format is exactly compatible, which it must be for this to work.我认为您可能正在寻找 crypto/tls,而不是 crypto/rsa。
我不是 100% 确定您在这里要做什么,但 tls 包确实具有一些读取 PEM 文件的功能。
I think you may be looking for crypto/tls, not crypto/rsa.
I'm not 100% sure what you're trying to do here, but the tls package does have some functionality for reading PEM files.