如何使用 Go 编程语言从 PEM 文件读取的 RSA 私钥进行加密?

发布于 2024-09-07 04:22:42 字数 830 浏览 16 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-09-14 04:22:42

等效函数似乎是 SignPKCS1v15ParsePKCS1PrivateKey 中的函数http://golang.org/pkg/crypto/x509/" rel="nofollow noreferrer">crypto/x509 包似乎最接近您需要在现有私钥中读取的内容,但我'我不确定 PEM 格式是否完全兼容,但必须兼容才能正常工作。

The equivalent function appears to be SignPKCS1v15. The function ParsePKCS1PrivateKey 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.

莫言歌 2024-09-14 04:22:42

我认为您可能正在寻找 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文