Standard library / crypto/rsa

crypto/rsa.DecryptPKCS1v15SessionKey()

Legacy interoperability only; PKCS#1 v1.5 encryption is vulnerable to padding-oracle attacks. Use OAEP or a reviewed hybrid protocol for new designs. Copies a valid plaintext of the requested length into a prefilled random key buffer; invalid padding leaves it unchanged. Public length/range errors may still be returned. This API alone does not make an application oracle-safe. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func DecryptPKCS1v15SessionKey(random io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error

Parameters & Attributes

NameTypeDescription
randomio.ReaderCryptographically secure random source; see description for exceptions.
priv*PrivateKeyPrivate key used for the operation.
ciphertext[]byteEncrypted message bytes.
key[]byteKey bytes; see description for size.

Return Value

error

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

Working Example

Example Snippetgo
package main
import (  "bytes"  "crypto/rand"  "crypto/rsa"  "fmt")
func main() {  // Historical interoperability demonstration only. Prefer modern protocols for new designs.  k := must(rsa.GenerateKey(rand.Reader, 2048))  secret := make([]byte, 32)  _, err := rand.Read(secret)  check(err)  ct := must(rsa.EncryptPKCS1v15(rand.Reader, &k.PublicKey, secret))  out := make([]byte, 32)  _, err = rand.Read(out)  check(err)  check(rsa.DecryptPKCS1v15SessionKey(nil, k, ct, out))  fmt.Println(bytes.Equal(secret, out))}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