Standard library / crypto/rsa

crypto/rsa.EncryptPKCS1v15()

Legacy interoperability only; PKCS#1 v1.5 encryption is vulnerable to padding-oracle attacks. Use OAEP or a reviewed hybrid protocol for new designs. Encrypts up to modulus size minus 11 bytes. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func EncryptPKCS1v15(random io.Reader, pub *PublicKey, msg []byte) ([]byte, error)

Parameters & Attributes

NameTypeDescription
randomio.ReaderCryptographically secure random source; see description for exceptions.
pub*PublicKeyPublic key used for the operation.
msg[]byteMessage bytes to encrypt.

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"  "fmt")
func main() {  // Historical interoperability demonstration only. Prefer modern protocols for new designs.  k := must(rsa.GenerateKey(rand.Reader, 2048))  ct := must(rsa.EncryptPKCS1v15(rand.Reader, &k.PublicKey, []byte("Go")))  fmt.Println(len(ct) == k.Size())}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