• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[转][C#]加密解密类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
{
    public static class Crypter
    {
        private static string FDefaultPassword = typeof(Crypter).FullName;

        public static string DefaultPassword
        {
            set
            {
                Crypter.FDefaultPassword = value;
            }
        }

        public static Stream Encrypt(Stream dest, string password)
        {
            ICryptoTransform transform = null;
            using (PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt")))
            {
                transform = new RijndaelManaged
                {
                    Padding = PaddingMode.ISO10126
                }.CreateEncryptor(passwordDeriveBytes.GetBytes(16), passwordDeriveBytes.GetBytes(16));
            }
            dest.Write(new byte[]
            {
                114,
                105,
                106
            }, 0, 3);
            return new CryptoStream(dest, transform, CryptoStreamMode.Write);
        }

        public static Stream Decrypt(Stream source, string password)
        {
            ICryptoTransform transform = null;
            using (PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt")))
            {
                transform = new RijndaelManaged
                {
                    Padding = PaddingMode.ISO10126
                }.CreateDecryptor(passwordDeriveBytes.GetBytes(16), passwordDeriveBytes.GetBytes(16));
            }
            int arg_5C_0 = source.ReadByte();
            int num = source.ReadByte();
            int num2 = source.ReadByte();
            if (arg_5C_0 == 114 && num == 105 && num2 == 106)
            {
                return new CryptoStream(source, transform, CryptoStreamMode.Read);
            }
            source.Position -= 3L;
            return null;
        }

        public static bool IsStreamEncrypted(Stream stream)
        {
            int arg_25_0 = stream.ReadByte();
            int num = stream.ReadByte();
            int num2 = stream.ReadByte();
            stream.Position -= 3L;
            return arg_25_0 == 114 && num == 105 && num2 == 106;
        }

        public static string EncryptString(string data)
        {
            return Crypter.EncryptString(data, Crypter.FDefaultPassword);
        }

        public static string EncryptString(string data, string password)
        {
            if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(password))
            {
                return data;
            }
            string result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (Stream stream = Crypter.Encrypt(memoryStream, password))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(data);
                    stream.Write(bytes, 0, bytes.Length);
                }
                result = "rij" + Convert.ToBase64String(memoryStream.ToArray());
            }
            return result;
        }

        public static string DecryptString(string data)
        {
            return Crypter.DecryptString(data, Crypter.FDefaultPassword);
        }

        public static string DecryptString(string data, string password)
        {
            if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(password) || !data.StartsWith("rij"))
            {
                return data;
            }
            data = data.Substring(3);
            string @string;
            using (Stream stream = Converter.FromString(typeof(Stream), data) as Stream)
            {
                using (Stream stream2 = Crypter.Decrypt(stream, password))
                {
                    byte[] array = new byte[data.Length];
                    int count = stream2.Read(array, 0, array.Length);
                    @string = Encoding.UTF8.GetString(array, 0, count);
                }
            }
            return @string;
        }

        public static string ComputeHash(Stream input)
        {
            byte[] array = new byte[input.Length];
            input.Read(array, 0, array.Length);
            return Crypter.ComputeHash(array);
        }

        public static string ComputeHash(byte[] input)
        {
            return BitConverter.ToString(new Murmur3().ComputeHash(input)).Replace("-", string.Empty);
        }

        public static string ComputeHash(string input)
        {
            return Crypter.ComputeHash(Encoding.UTF8.GetBytes(input));
        }
    }
}

来自:https://github.com/FastReports/FastReport

遇到一串不知道具体编码的字符串,使用以下代码勉强转中文了:

str = str.Replace("9B25", "");
List<byte> buffer = new List<byte>();
for (int i = 0; i < str.Length; i++)
{
    if (i % 2 == 1)
    {
        string s = str.Substring(i - 1, 2);
        buffer.Add(Convert.ToByte(s, 16));
    }
}
Encoding.Default.GetString(buffer.ToArray());

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap