Go Language - RSA Encryption And Decryption Example
The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone.
The following illustration highlights how asymmetric cryptography works:
Example 1: RSA Encryption and Decryption with OAEP,
package main
//Required imports for Encryption & Decryption
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"encoding/base64"
"fmt"
)
func main() {
//3072 is the number of bits for RSA
bitSize := 3072
//Generate RSA keys
privateKey, err := rsa.
GenerateKey(rand.Reader, bitSize)
if err != nil {
panic(err)
}
publicKey := privateKey.PublicKey
//Your secret text
secretMessage := "My Secret Text"
//Encryption
encryptedMessage := EncryptWithPublicKey(secretMessage,
publicKey)
//Print Cipher Text on the console
fmt.Println("Cipher Text:", encryptedMessage)
//Print secret text on the console
fmt.Println("Secret text:",
DecryptWithPrivateKey(encryptedMessage, *privateKey))
}
//Encryption with OAEP padding
func EncryptWithPublicKey(secretMessage string,
key rsa.PublicKey) string {
rng := rand.Reader
ciphertext, err := rsa.EncryptOAEP(sha512.New(),
rng, &key, []byte(secretMessage), nil)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(ciphertext)
}
//Decryption
func DecryptWithPrivateKey(cipherText string,
privKey rsa.PrivateKey) string {
//Decode the Cipher text
ct, err := base64.StdEncoding.DecodeString(cipherText)
rng := rand.Reader
secrettext, err := rsa.DecryptOAEP(sha512.New(),
rng, &privKey, ct, nil)
if err != nil {
panic(err)
}
return string(secrettext)
}
Run the application:
go run rsaoaepdemo.go
Output:
Example 2: RSA Encryption and Decryption with PKCS1v15,
package main
//Required imports for Encryption & Decryption
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"fmt"
)
func main() {
//2048 is the number of bits for RSA
bitSize := 2048
//Generate RSA keys
privateKey, err := rsa.
GenerateKey(rand.Reader, bitSize)
if err != nil {
panic(err)
}
publicKey := privateKey.PublicKey
//Your secret text
secretMessage := "My Secret Text"
//Encryption
encryptedMessage := Encrypt(secretMessage, publicKey)
//Print Cipher Text on the console
fmt.Println("Cipher Text:", encryptedMessage)
//Print secret text on the console
fmt.Println("Plaintext:",
Decrypt(encryptedMessage, *privateKey))
}
//Encryption with PKCS1v15 padding
func Encrypt(secretMessage string,
key rsa.PublicKey) string {
rng := rand.Reader
ciphertext, err := rsa.EncryptPKCS1v15(rng, &key,
[]byte(secretMessage))
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(ciphertext)
}
//Decryption
func Decrypt(cipherText string,
privKey rsa.PrivateKey) string {
//Decode the Cipher text
ct, err := base64.StdEncoding.DecodeString(cipherText)
rng := rand.Reader
text, err := rsa.DecryptPKCS1v15(rng, &privKey, ct)
if err != nil {
panic(err)
}
return string(text)
}