---恢复内容开始---
static void Main(string[] args)
{
string path = @"C:\Users\EDZ\Desktop\Test1\userData11.luxin";//文件的路径
#region MyRegion
if (!File.Exists(path))
{
Console.WriteLine("当前不包含文件");
File.Create(path);
}
//将文件加载到文件流中
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
List<string> allStr = new List<string>();
string str = string.Empty;
while (true)
{
str = sr.ReadLine();
if (!string.IsNullOrEmpty(str))
{
Console.WriteLine(str + "\r\n");
allStr.Add(str);
}
else
{
sr.Close();
fs.Close();
break;
}
}
//解析文本
foreach (var item in allStr)
{
string[] strSpl = item.Split(‘|‘);
int strSplLen = strSpl.Length;
for (int i = 0; i < strSplLen; i++)
{
if (i==0)
{
Console.WriteLine("账号"+strSpl[0]);
}
else if (i == 1)
{
Console.WriteLine("密码" + strSpl[1]);
}
}
}
#endregion
if (!File.Exists(path))
{
Console.WriteLine("文件不包含");
}
FileStream fs1 = new FileStream(path,FileMode.Append);
StreamWriter sw = new StreamWriter(fs1);
for (int i = 0; i < 2; 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 = System.Text.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();
}
}
---恢复内容结束---