从文件中加载加密的PKCS1私钥

huangapple go评论82阅读模式
英文:

Load encrypted PKCS1 private key from file

问题

我有一个PKCS1私钥文件,并使用以下代码加载它:

b, err := ioutil.ReadFile(path)
if err != nil {
    return nil, err
}

然后,我尝试将其转换为私钥对象:

block, _ := pem.Decode(b)
der, err := x509.DecryptPEMBlock(block, []byte("qwerty"))
if err != nil {
    return nil, err
}

bytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: der})

return x509.ParsePKCS1PrivateKey(bytes)

但是这段代码在DecryptPEMBlock中抛出异常:

x509: no DEK-Info header in block

我在golang的文档中没有找到关于此问题的任何说明。

英文:

I have a PKCS1 private key in a file and I load it using

b, err := ioutil.ReadFile(path)
if err != nil {
	return nil, err
}

Then, I try to convert it into private key object

block, _ := pem.Decode(b)
der, err := x509.DecryptPEMBlock(block, []byte("qwerty"))
if err != nil {
	return nil, err
}

bytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: der})

return x509.ParsePKCS1PrivateKey(bytes)

But this code throws exception in DecryptPEMBlock

x509: no DEK-Info header in block

I didn't find any documentation about this in golang

答案1

得分: 3

我犯了一个关于私钥文件的错误,这里是一个可用的代码:

func GetPrivateKey(path string) (*rsa.PrivateKey, error) {
    b, err := ioutil.ReadFile(path)
    if err != nil {
        return nil, err
    }

    block, _ := pem.Decode(b)
    der, err := x509.DecryptPEMBlock(block, []byte(*PrivateKeyPassword))
    if err != nil {
        return nil, err
    }

    return x509.ParsePKCS1PrivateKey(der)
}

附注:Go语言确实有一个用于解密PKCS1私钥的包,但没有用于PKCS8的包。

英文:

I made a mistake with my private key file and here is a working code

func GetPrivateKey(path string) (*rsa.PrivateKey, error) {
    b, err := ioutil.ReadFile(path)
    if err != nil {
	    return nil, err
    }

    block, _ := pem.Decode(b)
    der, err := x509.DecryptPEMBlock(block, []byte(*PrivateKeyPassword))
    if err != nil {
	    return nil, err
    }

    return x509.ParsePKCS1PrivateKey(der)
}

P.S. Go does have a package to decrypt PKCS1 private keys, but does not have for PKCS8.

答案2

得分: 0

Go似乎没有用于解码PKCS文件的包。

请查看此链接以获取更多详细信息PKCS

我找不到一个用于解码PKCS1密钥的包。

使用此包来解码PKCS8文件,有一些用于解码PKCS8、PKCS10和PKCS12的包,但没有用于PKCS1的包。

英文:

Go does not have package to decode PKCS files it seems

Checkout this link for more details PKCS

I cant find a package to decode PKCS1 keys

Use this package to decode the PKCS8 files, there are some packages to decode PKCS8, PKCS10, PKCS12 but not for PKCS1

huangapple
  • 本文由 发表于 2017年6月21日 18:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/44673695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定