main.go 650 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/pem"
  7. )
  8. func main() {
  9. key_pair, err := rsa.GenerateKey(rand.Reader, 4096)
  10. if err != nil {
  11. panic(err)
  12. }
  13. // encrypt as pem
  14. new_private_key := x509.MarshalPKCS1PrivateKey(key_pair)
  15. new_public_key := x509.MarshalPKCS1PublicKey(&key_pair.PublicKey)
  16. if err != nil {
  17. panic(err)
  18. }
  19. private_key_pem := pem.EncodeToMemory(&pem.Block{
  20. Type: "RSA PRIVATE KEY",
  21. Bytes: new_private_key,
  22. })
  23. public_key_pem := pem.EncodeToMemory(&pem.Block{
  24. Type: "RSA PUBLIC KEY",
  25. Bytes: new_public_key,
  26. })
  27. println(string(private_key_pem))
  28. println(string(public_key_pem))
  29. }