Golang crypto/rsa EncryptOAEP() and DecryptOAEP() functions example

package crypto/rsa

EncryptOAEP encrypts the given message with RSA-OAEP. The message must be no longer than the length of the public modulus less twice the hash length plus 2.

DecryptOAEP decrypts ciphertext using RSA-OAEP. If random != nil, DecryptOAEP uses RSA blinding to avoid timing side-channel attacks.

Golang crypto/rsa EncryptOAEP() and DecryptOAEP() functions usage example

 // EncryptOAEP
 msg := []byte("The secret message!")
 label := []byte("")
 md5hash := md5.New()

 encryptedmsg, err := rsa.EncryptOAEP(md5hash, rand.Reader, publickey, msg, label)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(msg), encryptedmsg)
 fmt.Println()


 // DecryptOAEP
 decryptedmsg, err := rsa.DecryptOAEP(md5hash, rand.Reader, privatekey, encryptedmsg, label)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 fmt.Printf("OAEP decrypted [%x] to \n[%s]\n",encryptedmsg, decryptedmsg)
 fmt.Println()

Please see RSA package functions example for full example.

References :

http://golang.org/pkg/crypto/rsa/#EncryptOAEP

http://golang.org/pkg/crypto/rsa/#DecryptOAEP

Advertisement