【问题标题】:Convert JAVA code KeyStore.getInstance to c#将 JAVA 代码 KeyStore.getInstance 转换为 c#
【发布时间】:2017-02-15 22:22:30
【问题描述】:

我正在尝试将以下JAVA 代码转换为C# 以获取ECPrivateKey

private static ECPrivateKey loadPrivateKey(String pkcs12Filename) throws KeyStoreException, NoSuchProviderException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
    KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
    keystore.load(new FileInputStream(pkcs12Filename), "".toCharArray());
    assert keystore.size() == 1 : "wrong number of entries in keychain";
    Enumeration<String> aliases = keystore.aliases();
    String alias = null;
    while (aliases.hasMoreElements()) {
      alias = aliases.nextElement();
    }
    return (ECPrivateKey) keystore.getKey(alias, null);
}

【问题讨论】:

    标签: java c# pkcs#12


    【解决方案1】:

    这是代码,我用这个得到了输出。

    private void GetPrivateKeyFromP12(string path, string passWord)
            {
                //this.mKeyStoreEntities = PKCSUtils.extractEntities(privateKeyFilePath, privateKeyPassword);
                /**
                 * Java has a minimum requirement for keystore password lengths.  Utilities like KeyChain will
                 * allow you to specify less but then you will receive an obscure java error when trying to
                 * load the keystore.  Check for it here and throw a meaningful error
                 */
    
                //X509Certificate certificate;
                //ECPrivateKey privateKey;
    
                using (StreamReader reader = new StreamReader(path))
                {
                    var fs = reader.BaseStream;
                    GetMerchantIdentifierField(fs);
    
                    fs.Position = 0;
                    GetPrivateKey(fs, passWord);
                }
            }
    
            private void GetPrivateKey(Stream fs, string passWord)
            {
                Pkcs12Store store = new Pkcs12Store(fs, passWord.ToCharArray());
    
                foreach (string n in store.Aliases)
                {
                    if (store.IsKeyEntry(n))
                    {
                        AsymmetricKeyEntry asymmetricKey = store.GetKey(n);
    
                        if (asymmetricKey.Key.IsPrivate)
                        {
                            this.merchantPrivateKey = asymmetricKey.Key as ECPrivateKeyParameters;
                        }
                    }
                }
            }
    

    【讨论】:

    • 嗨 raju,这段代码中的 GetMerchantIdentifierField 是什么?
    猜你喜欢
    • 2016-04-14
    • 2014-05-27
    • 2015-09-27
    • 2014-09-16
    • 1970-01-01
    • 2013-07-12
    • 2011-12-31
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多