RSA/ECDSA Private/Public Key To/From File
Private Key
Write RSA Private Key to file
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
)
func saveRSAPrivateKey(key *rsa.PrivateKey, filename string) error {
keyBytes := x509.MarshalPKCS1PrivateKey(key)
pemBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: keyBytes,
}
pemFile, err := os.Create(filename)
if err != nil {
return err
}
defer pemFile.Close()
return pem.Encode(pemFile, pemBlock)
}
func main(){
// Example Usage
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
err = saveRSAPrivateKey(privateKey, "rsa_private.pem")
if err != nil {
panic(err)
}
}
Read RSA Private Key from file
Write ECDSA Private Key to file
Read ECDSA Private Key from file
Public Key
Write Public Key to file
Read Public Key from file
Last updated