Standard library / crypto/x509

crypto/x509.DecryptPEMBlock()

Legacy RFC 1423 PEM encryption is unauthenticated and susceptible to padding-oracle attacks. Do not use for new key storage. Decrypts historical PEM data; a wrong password may not always be detected. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error)

Parameters & Attributes

NameTypeDescription
b*pem.BlockBinary OID content or PEM block, according to signature.
password[]bytePassword bytes for legacy PEM encryption; do not log.

Return Value

([]byte, error)

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "crypto/x509"  "fmt")
func main() {  // Historical interoperability demonstration only. Prefer modern protocols for new designs.  b := must(x509.EncryptPEMBlock(rand.Reader, "DEMO", []byte("Go"), []byte("test-only"), x509.PEMCipherAES256))  fmt.Println(string(must(x509.DecryptPEMBlock(b, []byte("test-only")))))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference