基于C#實現(xiàn)rar文件密碼破解工具
本文主要介紹了一個C#編寫的RAR壓縮文件密碼恢復(fù)工具,該程序通過加載密碼字典文件,逐個嘗試密碼來破解受密碼保護(hù)的RAR文件。
主要功能包括
1.選擇RAR文件和密碼字典
2.顯示恢復(fù)進(jìn)度
3.統(tǒng)計嘗試密碼數(shù)量和速度
4.計算剩余時間
本工具采用后臺線程運(yùn)行密碼破解過程,可以避免UI凍結(jié),并提供取消操作功能。
效果圖
程序代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Windows.Forms; using SharpCompress.Archives; using SharpCompress.Archives.Rar; using SharpCompress.Common; using SharpCompress.Readers; namespace RarPasswordRecovery { public partial class MainForm : Form { private BackgroundWorker worker; private long totalPasswords; private long testedPasswords; private Stopwatch stopwatch; private List<string> passwordList; private bool passwordFound; private string foundPassword; private bool isRunning; public MainForm() { InitializeComponent(); InitializeBackgroundWorker(); SetupUI(); } private void InitializeBackgroundWorker() { worker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; worker.DoWork += Worker_DoWork; worker.ProgressChanged += Worker_ProgressChanged; worker.RunWorkerCompleted += Worker_RunWorkerCompleted; } private void SetupUI() { // 設(shè)置窗體 this.Text = "RAR密碼恢復(fù)工具"; this.Size = new Size(800, 500); this.MinimumSize = new Size(600, 400); this.BackColor = Color.FromArgb(45, 45, 65); this.ForeColor = Color.White; this.FormBorderStyle = FormBorderStyle.FixedSingle; this.StartPosition = FormStartPosition.CenterScreen; // 創(chuàng)建控件 CreateControls(); } private void CreateControls() { // 標(biāo)題標(biāo)簽 Label titleLabel = new Label { Text = "RAR壓縮文件密碼恢復(fù)", Font = new Font("Segoe UI", 16, FontStyle.Bold), AutoSize = true, Location = new Point(20, 15), ForeColor = Color.LightSkyBlue }; this.Controls.Add(titleLabel); // RAR文件選擇 Label lblRarFile = new Label { Text = "RAR文件路徑:", Location = new Point(20, 60), AutoSize = true }; this.Controls.Add(lblRarFile); TextBox txtRarFilePath = new TextBox { Location = new Point(120, 57), Size = new Size(500, 25), Name = "txtRarFilePath", ReadOnly = true }; this.Controls.Add(txtRarFilePath); Button btnBrowseRar = new Button { Text = "瀏覽...", Location = new Point(630, 55), Size = new Size(80, 25), Name = "btnBrowseRar", BackColor = Color.FromArgb(70, 70, 90), ForeColor = Color.White, FlatStyle = FlatStyle.Flat }; btnBrowseRar.FlatAppearance.BorderSize = 0; btnBrowseRar.Click += BtnBrowseRar_Click; this.Controls.Add(btnBrowseRar); // 字典文件選擇 Label lblDictionary = new Label { Text = "密碼字典文件:", Location = new Point(20, 100), AutoSize = true }; this.Controls.Add(lblDictionary); TextBox txtDictionaryPath = new TextBox { Location = new Point(120, 97), Size = new Size(500, 25), Name = "txtDictionaryPath", ReadOnly = true }; this.Controls.Add(txtDictionaryPath); Button btnBrowseDict = new Button { Text = "瀏覽...", Location = new Point(630, 95), Size = new Size(80, 25), Name = "btnBrowseDict", BackColor = Color.FromArgb(70, 70, 90), ForeColor = Color.White, FlatStyle = FlatStyle.Flat }; btnBrowseDict.FlatAppearance.BorderSize = 0; btnBrowseDict.Click += BtnBrowseDict_Click; this.Controls.Add(btnBrowseDict); // 進(jìn)度條 ProgressBar progressBar = new ProgressBar { Location = new Point(20, 150), Size = new Size(690, 25), Name = "progressBar", Style = ProgressBarStyle.Continuous }; this.Controls.Add(progressBar); // 狀態(tài)標(biāo)簽 Label lblStatus = new Label { Location = new Point(20, 185), Size = new Size(690, 20), Name = "lblStatus", Text = "就緒", TextAlign = ContentAlignment.MiddleLeft }; this.Controls.Add(lblStatus); // 統(tǒng)計信息 Label lblTotal = new Label { Location = new Point(20, 215), Size = new Size(200, 20), Name = "lblTotalPasswords", Text = "總密碼數(shù): 0" }; this.Controls.Add(lblTotal); Label lblTested = new Label { Location = new Point(230, 215), Size = new Size(200, 20), Name = "lblTested", Text = "已嘗試: 0" }; this.Controls.Add(lblTested); Label lblSpeed = new Label { Location = new Point(440, 215), Size = new Size(200, 20), Name = "lblSpeed", Text = "速度: 0 p/s" }; this.Controls.Add(lblSpeed); // 時間信息 Label lblElapsed = new Label { Location = new Point(20, 245), Size = new Size(200, 20), Name = "lblElapsed", Text = "已用時間: 00:00:00" }; this.Controls.Add(lblElapsed); Label lblRemaining = new Label { Location = new Point(230, 245), Size = new Size(200, 20), Name = "lblRemaining", Text = "剩余時間: --:--:--" }; this.Controls.Add(lblRemaining); // 當(dāng)前嘗試密碼 Label lblCurrent = new Label { Text = "當(dāng)前嘗試密碼:", Location = new Point(20, 280), AutoSize = true }; this.Controls.Add(lblCurrent); TextBox txtCurrentPassword = new TextBox { Location = new Point(120, 277), Size = new Size(500, 25), Name = "txtCurrentPassword", ReadOnly = true, Font = new Font("Consolas", 10), BackColor = Color.FromArgb(30, 30, 40), ForeColor = Color.LightGreen }; this.Controls.Add(txtCurrentPassword); // 按鈕 Button btnStart = new Button { Text = "開始恢復(fù)", Location = new Point(200, 320), Size = new Size(120, 35), Name = "btnStart", BackColor = Color.SeaGreen, FlatStyle = FlatStyle.Flat, Font = new Font("Segoe UI", 10, FontStyle.Bold) }; btnStart.FlatAppearance.BorderSize = 0; btnStart.Click += BtnStart_Click; this.Controls.Add(btnStart); Button btnCancel = new Button { Text = "取消", Location = new Point(340, 320), Size = new Size(120, 35), Name = "btnCancel", Enabled = false, BackColor = Color.IndianRed, FlatStyle = FlatStyle.Flat, Font = new Font("Segoe UI", 10) }; btnCancel.FlatAppearance.BorderSize = 0; btnCancel.Click += BtnCancel_Click; this.Controls.Add(btnCancel); // 法律聲明 LinkLabel linkLegal = new LinkLabel { Text = "使用條款和道德聲明", Location = new Point(20, 370), AutoSize = true, LinkColor = Color.LightSkyBlue, ActiveLinkColor = Color.Cyan }; linkLegal.LinkClicked += LinkLegal_LinkClicked; this.Controls.Add(linkLegal); // 狀態(tài)圖標(biāo) PictureBox statusIcon = new PictureBox { Location = new Point(650, 320), Size = new Size(60, 60), SizeMode = PictureBoxSizeMode.Zoom }; // 注意:在實際項目中,您需要添加自己的圖標(biāo) statusIcon.BackColor = Color.Transparent; this.Controls.Add(statusIcon); } private void BtnBrowseRar_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "RAR files (*.rar)|*.rar|All files (*.*)|*.*"; openFileDialog.Title = "選擇RAR文件"; if (openFileDialog.ShowDialog() == DialogResult.OK) { GetTextBox("txtRarFilePath").Text = openFileDialog.FileName; } } } private void BtnBrowseDict_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.Title = "選擇密碼字典文件"; if (openFileDialog.ShowDialog() == DialogResult.OK) { GetTextBox("txtDictionaryPath").Text = openFileDialog.FileName; } } } private void BtnStart_Click(object sender, EventArgs e) { TextBox txtRarFilePath = GetTextBox("txtRarFilePath"); TextBox txtDictionaryPath = GetTextBox("txtDictionaryPath"); if (string.IsNullOrWhiteSpace(txtRarFilePath.Text)) { MessageBox.Show("請選擇RAR文件", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrWhiteSpace(txtDictionaryPath.Text)) { MessageBox.Show("請選擇密碼字典文件", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!File.Exists(txtRarFilePath.Text)) { MessageBox.Show("選擇的RAR文件不存在", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!File.Exists(txtDictionaryPath.Text)) { MessageBox.Show("選擇的字典文件不存在", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 重置UI passwordFound = false; foundPassword = null; testedPasswords = 0; isRunning = true; GetProgressBar().Value = 0; GetLabel("lblStatus").Text = "加載密碼字典..."; GetLabel("lblTested").Text = "已嘗試: 0"; GetLabel("lblSpeed").Text = "速度: 0 p/s"; GetLabel("lblElapsed").Text = "已用時間: 00:00:00"; GetLabel("lblRemaining").Text = "剩余時間: --:--:--"; GetTextBox("txtCurrentPassword").Text = ""; GetButton("btnStart").Enabled = false; GetButton("btnCancel").Enabled = true; try { // 讀取字典文件 passwordList = File.ReadAllLines(txtDictionaryPath.Text) .Where(p => !string.IsNullOrWhiteSpace(p)) .Distinct() .ToList(); totalPasswords = passwordList.Count; if (totalPasswords == 0) { MessageBox.Show("字典文件為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); ResetUI(); return; } GetLabel("lblTotalPasswords").Text = $"總密碼數(shù): {totalPasswords:N0}"; stopwatch = Stopwatch.StartNew(); worker.RunWorkerAsync(); } catch (Exception ex) { MessageBox.Show($"讀取字典文件錯誤: {ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); ResetUI(); } } private void Worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; passwordFound = false; foundPassword = null; string rarFilePath = GetTextBox("txtRarFilePath").Text; try { // 嘗試每個密碼 for (int i = 0; i < passwordList.Count; i++) { if (worker.CancellationPending) { e.Cancel = true; return; } string password = passwordList[i]; testedPasswords++; try { // 修復(fù):使用ReaderOptions傳遞密碼 var options = new ReaderOptions { Password = password, LookForHeader = true, DisableCheckIncomplete = true }; // 嘗試打開壓縮文件 using (var archive = RarArchive.Open(rarFilePath, options)) { // 嘗試訪問第一個文件 var entry = archive.Entries.FirstOrDefault(); if (entry != null) { // 嘗試讀取少量數(shù)據(jù) using (var stream = entry.OpenEntryStream()) { byte[] buffer = new byte[10]; stream.Read(buffer, 0, buffer.Length); // 如果成功讀取數(shù)據(jù),密碼正確 passwordFound = true; foundPassword = password; return; } } } } catch (SharpCompress.Common.CryptographicException) { // 密碼錯誤,繼續(xù)嘗試 } catch (InvalidFormatException) { // 密碼錯誤或文件格式問題 } catch (Exception ex) { // 其他錯誤,記錄并繼續(xù)嘗試 worker.ReportProgress(0, $"密碼 '{password}' 嘗試失敗: {ex.Message}"); } // 每100次嘗試報告一次進(jìn)度 if (testedPasswords % 100 == 0) { int progress = (int)((double)testedPasswords / totalPasswords * 100); worker.ReportProgress(progress, password); } } } catch (Exception ex) { worker.ReportProgress(0, $"錯誤: {ex.Message}"); } } private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (e.UserState is string currentPassword) { GetTextBox("txtCurrentPassword").Text = currentPassword; } GetProgressBar().Value = e.ProgressPercentage; GetLabel("lblTested").Text = $"已嘗試: {testedPasswords:N0} / {totalPasswords:N0}"; // 計算速度 double seconds = stopwatch.Elapsed.TotalSeconds; double passwordsPerSecond = seconds > 0 ? testedPasswords / seconds : 0; GetLabel("lblSpeed").Text = $"速度: {passwordsPerSecond:N1} p/s"; // 計算剩余時間 if (passwordsPerSecond > 0) { long remaining = totalPasswords - testedPasswords; TimeSpan remainingTime = TimeSpan.FromSeconds(remaining / passwordsPerSecond); GetLabel("lblRemaining").Text = $"剩余時間: {remainingTime:hh\\:mm\\:ss}"; } GetLabel("lblElapsed").Text = $"已用時間: {stopwatch.Elapsed:hh\\:mm\\:ss}"; GetLabel("lblStatus").Text = "正在嘗試恢復(fù)密碼..."; } private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { stopwatch.Stop(); isRunning = false; if (e.Cancelled) { GetLabel("lblStatus").Text = "操作已取消"; } else if (e.Error != null) { GetLabel("lblStatus").Text = $"錯誤: {e.Error.Message}"; } else if (passwordFound) { GetLabel("lblStatus").Text = "密碼已找到!"; GetTextBox("txtCurrentPassword").Text = foundPassword; GetTextBox("txtCurrentPassword").ForeColor = Color.Lime; MessageBox.Show($"密碼已找到: {foundPassword}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { GetLabel("lblStatus").Text = "在字典中未找到密碼"; GetTextBox("txtCurrentPassword").Text = "未找到匹配的密碼"; } GetButton("btnStart").Enabled = true; GetButton("btnCancel").Enabled = false; } private void BtnCancel_Click(object sender, EventArgs e) { if (worker.IsBusy) { worker.CancelAsync(); GetButton("btnCancel").Enabled = false; GetLabel("lblStatus").Text = "正在取消操作..."; } } private void LinkLegal_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { MessageBox.Show("本工具僅供教育目的使用\n\n" + "僅限您合法擁有的文件使用\n\n" + "未經(jīng)授權(quán)訪問他人文件是違法行為\n\n" + "使用本工具即表示您同意承擔(dān)所有責(zé)任", "法律和道德聲明", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void ResetUI() { GetButton("btnStart").Enabled = true; GetButton("btnCancel").Enabled = false; } // Helper methods to get controls by name private TextBox GetTextBox(string name) => this.Controls.Find(name, true).FirstOrDefault() as TextBox; private Label GetLabel(string name) => this.Controls.Find(name, true).FirstOrDefault() as Label; private Button GetButton(string name) => this.Controls.Find(name, true).FirstOrDefault() as Button; private ProgressBar GetProgressBar() => this.Controls.Find("progressBar", true).FirstOrDefault() as ProgressBar; } }
以上就是基于C#實現(xiàn)rar文件密碼破解工具的詳細(xì)內(nèi)容,更多關(guān)于C#破解rar文件密碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
.Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實例
下面小編就為大家?guī)硪黄?Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02C#十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制int的方法
這篇文章主要介紹了C#十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制int的方法,涉及C#操作數(shù)制轉(zhuǎn)換的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03C#實現(xiàn)DataGridView控件行列互換的方法
這篇文章主要介紹了C#實現(xiàn)DataGridView控件行列互換的方法,涉及C#中DataGridView控件元素遍歷與添加操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#中Invoke和BeginInvoke實際應(yīng)用詳解
這篇文章主要給大家介紹了關(guān)于C#中Invoke和BeginInvoke實際應(yīng)用的相關(guān)資料,Invoke是對象方法,BeginInvoke是靜態(tài)方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12淺談C#中HttpWebRequest與HttpWebResponse的使用方法
本篇文章主要介紹了淺談C#中HttpWebRequest與HttpWebResponse的使用方法,具有一定的參考價值,有興趣的可以了解一下。2017-01-01