首页 > 代码库 > C#内容加密
C#内容加密
/// <summary> /// 字符串加密 /// </summary> public class DesCrypto { private static readonly byte[] defaultIV; static DesCrypto() { DesCrypto.defaultIV = new byte[8]{ (byte) 18, (byte) 52, (byte) 86, (byte) 120, (byte) 144, (byte) 171, (byte) 205, (byte) 239 }; } /// <summary> /// 加密 /// </summary> /// <param name="encryptString">加密字符串</param> /// <returns></returns> public static string EncryptDES(string encryptString) { return EncryptDES(encryptString, "lncykangji"); } /// <summary> /// 解密 /// </summary> /// <param name="decryptString">解密字符串</param> /// <returns></returns> public static string DecryptDES(string decryptString) { return DecryptDES(decryptString, "lncykangji"); } /// <summary> /// 加密 /// </summary> /// <param name="encryptString">加密字符串</param> /// <param name="encryptKey">密钥</param> /// <returns></returns> private static string EncryptDES(string encryptString, string encryptKey) { try { byte[] bytes1 = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = DesCrypto.defaultIV; byte[] bytes2 = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, cryptoServiceProvider.CreateEncryptor(bytes1, rgbIV), CryptoStreamMode.Write); cryptoStream.Write(bytes2, 0, bytes2.Length); cryptoStream.FlushFinalBlock(); return Convert.ToBase64String(memoryStream.ToArray()); } catch { return encryptString; } } /// <summary> /// 解密 /// </summary> /// <param name="decryptString">加密字串</param> /// <param name="decryptKey">密钥</param> /// <returns></returns> private static string DecryptDES(string decryptString, string decryptKey) { if (decryptString == "") return ""; try { byte[] bytes = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = DesCrypto.defaultIV; byte[] buffer = Convert.FromBase64String(decryptString); DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, cryptoServiceProvider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write); cryptoStream.Write(buffer, 0, buffer.Length); cryptoStream.FlushFinalBlock(); return Encoding.UTF8.GetString(memoryStream.ToArray()); } catch (Exception ex) { throw ex; } }
C#内容加密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。