Standard library / crypto/rsa

crypto/rsa.DecryptOAEP()

Decrypts OAEP ciphertext using the corresponding private key, hash and exact label. In Go 1.23 random is ignored; reject any failure without exposing details. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) ([]byte, error)

Parameters & Attributes

NameTypeDescription
hashhash.HashHash algorithm, hash instance or digest as specified by the signature and description.
randomio.ReaderCryptographically secure random source; see description for exceptions.
priv*PrivateKeyPrivate key used for the operation.
ciphertext[]byteEncrypted message bytes.
label[]byteOAEP label; must match encryption.

Return Value

([]byte, error)

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

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "crypto/rsa"  "crypto/sha256"  "fmt")
func main() {  k := must(rsa.GenerateKey(rand.Reader, 2048))  ct := must(rsa.EncryptOAEP(sha256.New(), rand.Reader, &k.PublicKey, []byte("Go"), []byte("demo")))  p := must(rsa.DecryptOAEP(sha256.New(), nil, k, ct, []byte("demo")))  fmt.Println(string(p))}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