【C#】RSA加密,无限制长度

RSA加密,无限制长度

    /// <summary>
    /// RSA加密(无长度限制)
    /// </summary>
    public class EncryptHelper
    {
        //通过EncryptHelper.RSAKey方法生成私钥和公钥
        static string privateKey = "xxx";
        static string publicKey = "yyy";

        /// <summary>
        /// 加密
        /// </summary>
        public static string Encrypt(string rawInput)
        {
            return RsaEncrypt(rawInput, publicKey);
        }

        /// <summary>
        /// 解密
        /// </summary>
        public static string Decrypt(string encryptedInput)
        {
            return RsaDecrypt(encryptedInput, privateKey);
        }

        /// <summary>
        /// 生成密钥
        /// </summary>
        public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
        {
            var rsa = new RSACryptoServiceProvider();
            xmlKeys = rsa.ToXmlString(true);
            xmlPublicKey = rsa.ToXmlString(false);
        }

        /// <summary>
        /// 加密
        /// </summary>
        public static string RsaEncrypt(string rawInput, string publicKey)
        {
            if (string.IsNullOrEmpty(rawInput))
            {
                return string.Empty;
            }

            if (string.IsNullOrWhiteSpace(publicKey))
            {
                throw new ArgumentException("Invalid Public Key");
            }

            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var inputBytes = Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流
                rsaProvider.FromXmlString(publicKey);//载入公钥
                int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(inputBytes),
                     outputStream = new MemoryStream())
                {
                    while (true)
                    { //分段加密
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }

                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var encryptedBytes = rsaProvider.Encrypt(temp, false);
                        outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                    }
                    return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输
                }
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        public static string RsaDecrypt(string encryptedInput, string privateKey)
        {
            if (string.IsNullOrEmpty(encryptedInput))
            {
                return string.Empty;
            }

            if (string.IsNullOrWhiteSpace(privateKey))
            {
                throw new ArgumentException("Invalid Private Key");
            }

            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var inputBytes = Convert.FromBase64String(encryptedInput);
                rsaProvider.FromXmlString(privateKey);
                int bufferSize = rsaProvider.KeySize / 8;
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(inputBytes),
                     outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }

                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var rawBytes = rsaProvider.Decrypt(temp, false);
                        outputStream.Write(rawBytes, 0, rawBytes.Length);
                    }
                    return Encoding.UTF8.GetString(outputStream.ToArray());
                }
            }
        }
    }

下面将对象存储为加密文件

/// <summary>
/// LitJson对象转换
/// </summary>
public class JsonHelper
{
    public static T ToObject<T>(string json)
    {
        return JsonMapper.ToObject<T>(json);
    }

    public static string ToJson<T>(T obj)
    {
        return JsonMapper.ToJson(obj);
    }
}

/// <summary>
/// 文件读写
/// </summary>
public class FileHelper
{
    public static void WriteLine(string path, string value)
    {
        //如果不存在文件,则新建文件;如果存在文件,设置为false是不追加文本,重新写入
        StreamWriter sw = new StreamWriter(path, false, Encoding.Unicode);
        sw.WriteLine(value);
        sw.Dispose();
    }

    public static string Read(string path)
    {
        return File.ReadAllText(path);
    }
}

public class Helper
{
    /// <summary>
    /// 将模型以加密方式写入文件
    /// </summary>
    public static void WriteEncrypt<T>(string path, T t)
    {
        var json = JsonHelper.ToJson(t);
        var jsonEncrypt = EncryptHelper.Encrypt(json);
        FileHelper.WriteLine(path, jsonEncrypt);
    }

    /// <summary>
    /// 读取模型
    /// </summary>
    public static T ReadDecrypt<T>(string path)
    {
        var fileValue = FileHelper.Read(path);
        var jsonDecrypt = EncryptHelper.Decrypt(fileValue);
        var model = JsonHelper.ToObject<T>(jsonDecrypt);
        return model;
    }
}

写入和读取

Helper.WriteEncrypt(path, users);//将模型以加密方式写入文件
var userInfos = Helper.ReadDecrypt<List<UserInfo>>(path);//读取模型

 

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页