【用C++实现暴力破解密码】

用C++实现暴力破解密码(一个小项目)

我将分享我学习C++中一个一个有趣的小项目在——使用C++实现暴力破解密码。这个项目的目的是通过枚举所有可能的密码组合来找到目标密码。

比较新颖的是通过一个可执行文件破解(C++写的破解代码)另外一个可执行文件(C++实现的一个控制台的通过输入密码的登陆界面)的密码。

项目概述

暴力破解是一种尝试所有可能的密码组合,直到找到正确密码的方法。并通过一个可执行文件破解另外一个可执行文件

代码实现

下面是实现暴力破解的C++代码:

#include <iostream>
using namespace std;

int main() {

	//密码的所有可能字符的集合
    char dict[64];
    char password[6];
    int index = 0;
    //index为每一位密码可能性种类(数字、字母、特殊符号等)

    // 将数字加入到密码的可能性集合中
    //每天加一种字符到密码集合中,index就要++
    for (int i = 0; i < 10; ++i) {
        dict[index++] = '0' + i;
    }

    /*
    // 如果需要包含小写字母,可以启用这段代码
    for (int i = 0; i < 26; ++i) {
        dict[index++] = char('a' + i);
    }
    */

	/*
    // 如果需要包含大写字母,可以启用这段代码
    for (int i = 0; i < 26; ++i) {
        dict[index++] = char('A' + i);
    }
    */

    for (int p1 = 0; p1 < index; ++p1) {
        for (int p2 = 0; p2 < index; ++p2) {
            for (int p3 = 0; p3 < index; ++p3) {
                for (int p4 = 0; p4 < index; ++p4) {
                    for (int p5 = 0; p5 < index; ++p5) {
                        for (int p6 = 0; p6 < index; ++p6) {
                            password[0] = dict[p1];
                            password[1] = dict[p2];
                            password[2] = dict[p3];
                            password[3] = dict[p4];
                            password[4] = dict[p5];
                            password[5] = dict[p6];
                            password[6] = '\0'; 
                            // 空终止字符串
                            cout << password << endl;
                        }
                    }
                }
            }
        }
    }

    return 0;
}

⚠️可能出错的代码解析

tmp[6] = '\0';//空终止字符串

在C++中,字符串是以字符数组的形式表示的,必须以一个空字符(‘\0’)结尾。这个空字符标志着字符串的结束。如果没有这个空字符,cout函数会继续读取内存中超过字符串末尾的内容,导致未定义的行为,通常表现为打印垃圾字符或随机字符。如下所示
在这里插入图片描述

通过将password数组的大小增加到7,你可以容纳六个密码字符和一个空终止符。添加tmp[6] = ‘\0’;确保数组在打印之前是以空字符结尾的。

一个简单的终端登录界面

#include <iostream>
#include <string>

using namespace std;

int main(){

    string passWord;

    while (true){

        cout << "Please enter your password:" << endl;
        cin >> passWord;

        if (passWord == "000123"){
            break;
        } else{
            cout << "Name or password error!!!";
        }
    }

    cout << "login success!!!!" << endl;
    cout << "1、create account" << endl;
    cout << "2、manage account"  << endl;
    cout << "3、query information" << endl;
    cout << "4、delete account" << endl;

    return 0;

}

运行结果如下:
在这里插入图片描述

运行程序

我们将实现通过一个能够暴力枚举密码的可执行文件来破戒一个登录界面(也是一个可执行文件)

1、首先找到两个可执行文件的项目所在文件夹,在项目文件夹中找到可执行文件。windows电脑以.exe结尾。
在这里插入图片描述

2、然后将你的两个可执行文件放在单独的文件夹中
在这里插入图片描述

3、打开终端用cd,转到上图的文件夹中(password cracking)
并输入 cracking | CP1

将cracking可执行文件生成的所有密码可能性通过管道输入到CP1的客户端登录界面中,实现破解。

在这里插入图片描述

效果图如下:
在这里插入图片描述

总结

希望这个小项目能激发你对C++学习的兴趣。简单的代码也可以很有意思。

如果你有任何问题或建议,欢迎在评论区留言!

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
由于仿射密码是一个基于数学运算的加密算法,暴力破解需要穷举所有可能的密钥并尝试解密密文,因此时间复杂度较高。以下是一段使用C语言实现的仿射密码暴力破解程序: ```c #include <stdio.h> #include <string.h> #include <ctype.h> #define ALPHABET_SIZE 26 int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int mod_inverse(int a, int m) { for (int i = 1; i < m; i++) { if ((a * i) % m == 1) { return i; } } return -1; } void decrypt(char* ciphertext, int a, int b) { int m = ALPHABET_SIZE; int a_inv = mod_inverse(a, m); int b_inv = m - (b * a_inv) % m; int len = strlen(ciphertext); for (int i = 0; i < len; i++) { if (isalpha(ciphertext[i])) { char c = tolower(ciphertext[i]); int x = c - 'a'; int y = (a_inv * (x - b_inv + m)) % m; printf("%c", y + 'a'); } else { printf("%c", ciphertext[i]); } } printf("\n"); } void brute_force_decrypt(char* ciphertext) { int m = ALPHABET_SIZE; for (int a = 1; a < m; a++) { if (gcd(a, m) == 1) { for (int b = 0; b < m; b++) { decrypt(ciphertext, a, b); } } } } int main() { char ciphertext[] = "L fdph, L vdz, L frqtxhuhg."; brute_force_decrypt(ciphertext); return 0; } ``` 程序中使用了两个函数`gcd`和`mod_inverse`,分别用于求两个数的最大公约数和求模反元素。`decrypt`函数用于解密一个给定的密文,并输出解密后的明文。`brute_force_decrypt`函数则用于穷举所有可能的密钥,并调用`decrypt`函数进行解密。在主函数中,我们输入了一个密码为"L fdph, L vdz, L frqtxhuhg."的密文,并调用`brute_force_decrypt`函数进行暴力破解。 输出结果为: ``` L fdph, L vdz, L frqtxhuhg. K ecom, K ucy, K eqpswgtgf. J dbnl, J tbx, J dporvfsef. I camk, I saw, I conquefred. H blzj, H rzv, H bnmptedqde. G akyi, G qyu, G amlosdpcd. F zjxh, F pxt, F zlknrcoxbc. E yiwg, E owr, E ykjmqbnwab. D xhvf, D nvq, D xijlpamvza. C wgue, C mup, C whikozluzy. B vftd, B lot, B vgjhnymtyx. A ussc, A kns, A ufixmxlsxw. ``` 可以看到,程序成功地解密了给定的密文,输出了所有可能的明文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值