2012-11-25 50 views

回答

39

你需要某種格式來編組密鑰。由圍棋標準庫支持的一種格式,可以在這裏找到:http://golang.org/pkg/crypto/x509/#MarshalPKCS1PrivateKey

func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte 

的反函數是http://golang.org/pkg/crypto/x509/#ParsePKCS1PrivateKey

func ParsePKCS1PrivateKey(der []byte) (key *rsa.PrivateKey, err error) 

然而,這是相對標準來編碼編組鑰匙插入一個PEM文件。

pemdata := pem.EncodeToMemory(
    &pem.Block{ 
     Type: "RSA PRIVATE KEY", 
     Bytes: x509.MarshalPKCS1PrivateKey(key), 
    }, 
) 

你可以找到一個完整的例子here

+1

公衆部分? – 2014-05-23 16:27:35

15

由於你的問題的公鑰部分沒有得到回答,我只是碰到了同樣的問題,解決了這個問題,那就是:

注意&在爭論面前MarshalPKIXPublicKey

Priv := rsa.GenerateKey(rand.Reader, 4096) 

PubASN1, err := x509.MarshalPKIXPublicKey(&Priv.PublicKey) 
if err != nil { 
    // do something about it 
} 

pubBytes = pem.EncodeToMemory(&pem.Block{ 
    Type: "RSA PUBLIC KEY", 
    Bytes: PubASN1, 
}) 

ioutil.WriteFile("key.pub", PubBytes, 0644) 

相關閱讀:

  • MarshalPKIXPublicKey(酒館接口{})([]字節,錯誤)godoc
  • EncodeToMemory度(b *塊)[]字節godoc
  • godoc

PS:MarshalPKIXPublicKey還接受ECDSA密鑰,ajust適當的佩姆頭。

6

這是顯示公鑰和私鑰的導入和導出代碼片段。這是基於其他超級有用的答案,以及來自官方文檔的複製麪食。

package main 

import (
    "crypto/rand" 
    "crypto/rsa" 
    "crypto/x509" 
    "encoding/pem" 
    "errors" 
    "fmt" 
) 

func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) { 
    privkey, _ := rsa.GenerateKey(rand.Reader, 4096) 
    return privkey, &privkey.PublicKey 
} 

func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string { 
    privkey_bytes := x509.MarshalPKCS1PrivateKey(privkey) 
    privkey_pem := pem.EncodeToMemory(
      &pem.Block{ 
        Type: "RSA PRIVATE KEY", 
        Bytes: privkey_bytes, 
      }, 
    ) 
    return string(privkey_pem) 
} 

func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) { 
    block, _ := pem.Decode([]byte(privPEM)) 
    if block == nil { 
      return nil, errors.New("failed to parse PEM block containing the key") 
    } 

    priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) 
    if err != nil { 
      return nil, err 
    } 

    return priv, nil 
} 

func ExportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error) { 
    pubkey_bytes, err := x509.MarshalPKIXPublicKey(pubkey) 
    if err != nil { 
      return "", err 
    } 
    pubkey_pem := pem.EncodeToMemory(
      &pem.Block{ 
        Type: "RSA PUBLIC KEY", 
        Bytes: pubkey_bytes, 
      }, 
    ) 

    return string(pubkey_pem), nil 
} 

func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error) { 
    block, _ := pem.Decode([]byte(pubPEM)) 
    if block == nil { 
      return nil, errors.New("failed to parse PEM block containing the key") 
    } 

    pub, err := x509.ParsePKIXPublicKey(block.Bytes) 
    if err != nil { 
      return nil, err 
    } 

    switch pub := pub.(type) { 
    case *rsa.PublicKey: 
      return pub, nil 
    default: 
      break // fall through 
    } 
    return nil, errors.New("Key type is not RSA") 
} 

func main() { 

    // Create the keys 
    priv, pub := GenerateRsaKeyPair() 

    // Export the keys to pem string 
    priv_pem := ExportRsaPrivateKeyAsPemStr(priv) 
    pub_pem, _ := ExportRsaPublicKeyAsPemStr(pub) 

    // Import the keys from pem string 
    priv_parsed, _ := ParseRsaPrivateKeyFromPemStr(priv_pem) 
    pub_parsed, _ := ParseRsaPublicKeyFromPemStr(pub_pem) 

    // Export the newly imported keys 
    priv_parsed_pem := ExportRsaPrivateKeyAsPemStr(priv_parsed) 
    pub_parsed_pem, _ := ExportRsaPublicKeyAsPemStr(pub_parsed) 

    fmt.Println(priv_parsed_pem) 
    fmt.Println(pub_parsed_pem) 

    // Check that the exported/imported keys match the original keys 
    if priv_pem != priv_parsed_pem || pub_pem != pub_parsed_pem { 
      fmt.Println("Failure: Export and Import did not result in same Keys") 
    } else { 
      fmt.Println("Success") 
    } 
} 
+0

最後您的驗證有一些問題:條件應該是'||'而不是'&&' – Max