Reputation: 499
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!
Upvotes: 0
Views: 2927
Reputation: 4422
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.
Upvotes: 1