3

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

Aravind
  • 379
  • 4
  • 17
Rustam Ibragimov
  • 2,571
  • 7
  • 22
  • 33
  • 1
    Possible duplicate of ["No DEK-Info header in block" when attempting to read encrypted private key](https://stackoverflow.com/questions/32981821/no-dek-info-header-in-block-when-attempting-to-read-encrypted-private-key) – Aravind Jun 22 '17 at 16:22

2 Answers2

3

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.

Rustam Ibragimov
  • 2,571
  • 7
  • 22
  • 33
0

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

Aravind
  • 379
  • 4
  • 17