程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現的QQ登錄器

C#實現的QQ登錄器

編輯:關於C#

前幾天看到一篇文章說通過DOS命令就可以登陸QQ,在運行裡試了一下,真的可以

代碼如下:

Code

[copy to clipboard]

CODE:

QQ路徑 /start QQUIN:QQ號 PWDHASH:經過MD5和BASE64雙充加密的QQ密碼 /stat:登陸類型

今天就想做個QQ登錄器試一下,信息保存嘗試使用了序列化,發現功能真的太強大了,剛才整理了一下,現在完工,裡面做了大量的注釋,放出代碼,文章最下面有打包的下載:

QQLoginForm.cs窗體

Code

[copy to clipboard]

CODE:

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;
    private void 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);//每次登陸都序列化保存一次
      }
    }
    private bool ValidateInput()
    {//驗證是否輸入完整
      if (this.txtUser.Text == "")
      {
        this.txtUser.Focus();
        return false;
      }
      else if(this.txtPwd.Text=="")
      {
        this.txtPwd.Focus();
        return false;
      }
      return true;
    }
    private void 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");
    }
    private void btnExit_Click(object sender, EventArgs e)
    {
      Application.Exit();
    }
    private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
    {
      if ((e.KeyChar < '0' || e.KeyChar > '9')&&e.KeyChar!=8)
      {//只能輸入數字和退格鍵
        e.Handled = true;
      }
    }
    private void QQLoginForm_Load(object sender, EventArgs e)
    {
      LoadInfo();//單用戶獲取
    }
    private void 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;
      }
    }
    private void btnConfig_Click(object sender, EventArgs e)
    {
      ConfigForm cf = new ConfigForm();
      cf.ShowDialog();
      LoadInfo();
    }
  }
}

ConfigForm.cs 配置窗體

Code

[copy to clipboard]

CODE:

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();
    }
    private void txtPath_Click(object sender, EventArgs e)
    {//點擊一次文本框,彈出一次對話框來選擇QQ路徑
      DialogResult dr = this.opfQQ.ShowDialog();
      if (dr == DialogResult.OK)
      {
        this.txtPath.Text = opfQQ.FileName;
      }
    }
    private bool ValidateInput()
    {//驗證是否輸入完整
      if (this.txtUser.Text == "")
      {
        this.txtUser.Focus();
        return false;
      }
      else if (this.txtPwd.Text == "")
      {
        this.txtPwd.Focus();
        return false;
      }
      else if (this.txtPath.Text == "")
      {
        return false;
      }
      return true;
    }
    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    private void ConfigForm_Load(object sender, EventArgs e)
    {
      LoadInfo();
    }
    private void 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();
      }
    }
    private void 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 加密類

Code

[copy to clipboard]

CODE:

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>
    public static string 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>
    public static string 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);
      }
      return sb.ToString();
    }
    /**//// <summary>
    /// Base64編碼
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static string 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>
    public static string 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 序列化類,打算做多用戶登錄器呢,就預留了兩個用來序列化集合的方法

Code

[copy to clipboard]

CODE:

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>
    public static 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>
    public static void 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>
    public static 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>
    public static void 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);
      }
    }
  }
}

UserInfo.cs用戶信息類

Code

[copy to clipboard]

CODE:

using System;
using System.Collections.Generic;
using System.Text;
namespace QQLogin
{
  /**//// <summary>
  /// 用戶信息類
  /// </summary>
  [Serializable]
  class UserInfo
  {
    private string username;
    public string Username
    {
      get { return username; }
      set { username = value; }
    }
    private string password;
    public string Password
    {
      get { return password; }
      set { password = value; }
    }
    private string type;
    public string Type
    {
      get { return type; }
      set { type = value; }
    }
    private string path;
    public string Path
    {
      get { return path; }
      set { path = value; }
    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved