using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; using System.Diagnostics; namespace QQLogin { public partial class QQLoginForm : Form { public QQLoginForm() { InitializeComponent(); } UserInfo ui; privatevoid button1_Click(object sender, EventArgs e) { //单用户登陆if (ui ==null) { ui =new UserInfo();//如果没有提取出来对象,就创建一个 } if (ui !=null) { ui.Username =this.txtUser.Text.Trim(); ui.Password =this.txtPwd.Text; ui.Type =this.cboType.Text =="正常"?"41" : "40"; if (this.ValidateInput()) {//验证是否输入完全if (string.IsNullOrEmpty(ui.Path)) {//判断是否有QQ路径,如果没有就打开对话框来选择一下 DialogResult dr =this.opfQQ.ShowDialog(); if (dr == DialogResult.OK) { ui.Path = opfQQ.FileName;//将选择的路径赋值给对象this.LoginQQ(ui.Username, ui.Password, ui.Type, ui.Path);//登陆QQ } } else { this.LoginQQ(ui.Username, ui.Password, ui.Type, ui.Path); } } SerializeHelper.SerializeUserInfo(ui);//每次登陆都序列化保存一次 } } privatebool ValidateInput() {//验证是否输入完整if (this.txtUser.Text =="") { this.txtUser.Focus(); returnfalse; } elseif(this.txtPwd.Text=="") { this.txtPwd.Focus(); returnfalse; } returntrue; }
privatevoid LoginQQ(string user,string pwd,string type,string path) {//登陆QQ的命令,通过CMD命令来执行 Process MyProcess =new Process(); //设定程序名 MyProcess.StartInfo.FileName ="cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute =false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput =true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput =true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError =true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow =true; //执行强制结束命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine(path+" /start QQUIN:"+user+" PWDHASH:"+ EncodeHash.pwdHash(pwd) +" /stat:"+type);//直接结束进程ID MyProcess.StandardInput.WriteLine("Exit"); } privatevoid btnExit_Click(object sender, EventArgs e) { Application.Exit(); } privatevoid txtUser_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar <'0'|| e.KeyChar >'9')&&e.KeyChar!=8) {//只能输入数字和退格键 e.Handled =true; } } privatevoid QQLoginForm_Load(object sender, EventArgs e) { LoadInfo();//单用户获取 } privatevoid LoadInfo() {//单用户获取 ui = SerializeHelper.DeserializeUserInfo();//返回获取后对象if (ui !=null) { this.txtUser.Text = ui.Username;//填充文本框this.txtPwd.Text = ui.Password;//填充密码框this.cboType.SelectedIndex = ui.Type =="41"?0 : 1;//选择登陆方式 } else { this.cboType.SelectedIndex =0; } } privatevoid btnConfig_Click(object sender, EventArgs e) { ConfigForm cf =new ConfigForm(); cf.ShowDialog(); LoadInfo(); } } }
ConfigForm.cs 配置窗体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace QQLogin { public partial class ConfigForm : Form { UserInfo ui; public ConfigForm() { InitializeComponent(); } privatevoid txtPath_Click(object sender, EventArgs e) {//点击一次文本框,弹出一次对话框来选择QQ路径 DialogResult dr =this.opfQQ.ShowDialog(); if (dr == DialogResult.OK) { this.txtPath.Text = opfQQ.FileName; } } privatebool ValidateInput() {//验证是否输入完整if (this.txtUser.Text =="") { this.txtUser.Focus(); returnfalse; } elseif (this.txtPwd.Text =="") { this.txtPwd.Focus(); returnfalse; } elseif (this.txtPath.Text =="") { returnfalse; } returntrue; } privatevoid btnCancel_Click(object sender, EventArgs e) { this.Close(); } privatevoid ConfigForm_Load(object sender, EventArgs e) { LoadInfo(); } privatevoid btnSave_Click(object sender, EventArgs e) { ui =new UserInfo(); ui.Username =this.txtUser.Text.Trim(); ui.Password =this.txtPwd.Text; ui.Type =this.cboType.Text =="正常"?"41" : "40"; ui.Path =this.txtPath.Text; if (this.ValidateInput()) { SerializeHelper.SerializeUserInfo(ui); this.Close(); } } privatevoid LoadInfo() { ui = SerializeHelper.DeserializeUserInfo(); if (ui !=null) { this.txtUser.Text = ui.Username; this.txtPwd.Text = ui.Password; this.cboType.SelectedIndex = ui.Type =="41"?0 : 1; this.txtPath.Text = ui.Path; } else { this.cboType.SelectedIndex =0; } } } }
EncodeHash.cs 加密类
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace QQLogin { /**////<summary>/// 编码加密类 ///</summary>class EncodeHash { /**////<summary>/// 通过MD5加密后返回加密后的BASE64密码 ///</summary>///<param name="pwd">要加密的内容</param>///<returns>通过MD5加密后返回加密后的BASE64密码</returns>publicstaticstring pwdHash(string pwd) {//使用MD5的16位加密后再转换成BASE64 System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();//实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pwd)); return Convert.ToBase64String(s); } /**////<summary>/// 返回MD5的32位加密后的密码 ///</summary>///<param name="inputString">要加密的内容</param>///<returns>加密后的结果</returns>publicstaticstring StringToMD5Hash(string inputString) { MD5CryptoServiceProvider md5 =new MD5CryptoServiceProvider(); byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString)); StringBuilder sb =new StringBuilder(); for (int i =0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString(); } /**////<summary>/// Base64编码 ///</summary>///<param name="code"></param>///<returns></returns>publicstaticstring EncodeBase64(string code) { string encode =""; byte[] bytes = System.Text.Encoding.Default.GetBytes(code); try { encode = Convert.ToBase64String(bytes); } catch { encode = code; } return encode; } /**////<summary>/// Base64解码 ///</summary>///<param name="code_type"></param>///<param name="code"></param>///<returns></returns>publicstaticstring DecodeBase64(string code_type, string code) { string decode =""; byte[] bytes = Convert.FromBase64String(code); try { decode = Encoding.GetEncoding(code_type).GetString(bytes); } catch { decode = code; } return decode; } } }
SerializeHelper.cs 序列化类,打算做多用户登录器呢,就预留了两个用来序列化集合的方法
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Runtime.Serialization.Formatters.Binary;//序列化using System.IO; namespace QQLogin { /**////<summary>/// 序列化帮助类 ///</summary>class SerializeHelper { /**////<summary>/// 使用反序列化方式读取信息 ///</summary>///<returns></returns>publicstatic UserInfo DeserializeUserInfo() {//将用户信息使用反序列化方式提取 UserInfo ui =null; try { FileStream fileStream =null; fileStream =new FileStream(Application.StartupPath +"\UserInfo.bin", FileMode.Open); BinaryFormatter bf =new BinaryFormatter(); ui = (UserInfo)bf.Deserialize(fileStream); fileStream.Close(); } catch (Exception ex) { } return ui; } /**////<summary>/// 序列化UserInfo对象 ///</summary>///<param name="ui">传入要序列化的UserInfo对象</param>publicstaticvoid SerializeUserInfo(UserInfo ui) {//将用户信息使用序列化方式保存try { FileStream fileStream =null; fileStream =new FileStream(Application.StartupPath +"\UserInfo.bin", FileMode.Create); BinaryFormatter bf =new BinaryFormatter(); bf.Serialize(fileStream, ui); fileStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /**////<summary>/// 反序列化UserInfo泛型集合对象 ///</summary>///<returns></returns>publicstatic List<UserInfo> DeserializeList() {//将用户信息使用反序列化方式提取 List<UserInfo> lst =null; FileStream fileStream =null; try { fileStream =new FileStream(Application.StartupPath +"\UserInfo.bin", FileMode.Open); BinaryFormatter bf =new BinaryFormatter(); lst = (List<UserInfo>)bf.Deserialize(fileStream); fileStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return lst; } /**////<summary>/// 序列化UserInfo泛型集合对象 ///</summary>///<param name="ui">传入要序列化的List<UserInfo>对象</param>publicstaticvoid SerializeList(List<UserInfo> lst) {//将用户信息使用序列化方式保存 FileStream fileStream =null; try { fileStream =new FileStream(Application.StartupPath +"\UserInfo.bin", FileMode.Create); BinaryFormatter bf =new BinaryFormatter(); bf.Serialize(fileStream, lst); fileStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
非诚勿扰