Home > Backend Development > Golang > How to Generate RSA Keys in Go: Equivalently to OpenSSL's `genrsa`?

How to Generate RSA Keys in Go: Equivalently to OpenSSL's `genrsa`?

Mary-Kate Olsen
Release: 2024-12-07 17:43:12
Original
203 people have browsed it

How to Generate RSA Keys in Go: Equivalently to OpenSSL's `genrsa`?

Generating RSA Keys in Go: Equivalency to OpenSSL's genrsa Utility

Problem:

Creating RSA key pairs and extracting public keys can be a common task in cryptography. OpenSSL's genrsa utility provides a convenient way to accomplish this. However, how can we achieve the same functionality using Go?

Solution:

To generate and extract RSA key components in Go, you can follow these steps:

1. Generate an RSA Key Pair

rsa.GenerateKey can be used to create an RSA key pair. This function takes a *rand.Rand source as input.

2. Extract the Public Key

To obtain the public component from the private key, use rsa.PrivateKey.Public. It will return an rsa.PublicKey interface.

3. Convert to PKCS#1 PEM Format

Both private and public keys must be converted to PKCS#1 ASN.1 DER form. Use x509.MarshalPKCS1PrivateKey and x509.MarshalPKCS1PublicKey for this purpose.

4. Encode to PEM Blocks

PEM encoding is used to wrap the converted keys in recognizable PEM blocks. pem.EncodeToMemory can achieve this.

5. Write Output to Files

The final step involves writing the private and public keys to separate files.

Example:

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "io/ioutil"
)

func main() {
    filename := "key"
    bitSize := 4096

    // Generate RSA key pair.
    key, err := rsa.GenerateKey(rand.Reader, bitSize)
    if err != nil {
        panic(err)
    }

    // Extract public component.
    pub := key.Public()

    // Encode private key to PKCS#1 PEM.
    keyPEM := pem.EncodeToMemory(
        &pem.Block{
            Type:  "RSA PRIVATE KEY",
            Bytes: x509.MarshalPKCS1PrivateKey(key),
        },
    )

    // Encode public key to PKCS#1 PEM.
    pubPEM := pem.EncodeToMemory(
        &pem.Block{
            Type:  "RSA PUBLIC KEY",
            Bytes: x509.MarshalPKCS1PublicKey(pub.(*rsa.PublicKey)),
        },
    )

    // Write private key to file.
    if err := ioutil.WriteFile(filename+".rsa", keyPEM, 0700); err != nil {
        panic(err)
    }

    // Write public key to file.
    if err := ioutil.WriteFile(filename+".rsa.pub", pubPEM, 0755); err != nil {
        panic(err)
    }
}
Copy after login

This example will generate two files: key.rsa (private key) and key.rsa.pub (public key).

The above is the detailed content of How to Generate RSA Keys in Go: Equivalently to OpenSSL's `genrsa`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template