最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Best approch to decode the PKCS12 file and get the encrypted private key from it using JavaScript - Stack Overflow

programmeradmin0浏览0评论

Please suggest any idea to decode the PKCS12 file and get the encrypted private key from it using JavaScript. I know that it can be done very easily using Java Keytool mand and Java Security package. But I want it to be done by Java Script. Bellow is my actual requirement.

I have a ".p12" extention file which is one of the formats of pkcs12. It should be decoded first and need to trace out the decoded file where exactly the encrypted Private key is placed. Need to get that encrypted Private key and Decrypt it and send it to the receiver. And all this Should be done only in JAVASCRIPT.

Please suggest any idea to decode the PKCS12 file and get the encrypted private key from it using JavaScript. I know that it can be done very easily using Java Keytool mand and Java Security package. But I want it to be done by Java Script. Bellow is my actual requirement.

I have a ".p12" extention file which is one of the formats of pkcs12. It should be decoded first and need to trace out the decoded file where exactly the encrypted Private key is placed. Need to get that encrypted Private key and Decrypt it and send it to the receiver. And all this Should be done only in JAVASCRIPT.

Share Improve this question edited Jun 19, 2013 at 4:27 user2099644 asked Jun 19, 2013 at 4:24 user2099644user2099644 511 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I think this might be what you are looking for:

"A native implementation of TLS (and various other cryptographic tools) in JavaScript."

https://github./digitalbazaar/forge#pkcs12

It sounds like this example is close:

// decode p12 from base64
var p12Der = forge.util.decode64(p12b64);
// get p12 as ASN.1 object
var p12Asn1 = forge.asn1.fromDer(p12Der);
// decrypt p12
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, 'password');
// look at pkcs12.safeContents

// generate p12, base64 encode
var p12Asn1 = forge.pkcs12.toPkcs12Asn1(
  privateKey, certificateChain, 'password');
var p12Der = forge.asn1.ToDer(p12Asn1).getBytes();
var p12b64 = forge.util.encode64(p12Der);

Rgds....Hoonto/Matt

This will work Perfectly

   // get p12 as ASN.1 object

  var p12Asn1 = forge.asn1.fromDer(buffer);
  // decrypt p12 using the password 'password'
  var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);
  // get bags by type
  var certBags = p12.getBags({bagType: forge.pki.oids.certBag});
  var pkeyBags = p12.getBags({bagType: forge.pki.oids.pkcs8ShroudedKeyBag});
  // fetching certBag
  var certBag = certBags[forge.pki.oids.certBag][0];
  // fetching keyBag
  var keybag = pkeyBags[forge.pki.oids.pkcs8ShroudedKeyBag][0];
  // generate pem from private key
  var privateKeyPem = forge.pki.privateKeyToPem(keybag.key);
  // generate pem from cert
  var certificate = forge.pki.certificateToPem(certBag.cert);

Thanks to the examples from @Ujjawal and @hoonto I was able to get the following working well.

const decodePKCS12 = (
  file // Dom File object
) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = evt => {
      try {
        const binary = evt && evt.target ? evt.target.result : null
        if (!binary) {
          reject(new Error('No file data'))
        }
        const p12Asn1 = asn1.fromDer(binary)
        const p12 = pkcs12.pkcs12FromAsn1(p12Asn1)

        const certBags = p12.getBags({bagType: pki.oids.certBag})
        const pkeyBags = p12.getBags({bagType: pki.oids.pkcs8ShroudedKeyBag})

        const certBag = certBags[pki.oids.certBag][0]
        const keybag = pkeyBags[pki.oids.pkcs8ShroudedKeyBag][0]

        const certificate = pki.certificateToPem(certBag.cert)
        const privateKey = pki.privateKeyToPem(keybag.key)
        resolve({certificate, privateKey})
      } catch (e) {
        reject(e)
      }
    }
    reader.onerror = reject
    reader.readAsBinaryString(file)
  })
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论