Standard library / crypto/x509

crypto/x509.EncryptPEMBlock()

Legacy RFC 1423 PEM encryption is unauthenticated and susceptible to padding-oracle attacks. Do not use for new key storage. Creates a password-encrypted PEM block for historical compatibility. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func EncryptPEMBlock(rand io.Reader, blockType string, data, password []byte, alg PEMCipher) (*pem.Block, error)

Parameters & Attributes

NameTypeDescription
randio.ReaderCryptographically secure random source; see description for exceptions.
blockTypestringPEM type label.
data[]bytePlain bytes to encode.
password[]bytePassword bytes for legacy PEM encryption; do not log.
algPEMCipherLegacy PEM cipher identifier.

Return Value

(*pem.Block, 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(x509.IsEncryptedPEMBlock(b))}
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