Decryption of the RSA keys using golang

Hi Colleagues,
I am looking for help related to decryption of the RSA private keys in go.
I have the passphrase details and private key, I want to decrypt the private key which is encrypted by the third party with a passphrase and shared with me.
I have explored the crypto libraries However I think those would not help in my use case because these key are encrypted from third party and encrypted keys are shared with me as a file.
Can anyone please suggest if any other go library/Packages could help me to decrypt the private keys ?

Regards,
Bhanuprakash

Maybe you can try GitHub - libp2p/go-openssl: OpenSSL bindings for Go

package main

import (
	"fmt"
	"os"

	"github.com/libp2p/go-openssl"
)

func main() {

	privateKeyFile := "test_private.pem"
	passPhrase := "thisisawesome"

	encryptedPEM, err := os.ReadFile(privateKeyFile)
	if err != nil {
		panic(err)
	}

	fmt.Print(string(encryptedPEM))

	pvt, err := openssl.LoadPrivateKeyFromPEMWithPassword(encryptedPEM, passPhrase)

	if err != nil {
		panic(err)
	}

	decryptedPEM, err := pvt.MarshalPKCS1PrivateKeyPEM()

	if err != nil {
		panic(err)
	}

	fmt.Print(string(decryptedPEM))

}

To check, create encrypted private key using openssl:

openssl genrsa -aes128 -out test_private.pem 1024  

This should prompt you to enter and verify a pass phrase.

Now, decrypt the private key using the passphrase:

openssl rsa -in test_private.pem -out decrypted_test.pem

Compare decrypted_test.pem with the value decrypted using to check if it works.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.