package main import ( "crypto/rand" "crypto/rsa" "encoding/json" "fmt" ) func main() { privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { fmt.Println("Error generating private key: ", err) return } privateKeyBytes, err := json.Marshal(privateKey) if err != nil { fmt.Println("Error marshaling private key: ", err) return } // Do further processing with privateKeyBytes }
package main import ( "crypto/rand" "crypto/rsa" "crypto/sha256" "fmt" ) func main() { privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { fmt.Println("Error generating private key: ", err) return } data := []byte("Hello, World!") label := []byte("") encryptedData, err := rsa.EncryptOAEP( sha256.New(), rand.Reader, &privateKey.PublicKey, data, label, ) if err != nil { fmt.Println("Error encrypting data: ", err) return } decryptedData, err := rsa.DecryptOAEP( sha256.New(), rand.Reader, privateKey, encryptedData, label, ) if err != nil { fmt.Println("Error decrypting data: ", err) return } fmt.Println(string(decryptedData)) }In this example, we generate an RSA private key with length 2048 bits using the GenerateKey() function. We then use this private key to decrypt encrypted data created using the RSA public key. We encrypt the data using the EncryptOAEP() function and decrypt it using the DecryptOAEP() function. The decryptedData variable will contain the original plaintext data. Package library: crypto.rsa