恩蓝号

encrypt加密使用教程,encrypt后缀解密

    在程序中调用这个类Encryption:

using System;using System.Collections.Generic;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;namespace DBTools{ public class Encryption { /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); } /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = Text.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } // MD5 32 位 public static String Encrypt32(String convertString) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(convertString); bytes = md5.ComputeHash(bytes); md5.Clear(); string ret = ""; for (int i = 0; i < bytes.Length; i++) { ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0'); } return ret.PadLeft(32, '0').ToLower(); } // MD5 16 位 public static string Encrypt16(string convertString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(convertString)), 4, 8); t2 = t2.Replace("-", ""); return t2.ToLower(); } }}    cs:

编码时:string Pwd = DBTools.Encryption.Encrypt(LoginPwd, "Master"); //LoginPwd--要加密的数据。也可以将二进制值传递给此函数。此参数区分大小写,即使是在不区分大小写的数据库中也是如此。 //Master--用来对 LoginPwd 进行加密的加密密钥。解密时必须使用同一密钥才能获得原始值。此参数区分大小写,即使是在不区分大小写的数据库中也是如此。与大多数口令一样,最好选择无法被轻易猜到的密钥值。建议选择满足以下条件的密钥值:长度至少为 16 个字符,混合使用大小写并包含数字、字母和特殊字符。每次要对数据进行解密时,都需要使用此密钥。解码时:string Pwd = DBTools.Encryption.Decrypt(LoginPwd, "Master"); //跟编码讲解一致


免责声明:文章源自网络,版权归原作者所有,如有侵犯联系删除。

当前位置:首页 > 编程知识 » 2023-03-03 12:14:46

猜你喜欢


红米note5参数,红米note5刷机教程

红米Note5官方系统已经很久没有更新了,内测和开发版也都撤包了,最新稳定版也越来越渺茫,为了让这款老机再最后焕发一下光彩就只能靠自已动手了...

如何安装typora,typora使用教程

大家在平时的学习生活中必不可少地需要记笔记,或者是手机上或者是pad上,单纯地用记事本txt会很不方便,所以这时候就需要一款很方便而且好用的...