如何使用Go编程语言使用从PEM文件中读取的RSA私钥进行加密? [英] How do I encrypt with an RSA private key read from a PEM file using the Go programming language?

查看:1558
本文介绍了如何使用Go编程语言使用从PEM文件中读取的RSA私钥进行加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  RSA * key = PEM_read_RSAPrivateKey(f,NULL)

如何在go中执行以下C ++代码的等价操作? ,NULL,NULL);
std :: vector< CK_BYTE>出(128);
RSA_private_encrypt(in.size(),& in [0],& out [0],key,RSA_PKCS1_PADDING)

我查看了转到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(摘要,RSA.pkcs1_padding)

Go中是否有现有等价物?

解决方案

等价函数似乎是 SignPKCS1v15 ParsePKCS1PrivateKey 的功能 crypto / x509 软件包似乎与您需要阅读的内容最接近私钥,但我不确定PEM格式是否完全兼容,这一定是正确的。


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?

解决方案

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.

这篇关于如何使用Go编程语言使用从PEM文件中读取的RSA私钥进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆