Ejemplo n.º 1
0
        /// <summary>
        /// 从证书库中获取私钥和公钥
        /// </summary>
        /// <param name="subjectName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string GetPrivateKeyFromStore(string subjectName, string password)
        {
            try
            {
                subjectName = "CN=" + subjectName;
                //CurrentUser=当前用户   LocalMachine=本地计算机
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadWrite);
                X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
                foreach (X509Certificate2 x509 in storecollection)
                {
                    if (x509.Subject != subjectName)
                    {
                        continue;
                    }
                    x509.Export(X509ContentType.Pfx, password);
                    var privateKey = x509.PrivateKey.ToXmlString(true);
                    var publicKey  = x509.PublicKey.Key.ToXmlString(false);

                    var str = "逍遥帝君 15210253 xiaoyaodijun";

                    var result  = RSAHelper.RSAEncrypt(publicKey, str);
                    var content = RSAHelper.RSADecrypt(privateKey, result);
                }
                return("");
            }
            catch (Exception)
            {
                return("");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从私钥文件中读取私钥
        /// </summary>
        public void GetPrivateKey()
        {
            try
            {
                X509Certificate2 x509 = new X509Certificate2(pfxPath, "123456", X509KeyStorageFlags.Exportable);
                var publicKey         = x509.PublicKey.Key.ToXmlString(false);
                var privateKey        = x509.PrivateKey.ToXmlString(true);

                var str = "逍遥帝君 15210253 xiaoyaodijun";

                var result  = RSAHelper.RSAEncrypt(publicKey, str);
                var content = RSAHelper.RSADecrypt(privateKey, result);
            }
            catch (Exception)
            {
                throw;
            }
        }