Skip to content

Instantly share code, notes, and snippets.

@d4z3x
Created June 8, 2020 20:40
Show Gist options
  • Save d4z3x/d6226b524fed429a480bffb3b8f472c8 to your computer and use it in GitHub Desktop.
Save d4z3x/d6226b524fed429a480bffb3b8f472c8 to your computer and use it in GitHub Desktop.
some basic pem decode ways using the crypto interface
func decodeBackToPEMWithName(fname string, pk crypto.PrivateKey) string {
pemPrivateFile, _ := os.Create(fname)
var pemPrivateBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(pk.(*rsa.PrivateKey)),
}
pem.Encode(pemPrivateFile, pemPrivateBlock)
pemPrivateFile.Close()
return string(pemPrivateBlock.Bytes)
}
func decodeBackToPEM(pk crypto.PrivateKey) string {
pemPrivateFile, _ := os.Create("DecryptBytes_priv.key")
var pemPrivateBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(pk.(*rsa.PrivateKey)),
}
pem.Encode(pemPrivateFile, pemPrivateBlock)
pemPrivateFile.Close()
return string(pemPrivateBlock.Bytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment