Creating all possible passwords from a set of rules

closed account (GNqMSL3A)
.
Last edited on
Let's be positive and assume that the CIA has a powerful computer so I would try a brute force approach first.
Use a for loop between 123456 and 987654 and test if the number matches criteria 2-4.
If the number matches then ?? No instruction but you can write them to a file.
1. accept number from std::cin
2. check it's within upper and lower bounds, if so then ...
3. split number into individual digits
http://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits
4. check the numbers are unique
http://en.cppreference.com/w/cpp/algorithm/unique
5. then check if the sum of digits is prime - many programs available online to check if a number is prime
6. finally, the left digit would be index [0] of the container holding the digits - use vector - then sort the vector and the lowest digit would be the new index [0]
edit: you can also do some input validation at stage 1 that the console entry is actual an integer, no spaces, other characters etc
Last edited on
Creating all possible passwords from a set of rules

That is a tough job. But you don't need std::cin or anything.

1. The password must be an integer greater than or equal to 100,000 and less than 1,000,000 (6 digits).

You need a for loop for this.

2. All the digits in the number have to be unique.

Use std::stringstream, then std::count to check if the number has unique digits.

3. The sum of the digits must be a prime number.

You make an isPrime() function then you sum up all digits and check.

4. The permutation of the left-most digit (n) and the smallest digit (k) must be odd—for example: in 645923, the left-most is 6 and the smallest digit is 2.

I don't know what that means, could you please explain? Thanks.
That is a tough job. But you don't need std::cin or anything.

Really?
Creating all possible passwords from a set of rules

The OP have to create all possible passwords, not any amount of passwords based on the input from the user.
... the job of developing a program that will analyze possible passwords ...
CIA personnel will then use the program to make sure the passwords they select to protect data conform to the rules
The rules that your program must use to determine if the password is valid ...


and all these would be possible without
std::cin or anything
???

so we have a password checking program that has no way of accepting an actual password!
Last edited on
I thought that the program was supposed to enumerate all passwords that satisfied the criterion.

Also
I don't know what that means, could you please explain?

https://en.wikipedia.org/wiki/Parity_of_a_permutation
I thought that the program was supposed to enumerate all passwords that satisfied the criterion.

...


The rules that your program must use to determine if the password is valid ...
... this password has to come from somewhere ...
> this password

All the pass words (that satisfy the criteria)

> has to come from somewhere

From within the program ("the job of developing a program that generates all valid passwords"),
not from user input.


Spoiler, off the cuff, untested etc. (essentially brute force, but not as brutish as the ones suggested earlier):
http://coliru.stacked-crooked.com/a/fbacec38cbab4c26
CIA personnel will then use the program to make sure the passwords they select
closed account (GNqMSL3A)
.
Last edited on
Your way will be getting no where. Firstly, write multiple functions and assign each function to a specific task.

If you want to generate all possible passwords, you only need a for loop and you don't need to input unless there is something the assignment stated.

1
2
3
4
5
6
a = password/100000;
b = password/10000 % 10;
c = password/1000 % 10;
d = password/100 % 10;
e = password/10 % 10;
f = password % 10;


This is complex and very potentially error-prone. It also does not work for all numbers.

You can use the function itoa to convert a number to string. You can use std::stringstream if you can feel like it.

1
2
char num_str[11];
itoa(number, num_str, 10); 


Or :
1
2
3
4
std::string num_str;
std::stringstream ss; 
ss << number;
ss >> num_str;


It is better you move the code to a function and let the function do the first task. Don't forget to print out the result (for testing purpose) if your number is correct as a string. Let me know if you are on the right track.

Edit :
std::to_string may be very useful since you only need a line for this :

std::string num_str(std::to_string(number));
Last edited on
closed account (GNqMSL3A)
The only problem is I am not suppose to use a function
The only problem is I am not suppose to use a function

You can use std::to_string to instantly convert a number to string. See my post above.

After that you can use std::unique or std::count to check if all digits are unique.
Here is another delete OP and he emptied his posts without saying a thing.

samander8 wrote:
This is what I have so far..right now I am working on how to get all
the numbers unique in each password. If anyone has any advice let me
know any help is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main(void)
{
    int password = 0, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, unqiue
= 0, sum;
    cout << "Enter password" << endl;
    cin >> password;




    for (password; password >= 100000 && password < 1000000;
password++)
    {
    a = password/100000;
    b = password/10000 % 10;
    c = password/1000 % 10;
    d = password/100 % 10;
    e = password/10 % 10;
    f = password % 10;


    //all the digits in the number have to be unique
    if (a != b && a != c && a != d && a != e && a != f && b !=c && b
!= d && b != e && b != d && b != e && b != e && c != d && c != e && c
!= f && d != e && d != f && e != f)
    {
        unqiue = 1; //means that all the numbers are unique
    }
    //sum of the digits must be a prime number
    sum = a + b + c + d + e + f;

    int primeCandidate;
    int isPrime = 1;//assume is a prime number

    for (int i=2; i<primeCandidate; i++)
    {
        if (primeCandidate%i == 0)
        {
            isPrime = 0; //not prime
        }
    {

    }
    cout << "Your passwords are: " << endl;
    cout << a << b << c << d << e << f <<endl;

    }


    return 0;

}}

My brute-force solution if I follow your approach - it is not complete yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <cmath>

int main(void)
{
    int password, a, b, c, d, e, f;
    for (password = 100000; password <= 110000; password++)
    {
        int password_ = password;
        
        a = password_ % 10; password_ /= 10; 
        b = password_ % 10; password_ /= 10; 
        c = password_ % 10; password_ /= 10; 
        d = password_ % 10; password_ /= 10; 
        e = password_ % 10; password_ /= 10; 
        f = password_ % 10; password_ /= 10; 
        
        auto is_unique = [&](int &chk)
        {
            int i = chk;
            chk = -999;
            
            if(i == a) return false;
            if(i == b) return false;
            if(i == c) return false;
            if(i == d) return false;
            if(i == e) return false;
            if(i == f) return false;

            chk = i;
            return true;
        };
 
        if( is_unique(a) && is_unique(b) && is_unique(c) && is_unique(d) && is_unique(e) && is_unique(f) )
        {   
            std::cout << f << e << d << c << b << a << std::endl;
        }
    }

    return 0;

}


As my parting gift since you just closed your account yourself.
Topic archived. No new replies allowed.