English 中文(简体)
How do I encrypt with an RSA private key read from a PEM file using the Go programming language?
原标题:
  • 时间:2010-06-24 03:55:59
  •  标签:
  • rsa
  • go
  • 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.

问题回答

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.





相关问题
Sign with RSA-1024 an SHA-256 digest: what is the size?

I was wondering: 1) if I compute the digest of some datas with SHA-512 => resulting in a hash of 64 bytes 2) and then I sign this hash with RSA-1024 => so a block of 128 bytes, which is bigger than ...

Access violation writing location

I have the following code: #include <openssl/bn.h> #include <openssl/rsa.h> unsigned char* key; RSA* rsa = RSA_new(); rsa = RSA_generate_key(1024,65537,NULL,NULL); //init pubkey key[...

How to sign an XML file with a RSA key in .NET?

I am trying to sign an XML file in C# .NET 3.5 with a private RSA Key generated by OpenSSL. Here is how I proceeded: I converted the RSA key from PEM format to XML format using the chilkat framework (...

Java to Python RSA

I m trying to encrypt a string from Java to Python, using the library Bouncy Castle J2ME on the client side and Python M2Crypto on the other. Everything is pretty good, I can decrypt it properly, but ...

storing credit card info

So I would like to modify a PHP / MySQL application in order to store credit card but not cvv and bank account info securely. PCI DSS require 1024 RSA/DSA. A small number of users will be given ...

Load RSA keys from files

I used openSSL command to create 2 files: 1 for RSA public key & 1 for RSA private key. How do I recover RSA keys using C? Specifically, I have these functions: RSA_public_encrypt(read_num, ...

热门标签