openssl:
/bin/bash
# Generate an RSA private key to PKCS #1, ASN.1 DER form.
openssl genrsa -out rsa_prikey.p1 1024
# Generate an RSA public key to PKCS #1, ASN.1 DER form.
openssl rsa -in rsa_prikey.p1 -pubout -RSAPublicKey_out > rsa_pubkey.p1
# Convert an RSA public key to PKCS #8 from PKCS #1, ASN.1 DER form.
openssl rsa -RSAPublicKey_in -in rsa_pubkey.p1 -pubout > rsa_pubkey.p8
# Generate an RSA private key to PKCS #1, ASN.1 DER form.
openssl genrsa -out rsa_prikey.p1 1024
# Generate an RSA public key to PKCS #8, ASN.1 DER form.
openssl rsa -in rsa_prikey.p1 -pubout -out rsa_pubkey.p8
# Convert an RSA private key to PKCS #8 from PKCS #1, ASN.1 DER form.
openssl pkcs8 -in rsa_prikey.p1 -topk8 -out rsa_prikey.p8 -nocrypt
golang:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
)
func main() {
// Generate an RSA keypair.
rsaKey, _ := rsa.GenerateKey(rand.Reader, 1024)
// Generate an RSA private key to PKCS #1, ASN.1 DER form.
priPKCS1Key := x509.MarshalPKCS1PrivateKey(rsaKey)
priPKCS1Block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: priPKCS1Key,
}
priPKCS1Bytes := pem.EncodeToMemory(priPKCS1Block)
fmt.Printf("%s", priPKCS1Bytes)
// Generate an RSA private key to PKCS #8, ASN.1 DER form.
priPKCS8Key, _ := x509.MarshalPKCS8PrivateKey(rsaKey)
priPKCS8Block := &pem.Block{
Type: "PRIVATE KEY",
Bytes: priPKCS8Key,
}
priPKCS8Bytes := pem.EncodeToMemory(priPKCS8Block)
fmt.Printf("%s", priPKCS8Bytes)
// Generate an RSA public key to PKCS #1, ASN.1 DER form.
pubPKCS1Key := x509.MarshalPKCS1PublicKey(&rsaKey.PublicKey)
pubPKCS1Block := &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubPKCS1Key,
}
pubPKCS1Bytes := pem.EncodeToMemory(pubPKCS1Block)
fmt.Printf("%s", pubPKCS1Bytes)
// Generate an RSA public key to PKCS #8, ASN.1 DER form.
pubPKCS8Key, _ := x509.MarshalPKIXPublicKey(&rsaKey.PublicKey)
pubPKCS8Block := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubPKCS8Key,
}
pubPKCS8Bytes := pem.EncodeToMemory(pubPKCS8Block)
fmt.Printf("%s", pubPKCS8Bytes)
}
ECC
openssl:
/bin/bash
# Generate an EC private key to SEC 1, ASN.1 DER form.
# It likes PKCS #1, ASN.1 DER form (-----BEGIN EC PRIVATE KEY-----)
openssl ecparam -name prime256v1 -genkey -noout -out ec_prikey.tradfile
# Convert an EC private key to PKCS #8 from SEC 1, ASN.1 DER form.
openssl pkcs8 -topk8 -in ec_prikey.tradfile -out ec_prikey.p8 -nocrypt
# Generate an EC public key to PKCS #8, ASN.1 DER form.
openssl ec -in ec_prikey.p8 -pubout -out ec_pubkey.p8
# Generate an EC private key to SEC 1, ASN.1 DER form.
openssl ecparam -name prime256v1 -genkey -noout -out ec_prikey.tradfile
# Generate an EC public key to PKCS #8, ASN.1 DER.
openssl ec -in ec_prikey.tradfile -pubout -out ec_pubkey.p8
golang:
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
)
func main() {
// Generate an EC keypair.
ecKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
// Generate an EC private key to SEC 1, ASN.1 DER form.
priECKey, _ := x509.MarshalECPrivateKey(ecKey)
priECBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: priECKey,
}
priECBytes := pem.EncodeToMemory(priECBlock)
fmt.Printf("%s", priECBytes)
// Generate an EC public key to PKCS #8, ASN.1 DER form.
pubPKCS8Key, _ := x509.MarshalPKIXPublicKey(&ecKey.PublicKey)
pubPKCS8Block := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubPKCS8Key,
}
pubPKCS8Bytes := pem.EncodeToMemory(pubPKCS8Block)
fmt.Printf("%s", pubPKCS8Bytes)
}