Golang crypto/x509.DecryptPEMBlock function example

package crypto/x509

DecryptPEMBlock takes a password encrypted PEM block and the password used to encrypt it and returns a slice of decrypted DER encoded bytes. It inspects the DEK-Info header to determine the algorithm used for decryption. If no DEK-Info header is present, an error is returned. If an incorrect password is detected an IncorrectPasswordError is returned.

Golang crypto/x509.DecryptPEMBlock function usage example

 blockType := "RSA PRIVATE KEY"
 password := []byte("password")


 // see http://golang.org/pkg/crypto/x509/#pkg-constants
 cipherType := x509.PEMCipherAES256

 EncryptedPEMBlock, err := x509.EncryptPEMBlock(rand.Reader,
 blockType,
 []byte("secret message"),
 password,
 cipherType)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 DecryptedPEMBlock, err := x509.DecryptPEMBlock(EncryptedPEMBlock, password)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

See https://www.socketloop.com/tutorials/golang-encrypt-and-decrypt-data-with-x509-crypto

Reference :

http://golang.org/pkg/crypto/x509/#DecryptPEMBlock

  See also : Golang crypto/x509.EncryptPEMBlock function example

Advertisement