首页 > 解决方案 > 每次用户几乎找到密码时都尝试生成不同的密码

问题描述

首先,英语不是我的母语,所以如果您有什么不明白的地方请告诉我。

分配是,有一个9位,只有整数密码,例如:385987231。用户将输入一个9位输入。每次用户接近猜测/找到密码时,密码将被更改。如果8位数字密码和用户输入匹配,密码会改变。数字的顺序并不重要。唯一重要的是,如果八个数字匹配,密码就会改变。

例如:密码是 123456789,用户输入 223456789 在这种情况下密码会自行更改。

有一个 hiddenPassword 它是在开始时创建的,还有一个 Password 它是用户的输入。

我已经创建了 passwordChecker 和 passwordCreater 函数。passwordChecker 检查数组中的 2 个数字是否匹配,并且每次匹配时计数器都会增加 1。如果计数器为 8,则 hiddenPassword 会更改。如果不是,它不会改变。

passwordCreater 函数创建随机密码。因为它在 char 变量中,所以我在 ASCII 表中添加 48 以使其成为整数,然后问题就开始了。

即使函数创建了密码,它也不返回密码并且密码不正确。而且我认为passwordChecker 不起作用,因为它是一个无效函数,因此它不会返回任何内容。当我在passwordChecker 中调用passwordCreater 时,什么也没有,也什么都没有。可悲的是。

#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <time.h> 
#include <stdlib.h>
using namespace std;


char passwordCreater()
{
    srand((unsigned)time(0)); 
    char random_integer[20]; 
    for (int index=0; index<9; index++)
    {
        random_integer[index] = (rand()%10)+1; 
        random_integer[index]=random_integer[index]+48;
        cout << random_integer << endl; 
    } 
    return random_integer[9];
}



void passwordChecker (char a[9], char b[9])
{
    int counter;
    for (int i=0;i++;i<10)
    {
        if (a[i]==b[i])
        {
            counter=counter+1;
        } 
        else
        {
            counter=counter;
        }
    }
    printf("%d \n",counter);
    if (counter==8)
    {
        b[9]==passwordCreater();
    } 
    else if (counter==9)
    {
        printf("Password is right!");
    } 
    else
    {
        printf("Password is wrong!");
        return;
    }
}


int main()
{
    char hiddenPassword[9];
    hiddenPassword[9]==passwordCreater();
    char password[9];
    printf("%s \n",hiddenPassword);
    printf("------------------------");
    printf("\n       PASSWORD:       ");
    printf("\n------------------------\n");
    scanf("%s", &password);
    printf("%s \n",password);
    passwordChecker(password,hiddenPassword);
    return 0;
}

标签: c++passwords

解决方案


这是一个解决方案:单独生成随机种子,并且只生成一次

相当简单地生成随机密码,然后通过分两部分分析来检查用户输入。

#1:检查它们之前的长度是否相同

#2:比较输入密码中的每个数字,并相应地给出反馈。如果Counter读数为 8,则会创建一个新密码。享受。

编辑:我使用<random>了 header 而不是srand(time(0))andrand()来摆脱偏见。

#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <vector>
using namespace std;

mt19937 Generator;
uniform_int_distribution<int> Distribution(0,9);

void GenerateRandomSeed() {
    Generator.seed(time(0));
}

vector<char> CreatePassword() {
    cout << "Hidden password: ";
    vector<char> Password;
    for(int i = 0; i < 9; ++i) {
        Password.push_back(Distribution(Generator) + 48);
        cout << Password[i];
    }
    cout << endl;
    return Password;
}

bool isPasswordCorrect(string & InputPassword, vector<char> & HiddenPassword) {
    if(InputPassword.length() != HiddenPassword.size()) {
        cout << "Password is wrong!" << endl;
        return false;
    }
    int Counter = 0;
    for(int i = 0; i < InputPassword.length(); ++i) {
        if(InputPassword[i] == HiddenPassword[i])
            ++Counter;
    }
    if(Counter == 8) {
        HiddenPassword = CreatePassword();
        cout << "Password was close, new hidden password has been generated" << endl;
        return false;
    } else if (Counter == 9){
        cout << "Password is right!" << endl;
        return true;
    }
    cout << "Password is wrong!" << endl;
    return false;
}

int main() {
    GenerateRandomSeed();
    string InputPassword;
    vector<char> HiddenPassword;
    HiddenPassword = CreatePassword();
    do {
        cout << "Input correct password please: ";
        cin >> InputPassword;
    } while (!isPasswordCorrect(InputPassword, HiddenPassword));
    return 0;
}

推荐阅读