c# - 如何从 p12 文件中读取 SecretKey?

标签 c# bouncycastle pkcs#12

我正在尝试从 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 的信息,但它仍然是加密的或编码的。我如何解码该信息?

最佳答案

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

以下是如何读取第一个条目的 key 的示例。

 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

关于c# - 如何从 p12 文件中读取 SecretKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66275367/

相关文章:

c# - 使用 C# 从 p7+p8 文件获取 PEM

java - 转换 PGP 公钥

security - 需要帮助将 P12 证书转换为 JKS

c# - 在不消耗内存的情况下运行连续的 Windows 后台任务

c# - DNX SDK版本 'dnx-clr-win-x86.1.0.0-beta8-15530'安装失败

c# - 我如何从 JS 访问 ViewBag

c# - Microsoft.Management.Infrastructure 命名空间 - Cim 类

c# - 需要使用 C# 进行 BouncyCaSTLe PGP 文件加密的示例

Java keystore 加载第一次工作,第二次抛出错误

iphone - 如何使用终端将 .p12 文件转换为 .pem 文件?