【问题标题】:How to read a SecretKey from a p12 file?如何从 p12 文件中读取 SecretKey?
【发布时间】:2021-05-22 07:33:00
【问题描述】:

我正在尝试从 p12 文件中读取 SecretKey,但它不是在 Bouncy Castle 中创建的。需要注意的一个有趣的事情是该文件没有任何证书,并且 System.Security.Cryptography.X509Certificates 类无法读取任何内容。

  public void Pkcs12Pfx2()
    {
        Pfx bag = Pfx.GetInstance(File.ReadAllBytes(path));
        ContentInfo info = bag.AuthSafe;
        MacData mData = bag.MacData;
        DigestInfo dInfo = mData.Mac;
        AlgorithmIdentifier algId = dInfo.AlgorithmID;
        byte[] salt = mData.GetSalt();
        int itCount = mData.IterationCount.IntValue;

        Asn1OctetString content = Asn1OctetString.GetInstance(info.Content);
        AuthenticatedSafe authSafe = AuthenticatedSafe.GetInstance(content.GetOctets());
        ContentInfo[] c = authSafe.GetContentInfo();

        foreach (ContentInfo ci in c)
        {
            DerObjectIdentifier oid = ci.ContentType;

            byte[] octetsCi = null;
            if (oid.Equals(PkcsObjectIdentifiers.Data))
            {
                octetsCi = Asn1OctetString.GetInstance(ci.Content).GetOctets();
            }

            if (octetsCi != null)
            {
                Asn1Sequence seqCi = Asn1Sequence.GetInstance(octetsCi);

                foreach (Asn1Sequence subSeq in seqCi)
                {
                    SafeBag bagCi = SafeBag.GetInstance(subSeq);

                    foreach (var item in Asn1Sequence.GetInstance(bagCi.BagValue))
                    {
                        if (item.Equals(PkcsObjectIdentifiers.Pkcs8ShroudedKeyBag))
                        {

                        }
                        else
                        {
                            PrivateKeyInfo pki = new PrivateKeyInfo(algId, (Asn1Encodable)item);
                            var epki = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo("PBEWITHSHA1AND3-KEYTRIPLEDES-CBC", password.ToCharArray(), salt, itCount, pki);

                            var aa = epki.ToAsn1Object();
                        }
                    }
                }
            }
        }

对象 epki 包含 SecretKey 的信息,但它仍然被加密或编码。如何解码这些信息?

【问题讨论】:

    标签: c# bouncycastle pkcs#12


    【解决方案1】:

    如果您只是想打开文件并获取密钥,则使用Pkcs12Store 类要简单得多。您提供文件和文件密码以打开它。然后,一旦您创建了对象,您就有了一个属性 Aliases 将列出文件中所有条目的别名。然后,您只需使用别名调用“GetKey”方法即可获取条目的密钥。

    这里是如何读取第一个条目的密钥的示例。

     Pkcs12Store pkcs12Store = new Pkcs12Store(new FileStream("test.p12", FileMode.Open), "password".ToCharArray());
     AsymmetricKeyEntry key = pkcs12Store.GetKey(pkcs12Store.Aliases.Cast<string>().First());
     RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)key.Key; // here you should check what kind of key it is to then access the specific key parameters
    

    【讨论】:

    • 我测试了类似的东西,但它也不起作用。当我查看 pkcs12Store 对象时,它无法获取任何别名,仅加载了算法的数量。不知道是不是因为文件没有证书。
    • 我不这么认为,因为文件格式说它也可以只是键。你确定这是一个有效的 pkcs12 文件吗?它是如何创建的?
    • 我没有被告知它是如何创建的,而且我可能不会。 (这很可悲……)。但是当我将文件放入lapo.it 或使用 certutil:certutil -v -p pass -privatekey -dumpPFX .\key.p12 时,它可以被读取。
    • 你能用openssl导出你的私钥吗:openssl.exe pkcs12 -in test.p12 -nocerts -out privateKey.pem
    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 2017-04-13
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多