Standard library / crypto/rsa
crypto/rsa.PrivateKey.Decrypt()
Implements crypto.Decrypter. OAEPOptions selects OAEP; nil or PKCS1v15DecryptOptions selects legacy decryption. Prefer explicit OAEP options. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func (priv *PrivateKey) Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rand | io.Reader | Cryptographically secure random source; see description for exceptions. |
| ciphertext | []byte | Encrypted message bytes. |
| opts | crypto.DecrypterOpts | Algorithm-specific options; see description for nil behavior. |
Return Value
(plaintext []byte, err error)
Result as described above; check any returned error before use.
Working Example
Example Snippetgo
package main
import ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" "fmt")
func main() { // Historical interoperability demonstration only. Prefer modern protocols for new designs. k := must(rsa.GenerateKey(rand.Reader, 2048)) ct := must(rsa.EncryptOAEP(sha256.New(), rand.Reader, &k.PublicKey, []byte("Go"), []byte("demo"))) p := must(k.Decrypt(nil, ct, &rsa.OAEPOptions{Hash: crypto.SHA256, Label: []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) }}