数据的加密和解密(MD5)

mac2022-06-30  19

class Program { static void Main(string[] args) {

   string path = @"C:\Users\EDZ\Desktop\Test1\userData11.luxin";   if (!File.Exists(path))   {      Console.WriteLine("文件不包含");   }   FileStream fs = new FileStream(path,FileMode.Append);   StreamWriter sw = new StreamWriter(fs);   for (int i = 0; i < 3; i++)   {      Console.WriteLine("请用户输入账号:");      string username = Console.ReadLine();      Console.WriteLine("请用户输入密码:");      string password = Console.ReadLine();     string writeCon = username +"|"+ password;     sw.WriteLine(Md5Encrypt(writeCon));  }  sw.Close();  fs.Close(); Console.WriteLine("写入完成"); //读取 if (File.Exists(path))  {       FileStream fs_ = new FileStream(path,FileMode.Open);      StreamReader sr_ = new StreamReader(fs_);      string str_ = string.Empty;      while (true)     {        str_ = sr_.ReadLine();        if (string.IsNullOrEmpty(str_))        {            break;       }       else        {     //解析     string newStr = Md5Decrypt(str_);     string[] newStrSpl = newStr.Split('|');     foreach (var item in newStrSpl)     {     Console.WriteLine(item);     }   } }

}

Console.ReadKey(); }

//加密方法 static string Md5Encrypt(string strSource) { //把字符串放到byte数组中 byte[] bytIn = Encoding.Default.GetBytes(strSource); //建立加密对象的密钥和偏移量 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 //实例DES加密类 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); //实例MemoryStream流加密密文件 System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); string strOut = System.Convert.ToBase64String(ms.ToArray()); return strOut;

}

//解密方法

static public string Md5Decrypt(string Source) { //将解密字符串转换成字节数组 byte[] bytIn = System.Convert.FromBase64String(Source); //给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; //实例流进行解密 System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader strd = new StreamReader(cs, Encoding.Default); return strd.ReadToEnd(); } }

转载于:https://www.cnblogs.com/Damon-3707/p/11257871.html

相关资源:md5解密工具Md5Decrypt
最新回复(0)