0

I am trying to write a program which encrypts data using a RSA public key and and decrypts data using private key. The RSA keys were generated with openssl tool.

I found Spacemonkeygo Openssl https://github.com/spacemonkeygo/openssl wrapper for this purpose. But unable to find any sample over & also their is no document available for the same. So that I am unable to use.

Please guide me how can I use Openssl in Golang?

I am using first time encryption decryption & Openssl.

Thank you in advance!

dinu0101
  • 461
  • 2
  • 5
  • 18
  • Have you looked at the [rsa](https://golang.org/pkg/crypto/rsa/) package in the standard library? It contains everything you need to perform RSA encryption/decryption and will be a nicer interface than wrappers around openssl. – Marc Sep 16 '18 at 11:14
  • @Marc, Yes, I have seen and It's working fine. But the requirement is use Openssl not default one. – dinu0101 Sep 16 '18 at 13:37

1 Answers1

1

I am trying to write a program which encrypts data using a RSA public key and and decrypts data using private key. The RSA keys were generated with openssl tool.

You don't need an OpenSSL library package to do this: you just need some of the crypto, encoding, and other packages in the Go standard library. Namely:

Create a PEM block from the key, setting Type to "RSA PRIVATE KEY" or "RSA PUBLIC KEY", parse the keys with the x509 functions (PKIX for public), use a type assertion to make it the appropriate RSA public/private type, encrypt the message using OAEP padding, an SHA-256 hash function, and rand.Reader for a source of entropy, base64 encode the resulting cipher if you're sending it as text rather than binary, then base64 decode it and decrypt it using the same but with the private key on the other side.

See in particular func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) ([]byte, error).

Read the documentation in these packages and some general info about encryption and RSA, there are also usage examples for each of these packages on StackOverflow -- though perhaps not put all together.

Every package you need for the described goal is in the Go standard library.

You may need to check that your PKCS function version (e.g. PKCS8) lines up with the private key produced by your OpenSSL version.

jrefior
  • 4,092
  • 1
  • 19
  • 29
  • Hi Jrefior, I made a programme that encrypt from RSA Public key & decrypt with private key. But that is from the Golang default. But t he requirement is that we want to use Openssl like this https://sosedoff.com/2015/05/22/data-encryption-in-go-using-openssl.html – dinu0101 Sep 16 '18 at 13:46
  • @dinu0101 You didn't describe (or even hint at) that in your question. So you're looking to combine RSA with AES somehow? Go does have a standard AES library, and you can use [`func NewCipher(key []byte) (cipher.Block, error)`](https://golang.org/pkg/crypto/aes/#NewCipher) with a 32-byte key argument to select AES-256. – jrefior Sep 16 '18 at 16:43