1

我正在寻找有关如何使用智能卡解密电子邮件的建议。我在这里发布了一个关于相同的查询。Owlstead 先生建议我使用证书来解密通常可通过智能卡获得的证书。现在我对如何使用证书来获取密钥和解密电子邮件感到震惊。所以我尝试了下面的代码,但我得到 Keyset not present 异常。有人可以帮忙吗?

public static void DecryptContent(byte[] data)
        {
            X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            st.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection col = st.Certificates;

            X509Certificate2 card = null;
            // X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificates", "Select one to sign", X509SelectionFlag.SingleSelection);

            if (col.Count > 0)
            {
                for (int i = 0; i < col.Count; i++)
                {
                    var cert = col[i];

                    // Log cert.FriendlyName
                    DecryptContent(data,cert);
                }
            }

            st.Close();
        }

        public static void DecryptContent(byte[] data, X509Certificate2 certificate)
        {
            try
            {
                RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
                rsa.Decrypt(data, true);
            }
            catch (Exception e)
            {
                   // Log error info.
            }
        }
4

0 回答 0