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 :
Advertisement
Something interesting
Tutorials
+10k Golang : Concatenate (combine) buffer data example
+8.6k Golang : Populate slice with sequential integers example
+13.7k Golang : How to check if IP address is in range
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+7k Golang : Multiplexer with net/http and map
+14.5k Golang : Loop each day of the current month example
+4.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+20k Golang : GORM create record or insert new record into database example
+17.6k Golang : Get host name or domain name from IP address
+4.4k Golang : Detect words using using consecutive letters in a given string
+10.1k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions