.net中RSA的加密与解密

        #region
        /// <summary>
        /// RSA获取公钥私钥
        /// </summary>
        /// <param name="str_PrivateKey"></param>
        /// <param name="str_PublicKey"></param>
        public  void RSACreateKey(ref string str_PublicKey, ref string str_PrivateKey)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(512);
            //str_PublicKey = Convert.ToBase64String(RSA.ExportCspBlob(false));
            //str_PrivateKey = Convert.ToBase64String(RSA.ExportCspBlob(true));


            str_PublicKey = RSA.ToXmlString(false);
            str_PrivateKey = RSA.ToXmlString(true);
        }


        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="str_PrivateKey"></param>
        /// <param name="str_PublicKey"></param>
        /// <returns></returns>
        public  string RSAEncrypt(string source, string str_PublicKey)
        {
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();


                RSA.FromXmlString(str_PublicKey);


                //str_PublicKey = Convert.ToBase64String(RSA.ExportCspBlob(false));
                //str_PrivateKey = Convert.ToBase64String(RSA.ExportCspBlob(true));
                byte[] DataToEncrypt = Encoding.UTF8.GetBytes(source);


                byte[] bs = RSA.Encrypt(DataToEncrypt, false);
                //str_PublicKey = Convert.ToBase64String(RSA.ExportCspBlob(false));
                //str_PrivateKey = Convert.ToBase64String(RSA.ExportCspBlob(true));
                string encrypttxt = Convert.ToBase64String(bs);


                return encrypttxt;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }


        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="strRSA"></param>
        /// <param name="str_PrivateKey"></param>
        /// <returns></returns>
        public  string RSADecrypt(string strRSA, string str_PrivateKey)
        {
            try
            {
                byte[] DataToDecrypt = Convert.FromBase64String(strRSA);
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();


                RSA.FromXmlString(str_PrivateKey);
                //byte[] bsPrivatekey = Convert.FromBase64String(str_PrivateKey);
                //RSA.ImportCspBlob(bsPrivatekey);


                byte[] bsdecrypt = RSA.Decrypt(DataToDecrypt, false);


                string strRE = Encoding.UTF8.GetString(bsdecrypt);
                return strRE;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());
                return null;
            }
        }
        #endregion


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值