How to get private key from PKCS#12 (.p12) file using C#

后端 未结 4 1473
忘了有多久
忘了有多久 2021-01-13 16:30

Im trying to sign some data using PKCS#12 certificate ,however i have problem with obtaining private key from PKCS#12 (.p12) file.

    public byte[] sign(str         


        
4条回答
  •  情歌与酒
    2021-01-13 16:55

    This was done for using Android - so the R.raw.key below was my file in the Android Raw folder.

    I opened key.p12 as as input stream. Which I then converted to the private key using the libraries as seen in the example.

    http://www.flexiprovider.de/examples/ExampleSMIMEsign.html

    My code looks like this

    Security.addProvider(new de.flexiprovider.core.FlexiCoreProvider());
        // Next, we have to read the private PKCS #12 file, since the the
        // private key used for signing is contained in this file:
        DERDecoder dec = new DERDecoder(getResources().openRawResource(
                R.raw.key));
        PFX pfx = new PFX();
        try {
            pfx.decode(dec);
            SafeBag safeBag = pfx.getAuthSafe().getSafeContents(0)
                    .getSafeBag(0);
            PKCS8ShroudedKeyBag kBag = (PKCS8ShroudedKeyBag) safeBag
                    .getBagValue();
            char[] password = "my password for the p12".toCharArray();
            privKey = kBag.getPrivateKey(password);
            new AsyncLoadStorage(this).execute();
        } catch (ASN1Exception e) {
    

提交回复
热议问题