Data encryption and decryption (MD5)

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("写入完成");
// Read
 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);
    }
  }
}

}

The Console.ReadKey ();
}
static Md5Encrypt String (String strSource) {
// the string into a byte array
byte [] = bytIn Encoding.Default.GetBytes (strSource);
// build encryption key and the offset of the object the amount of
byte [] iv = {102, 16, 93, 156, 78, 4, 218, 32}; // define offset
byte [] key = {55, 103, 246, 79, 36, 99, 167, 3}; // define key
// class instance DES encrypting
the DESCryptoServiceProvider mobjCryptoService the DESCryptoServiceProvider new new = ();
mobjCryptoService.Key = IV;
mobjCryptoService.IV = key;
of the ICryptoTransform encrypto mobjCryptoService.CreateEncryptor = ();
// example encryption stream MemoryStream file
System.IO.MemoryStream new new System.IO.MemoryStream MS = ();
the CryptoStream new new CS = the CryptoStream (MS, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
string strOut = System.Convert.ToBase64String(ms.ToArray());
return strOut;

}

Md5Decrypt String public static (the Source String)
{
// converts the decrypted string into a byte array
byte [] = bytIn System.Convert.FromBase64String (the Source);
// offset given key and decryption key, and key must be identical to the offset amount and offset encrypted
byte [] iv = {102, 16, 93, 156, 78, 4, 218, 32}; // define offset
byte [] key = { 55, 103, 246, 79, 36, 99, 167, 3}; // define key
the DESCryptoServiceProvider mobjCryptoService the DESCryptoServiceProvider new new = ();
mobjCryptoService.Key = IV;
mobjCryptoService.IV = key;
// examples of decrypting a stream
System. MS = new new System.IO.MemoryStream IO.MemoryStream (bytIn, 0, bytIn.Length);
of the ICryptoTransform encrypto mobjCryptoService.CreateDecryptor = ();
the CryptoStream new new CS = the CryptoStream (MS, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
}

Guess you like

Origin www.cnblogs.com/Damon-3707/p/11257871.html