Generate RSA key pairs in Go using crypto/rand and crypto/rsa packages.
Use the crypto/rand and crypto/rsa packages to generate a private key, then derive the public key from it.
package main
import (
"crypto/rand"
"crypto/rsa"
"fmt"
)
func main() {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
pub := &priv.PublicKey
fmt.Printf("Private Key: %+v\nPublic Key: %+v\n", priv, pub)
}
Generating RSA key pairs in Go creates a matching set of cryptographic keys used to secure data. The private key stays secret on your machine to decrypt messages, while the public key is shared openly so others can encrypt messages for you. It is like creating a unique lock and key where anyone can lock a box, but only you have the key to open it.