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

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

How do I encrypt with an RSA private key read from a PEM file using the Go programming language?

问题

我如何在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()的等效函数。但是我没有看到如何创建PrivateKey对象,除非使用GenerateKey(),通过随机素数生成一个(可以通过查看源代码来确认)。

我需要弄清楚如何解码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?

答案1

得分: 2

我认为你可能正在寻找crypto/tls,而不是crypto/rsa。

我不完全确定你在这里尝试做什么,但是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.

答案2

得分: 1

等效的函数似乎是SignPKCS1v15。在crypto/x509包中的函数ParsePKCS1PrivateKey似乎是最接近您需要读取现有私钥的内容,但我不确定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.

huangapple
  • 本文由 发表于 2010年6月24日 11:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/3107143.html
匿名

发表评论

匿名网友

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

确定