最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于C#實現(xiàn)rar文件密碼破解工具

 更新時間:2025年07月01日 11:08:51   作者:iCxhust  
這篇文章主要為大家詳細(xì)介紹了如何基于C#實現(xiàn)一個rar文件密碼破解工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文主要介紹了一個C#編寫的RAR壓縮文件密碼恢復(fù)工具,該程序通過加載密碼字典文件,逐個嘗試密碼來破解受密碼保護(hù)的RAR文件。

主要功能包括

1.選擇RAR文件和密碼字典

2.顯示恢復(fù)進(jìn)度

3.統(tǒng)計嘗試密碼數(shù)量和速度

4.計算剩余時間

本工具采用后臺線程運行密碼破解過程,可以避免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)文章

  • C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲原理分析

    C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲原理分析

    這篇文章主要介紹了C# 指針 內(nèi)存控制 Marshal 內(nèi)存數(shù)據(jù)存儲原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C#圖片按比例縮放的實現(xiàn)代碼

    C#圖片按比例縮放的實現(xiàn)代碼

    這篇文章主要介紹了C#圖片按比例縮放的實現(xiàn)代碼,有需要的朋友可以參考一下
    2014-01-01
  • C#基于時間輪調(diào)度實現(xiàn)延遲任務(wù)詳解

    C#基于時間輪調(diào)度實現(xiàn)延遲任務(wù)詳解

    在很多.net開發(fā)體系中開發(fā)者在面對調(diào)度作業(yè)需求的時候一般會選擇三方開源成熟的作業(yè)調(diào)度框架來滿足業(yè)務(wù)需求,但是有些時候可能我們只是需要一個簡易的延遲任務(wù)。本文主要分享一個簡易的基于時間輪調(diào)度的延遲任務(wù)實現(xiàn),需要的可以參考一下
    2022-12-12
  • C#泛型設(shè)計需要注意的一個小陷阱

    C#泛型設(shè)計需要注意的一個小陷阱

    這篇文章主要給大家介紹了關(guān)于C#泛型設(shè)計需要注意的一個小陷阱,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Unity3D使用UGUI開發(fā)原生虛擬搖桿

    Unity3D使用UGUI開發(fā)原生虛擬搖桿

    這篇文章主要為大家詳細(xì)介紹了Unity3D使用UGUI開發(fā)原生虛擬搖桿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#日期和時間DateTime轉(zhuǎn)字符串最佳實踐

    C#日期和時間DateTime轉(zhuǎn)字符串最佳實踐

    在 C# 開發(fā)中,DateTime類型的時間格式化是高頻操作場景,本文將通過 20 + 實戰(zhàn)示例,全面解析DateTime轉(zhuǎn)換為字符串的核心方法與最佳實踐,感興趣的朋友一起看看吧
    2025-06-06
  • C#實現(xiàn)的Win32控制臺線程計時器功能示例

    C#實現(xiàn)的Win32控制臺線程計時器功能示例

    這篇文章主要介紹了C#實現(xiàn)的Win32控制臺線程計時器功能,結(jié)合實例形式分析了C#基于控制臺的時間操作相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法

    C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法

    這篇文章主要介紹了C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法,C#的數(shù)組可作為實參傳遞給方法形參,需要的朋友可以參考下
    2016-01-01
  • C# Datatable的幾種用法小結(jié)

    C# Datatable的幾種用法小結(jié)

    這篇文章主要介紹了C# Datatable的幾種用法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 理解C#中的Lambda表達(dá)式

    理解C#中的Lambda表達(dá)式

    這篇文章主要介紹了理解C#中的Lambda表達(dá)式,本文用實例代碼來講解Lambda表達(dá)式,用不同的角度總結(jié)對它的認(rèn)識,需要的朋友可以參考下
    2015-04-04

最新評論

合水县| 和硕县| 武夷山市| 罗田县| 新建县| 龙州县| 荥经县| 志丹县| 张家港市| 眉山市| 大埔区| 芒康县| 唐河县| 浪卡子县| 黔南| 德格县| 安国市| 陕西省| 临城县| 台中县| 二手房| 石狮市| 盐城市| 阿克陶县| 孟州市| 襄汾县| 榆社县| 竹山县| 包头市| 望都县| 庆城县| 独山县| 闻喜县| 盐津县| 元阳县| 南昌市| 宁德市| 桦川县| 广德县| 永康市| 遂宁市|