首页 / C# / [转][C#]加密解密类
[转][C#]加密解密类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[转][C#]加密解密类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3066字,纯文字阅读大概需要5分钟。
内容图文
![[转][C#]加密解密类](/upload/InfoBanner/zyjiaocheng/844/9b6cdc85c88b4364b579ba593f7f0790.jpg)
{ 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
内容总结
以上是互联网集市为您收集整理的[转][C#]加密解密类全部内容,希望文章能够帮你解决[转][C#]加密解密类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。