Home > other >  How to check pkcs format of a private key using golang code?
How to check pkcs format of a private key using golang code?

Time:08-10

I am trying find the encryption format of a private key whether it is pkcs1 or pkcs8. Is there any golang code that helps me identify it

CodePudding user response:

Just parse the key, err comes if mismatch.

func pkcs() {

    var pkcs8RSAPrivateKeyHex = `30820278020100300d06092a864886f70d0101010500048202623082025e02010002818100cfb1b5bf9685ffa97b4f99df4ff122b70e59ac9b992f3bc2b3dde17d53c1a34928719b02e8fd17839499bfbd515bd6ef99c7a1c47a239718fe36bfd824c0d96060084b5f67f0273443007a24dfaf5634f7772c9346e10eb294c2306671a5a5e719ae24b4de467291bc571014b0e02dec04534d66a9bb171d644b66b091780e8d020301000102818100b595778383c4afdbab95d2bfed12b3f93bb0a73a7ad952f44d7185fd9ec6c34de8f03a48770f2009c8580bcd275e9632714e9a5e3f32f29dc55474b2329ff0ebc08b3ffcb35bc96e6516b483df80a4a59cceb71918cbabf91564e64a39d7e35dce21cb3031824fdbc845dba6458852ec16af5dddf51a8397a8797ae0337b1439024100ea0eb1b914158c70db39031dd8904d6f18f408c85fbbc592d7d20dee7986969efbda081fdf8bc40e1b1336d6b638110c836bfdc3f314560d2e49cd4fbde1e20b024100e32a4e793b574c9c4a94c8803db5152141e72d03de64e54ef2c8ed104988ca780cd11397bc359630d01b97ebd87067c5451ba777cf045ca23f5912f1031308c702406dfcdbbd5a57c9f85abc4edf9e9e29153507b07ce0a7ef6f52e60dcfebe1b8341babd8b789a837485da6c8d55b29bbb142ace3c24a1f5b54b454d01b51e2ad03024100bd6a2b60dee01e1b3bfcef6a2f09ed027c273cdbbaf6ba55a80f6dcc64e4509ee560f84b4f3e076bd03b11e42fe71a3fdd2dffe7e0902c8584f8cad877cdc945024100aa512fa4ada69881f1d8bb8ad6614f192b83200aef5edf4811313d5ef30a86cbd0a90f7b025c71ea06ec6b34db6306c86b1040670fd8654ad7291d066d06d031`
    derBytes, _ := hex.DecodeString(pkcs8RSAPrivateKeyHex)

    _, err := x509.ParsePKCS1PrivateKey(derBytes)
    if err != nil {
        log.Fatalln(err)
        return
    }

}
2022/08/10 15:59:08 x509: failed to parse private key (use ParsePKCS8PrivateKey instead for this key format)

CodePudding user response:

        func main() {
        pemString := `-----BEGIN RSA PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,1ADE9ED351F5B8C7
    
    z8nRX5jG2eUxcahgzF/Sn47DWXKs/y pOcyYzTA3eBYwkqnjOzSqK9YbV8irAAZm
    uwwoGBCjn7740nCo pEYe ECC4MJBrAl6uPcX6eL0ltj1En5gqFB6bPqFrVa8uuf
    MDoSGJzIcDl9kuLK4xS0q9TE7K5ayrzfn7ecn6/KHjqgIdGtEsrZT1Ajhm6Quubs
    PhCRTldky/o1LegEoooUm2s509EGX2PixAWfTS36Z3UENOSWAgHJCdfybDR7 jvP
    lC9ZHXD/OVcgDVC K3UjF7kV5HMD CosuCwR6oXHEiKGv6X6rW46wyB17ul/zDA5
    6MbQeEDDE3uEa cpAGzeYwudCRttclsBlLKBDHv2DgjI yoGU/GuwuV5Gd9y4fYw
    5ZakGhezbm9W5g4XiuA3wTM9j4x2hNIB2Bexu8b9KlZwJnXZOY8glDcfdT5 kmQL
    CY4LlqZQU/YXFLoGpChidQmIQhxzVsYRx46NK0l hrrJJ2xGz Cj5AhqomOjq69y
    5AAjL3THAj1ys2dy54Ue1m1T4w12Oz fAICVC2mb966821cUjkSzC2QVGqE UdrS
    C9qaMtBvKxumN/88Pl3SsO1tDpgSG4OTcr Hyeh20fg3wVnFPZJZyWDVnv4cXrVs
    SYAk7m3ngAvr XOu8WOdOc8ouuiWlaFhKNpGC6MZT1FKCCD53VANeehdBbYngmrR
    oZAuQBtS1aJpMpP2zcsXcexcuZrHSdVlaU1Cs3JykIXt1kmJDKAcyz1C8dRA23fI
    QlP3Q lbohQpWdS8hSDhetxUaCYHIYHJL3fp/0hsOWrd2oJ7ukNBEPIaEK9FnKHe
    nrAp6spTrtCAFum6UAqiV6eoAyX2SwIJepPivgxcquA7rIeO/D4OZCHRsQtLMFPn
    FG7KYJ0ZcAincs5AKX67hxrZBLt1VvjvqyifdhFEJoCI0vwza1Q9tFtrkJ4dXr5W
    n5wnS8dSZnin9JWYCf1ltxVwPNxlii1jwYmIKM49D3qcmSsKsWNNrAW/nvrkOP0R
    CasZKUGgpYu6cu0T1JYayZyb5gWjawDUkZpdd0E/RS/xk5qvJ6jrcu8QoiOo0s f
    EizdBvhiLuA6SNWHj/ksXf3seTTdHxDdG91iea5t vK1ZJoJC9PTxB18ws6BALCH
    3DGXBo1vdoz7HVTq0YeTwavzhY9l3UYPcNeM6 blEuy7S0p5OoavVevynKXp /Ct
    gsODMh6Xhz1kjPIDfvuA36HJiB84FGED6d2s 7MOTh/z7p5d1OfUMu 0IsyDR6Un
    p17G82ihgZlE9MbJVnAGSpUPQd6gmtdFh7jgu2S7SYqph5P6s/aVlZsH36xVgdRG
    3pdo 7qhuWulBwN9dsdSigy0CLAOiGJAU3Pxu1QdKiwE /Qs6LmLRkLWbd4S42N2
    Ur1xwXiBC8Qz35JZpE6 MyvR4jUybSC6IMhWPHrpQ2sVw6HFlRvPN78egO9zuO6v
    UTqL30g3J5 SS ulLm16xwRgBAiLBYgvDLpfsQB3G3rShOyiNvvOneGZ8FZIerpB
    RVpv18PvAvhFICyUuxVkvTm6j1BObmMvoefPOUWeZHsE Miu/N2UoQ==
    -----END RSA PRIVATE KEY-----`
    
        block, _ := pem.Decode([]byte(pemString))
        _, err := x509.ParsePKCS8PrivateKey(block.Bytes)
        if err != nil {
            fmt.Println(err)
            fmt.Println("hi")
        } else {
            fmt.Println("bye")
        }
    }


asn1: structure error: length too large
hi
  • Related