C # console to create a file to delete files account password to encrypt documents

--- --- restore content begins

static void Main(string[] args)
{

string path = @:; "C \ Users \ EDZ \ Desktop \ Test1 \ userData11.luxin" // file path
#region myregion
(! the File.Exists (path)) IF
{
Console.WriteLine ( "file does not currently contain" );
File.Create (path);
}
// load the file into the file stream
the fileStream new new FS = the fileStream (path, FileMode.Open);
the StreamReader the StreamReader new new SR = (FS);
List <String> = allStr new new List < String> ();
String STR = string.Empty;
the while (to true)
{
STR = sr.ReadLine ();
IF (String.IsNullOrEmpty (STR)!)
{
Console.WriteLine (STR + "\ R & lt \ n-");
allStr.Add (STR);
}
the else
{
sr.Close ();
fs.Close ();
BREAK;
}
}
// parse text
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 ( "write completion");
// 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 System.Text.Encoding.Default.GetBytes (strSource);
// establish secret encryption objective key and the offset 
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 MemoryStream stream file encryption 
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();
}
}

--- end --- restore content

Guess you like

Origin www.cnblogs.com/chenquanxi213356/p/11257869.html