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

RSA加密,无限制长度

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

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

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

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

        /// 
        /// 加密
        /// 
        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());//转化为字节流方便传输
                }
            }
        }

        /// 
        /// 解密
        /// 
        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());
                }
            }
        }
    }

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

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

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

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

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

写入和读取

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

 

 

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