C# 加密算法RSA

using System;
using System.Security.Cryptography;
using System.Text;

namespace XiaoFeng.Cryptography
{
    /// 
    /// RSA加密解密
    /// version : 1.0.0
    /// 
    public class RSACrypto
    {
        #region 构造器
        /// 
        /// 无参构造器
        /// 
        public RSACrypto()
        {
            this.encoding = Encoding.Default;
        }
        #endregion

        #region 属性
        /// 
        /// 编码
        /// 
        public Encoding encoding { get; set; }
        /// 
        /// 公钥
        /// 
        public string PublicKey { get; set; }
        /// 
        /// 私钥
        /// 
        public string PrivateKey { get; set; }
        /// 
        /// 静态方法
        /// 
        public static RSACrypto RSA { get { return new RSACrypto(); } }
        #endregion

        #region 方法

        #region 产生 密钥
        /// 
        /// RSA产生密钥
        /// 
        /// 私钥
        /// 公钥
        public void CreateKeys(out string PrivateKey, out string PublicKey)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                PrivateKey = rsa.ToXmlString(true);
                PublicKey = rsa.ToXmlString(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region 加密函数
        /// 
        /// 加密函数
        /// 
        /// 公钥
        /// 待加密的字符串
        /// 
        public string Encrypt(string publicKey, string data)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(publicKey);
                var _data = rsa.Encrypt(data.GetBytes(this.encoding), false);
                return Convert.ToBase64String(_data); ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// 
        /// 加密函数
        /// 
        /// 明文
        /// 
        public string Encrypt(string data)
        {
            var encryptBytes = new RSACryptoServiceProvider(new CspParameters()).Encrypt(data.GetBytes(this.encoding), false);
            return Convert.ToBase64String(encryptBytes);
        }
        #endregion

        #region 解密函数        
        /// 
        /// 解密函数
        /// 
        /// 私钥
        /// 待解密的字符串
        /// 
        public string Decrypt(string privateKey, string data)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(privateKey);
                var _data = rsa.Decrypt(Convert.FromBase64String(data.Replace(" ", "+")), false);
                return _data.GetString(this.encoding);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// 
        /// 解密函数
        /// 
        /// 密文
        /// 
        public string Decrypt(string data)
        {
            try
            {
                var DecryptBytes = new RSACryptoServiceProvider(new CspParameters()).Decrypt(Convert.FromBase64String(data), false);
                return DecryptBytes.GetString(this.encoding);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        #endregion

        #endregion
    }
}

你可能感兴趣的:(加密算法)