public static object LoadPasswordPure(string filename) //序列化读取文件 { PassWordStruct aaa = new PassWordStruct(9); aaa.password = _LoadPasswordPure(filename); return(aaa); }
private void pswreadlocal(string username) { PureFlag = checkBox5.Checked; OpenFileDialog file = new OpenFileDialog(); if (PureFlag) { file.InitialDirectory = OriPath + "Users\\" + username + "\\bak"; } else { file.InitialDirectory = OriPath + "Users\\" + username + "\\library"; } //label5.Text = file.InitialDirectory; if (file.ShowDialog() == DialogResult.OK) { string read = file.InitialDirectory + "\\" + file.SafeFileName; PassWordStruct aaa = new PassWordStruct(); try { if (PureFlag) { aaa = (PassWordStruct)aes_logic.LoadPasswordPure(read); } else { aaa = (PassWordStruct)aes_logic.LoadPassword(read); } AES_KEY = aes_logic.EncryptWithMD5(textBox9.Text); textBox1.Text = aes_logic.AesDecrypt(aaa.password, AES_KEY); textBox6.Text = aaa.name; textBox3.Text = System.IO.Path.GetFileNameWithoutExtension(read); Clipboard.SetText(textBox1.Text); } catch (Exception) { MessageBox.Show("密钥错误!!"); } } }
private void savesingle(string info, string name, string password) { string SavePath = OriPath + "Users\\" + textBox10.Text + "\\library\\" + info + ".pwd"; string SavePath2 = OriPath + "Users\\" + textBox10.Text + "\\bak\\" + info + ".bak"; //string aespassword = aes_logic.AesEncrypt(textBox1.Text, AES_KEY); PassWordStruct aaa = new PassWordStruct(); aaa.NO = 10; aaa.name = name; aaa.password = password; aaa.info = info; aes_logic.SavePassword(aaa, SavePath, false); aes_logic.SavePasswordPure(aaa, SavePath2, false); }
private void button5_Click(object sender, EventArgs e) { OnLoad(OriPath + "Users\\" + textBox10.Text + "\\library"); //AES_KEY = textBox4.Text; //AES_KEY_2 = textBox5.Text; int k = 0; foreach (string item in pswPathList) { PassWordStruct aaa = new PassWordStruct(); string buffcode = ""; try { /* * aaa.NO = 10; * aaa.name = System.IO.Path.GetFileName(item); ; * aaa.password = "******"; * aes_logic.SavePassword(aaa, item); */ //反序列化读结构体 aaa = (PassWordStruct)aes_logic.LoadPassword(item); /* * //使用第一个密钥解密 * buffcode = aes_logic.AesDecrypt(aaa.password, AES_KEY); * //换第二个密钥加密 * string aespassword = aes_logic.AesEncrypt(buffcode, AES_KEY_2); * //序列化存入结构体 * aaa.password = aespassword; */ aaa.info = System.IO.Path.GetFileNameWithoutExtension(item); //存入对应位置 aes_logic.SavePassword(aaa, item, false); aes_logic.SavePasswordPure(aaa, bakPathList[k], false); k++; } catch (Exception) { MessageBox.Show("密钥错误!!!"); } } }
private void pswsavelocal(string username) { //string SavePath = OriPath + "library\\" + textBox3.Text + ".pwd"; //string SavePath2 = OriPath + "bak\\" + textBox3.Text + ".bak"; string SavePath = OriPath + "Users\\" + username + "\\library\\" + textBox3.Text + ".pwd"; string SavePath2 = OriPath + "Users\\" + username + "\\bak\\" + textBox3.Text + ".bak"; if (System.IO.File.Exists(SavePath)) { //这里可以改成跳出是否覆盖 DialogResult dr = MessageBox.Show("该名称密码已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { //MessageBox.Show("覆盖后原密码不可找回,是否确认覆盖?", "警告"); } else if (dr == DialogResult.No) { return; } else { return; } } AES_KEY_2 = aes_logic.EncryptWithMD5(textBox9.Text); string aespassword = aes_logic.AesEncrypt(textBox1.Text, AES_KEY_2); PassWordStruct aaa = new PassWordStruct(); aaa.NO = 10; aaa.name = textBox6.Text; aaa.password = aespassword; aaa.info = textBox3.Text; aes_logic.SavePassword(aaa, SavePath, true); aes_logic.SavePasswordPure(aaa, SavePath2, true); }
private byte[][] UploadPSWbak() { byte[][] backmsgs = new byte[4][]; backmsgs[0] = new byte[] { 0x00 }; OnLoad(OriPath + "Users\\" + textBox10.Text + "\\library"); //AES_KEY = textBox4.Text; //AES_KEY_2 = textBox5.Text; string[] buffname = new string[pswPathList.Count()]; string[] buffpsw = new string[pswPathList.Count()]; string[] buffinfo = new string[pswPathList.Count()]; int i = 0; foreach (string item in pswPathList) { PassWordStruct aaa = new PassWordStruct(); string buffcode = ""; try { /* * aaa.NO = 10; * aaa.name = System.IO.Path.GetFileName(item); ; * aaa.password = "******"; * aes_logic.SavePassword(aaa, item); */ //反序列化读结构体 aaa = (PassWordStruct)aes_logic.LoadPassword(item); //这里测试一下buffname[i] = aaa.name; buffname[i] = aaa.name; buffpsw[i] = aaa.password; buffinfo[i] = System.IO.Path.GetFileNameWithoutExtension(item); } catch (Exception) { MessageBox.Show("密钥错误!!!"); } i++; } byte[] msgb = ObjectToBytes(buffname); byte[] msgc = ObjectToBytes(buffpsw); byte[] msgd = ObjectToBytes(buffinfo); string blen = (msgb.Length).ToString(); string clen = (msgc.Length).ToString(); string dlen = (msgd.Length).ToString(); //第一位功能待定(可以定为只新增或完全同步模式),后三位为数据长度 string[] userinfo = { "0", blen, clen, dlen }; byte[] msga = ObjectToBytes(userinfo); backmsgs[0] = msga; backmsgs[1] = msgb; backmsgs[2] = msgc; backmsgs[3] = msgd; return(backmsgs); }
private void pswsync() { try { Int32 port = Convert.ToInt32(textBox7.Text); string server = textBox8.Text; //表示c# 客户端 接下来数据长度是256byte string[] messages1 = { "001", "0256" }; string Username = textBox10.Text; string key = aes_logic.EncryptWithMD5(textBox9.Text); string Superadmin = textBox11.Text; //代表新建用户 用户名 密钥 超级管理员 string[] messages = { "1", Username, key, Superadmin }; TcpClient client = new TcpClient(server, port); //设置接收超时 client.ReceiveTimeout = 5000; NetworkStream stream = client.GetStream(); Byte[] data1 = aes_logic.StringArrToBytes(messages1); stream.Write(data1, 0, data1.Length); Byte[] data = ObjectToBytes(messages); stream.Write(data, 0, data.Length); // Buffer to store the response bytes. data = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; String responseData2 = String.Empty; Thread.Sleep(2000); // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); string[] receive1 = (string[])BytesToObject(data); int error = Convert.ToInt32(receive1[0]); if (error == 1) { int blen = Convert.ToInt32(receive1[1]); int clen = Convert.ToInt32(receive1[2]); int dlen = Convert.ToInt32(receive1[3]); Byte[] data2 = new Byte[blen]; Byte[] data3 = new Byte[clen]; Byte[] data4 = new Byte[dlen]; Int32 bytes2 = stream.Read(data2, 0, data2.Length); Int32 bytes3 = stream.Read(data3, 0, data3.Length); Int32 bytes4 = stream.Read(data4, 0, data4.Length); //responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); string[] receive2 = (string[])BytesToObject(data2); string[] receive3 = (string[])BytesToObject(data3); string[] receive4 = (string[])BytesToObject(data4); int ii = 0; curpasswordList = new List <PassWordStruct>(); foreach (var name in receive2) { PassWordStruct buffer = new PassWordStruct(1); buffer.name = name; buffer.password = receive3[ii]; buffer.info = receive4[ii]; curpasswordList.Add(buffer); ii++; } MessageBox.Show("成功同步所有密码"); } else { MessageBox.Show("下载失败"); } // Close everything. stream.Close(); client.Close(); } catch (ArgumentNullException ex) { Console.WriteLine("ArgumentNullException: {0}", ex); } catch (SocketException ex) { Console.WriteLine("SocketException: {0}", ex); } catch (Exception ex) { Console.WriteLine("TimeoutException: {0}", ex); } }
public static void SavePasswordPure(object password, string filename, bool msgshow) //序列化保存 { PassWordStruct aaa = (PassWordStruct)password; _SavePasswordPure(aaa.password, filename, msgshow); }