Guest User

C++ Extra Extra Extra Credit Password Generator

a guest
Apr 6th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8.     You're challenge for today is to create a random password generator!
  9.     For extra credit, allow the user to specify the amount of passwords to generate.
  10.     For even more extra credit, allow the user to specify the length of the strings he wants to generate!
  11. */
  12.  
  13. int main()
  14. {
  15.     srand( time(NULL) );
  16.     char CHA;
  17.     int LENGTH = 0, AMOUNT = 0, NUMBERS = 0, LETTERS = 0, CAPITALS = 0, SPECIAL = 0, RAND1 = 0, RAND2 = 0;
  18.     bool numbers = false, letters = false, capitals = false, special = false;
  19.     string STRINGLET = "abcdefghijklmnopqrstuvwxyzzflkjda"; //31
  20.     string STRINGCAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZSRTKLJD"; //31
  21.     string STRINGNUM = "012345678901234567890123456789016"; //31
  22.     string STRINGSPE = "`~!@#$%^&*()_+-={}[]|\\:\";'<>?,./"; //31
  23.  
  24.     cout << "Lenght of password in positive whole numbers? (Maximum 50 characters):  ";
  25.     cin >> LENGTH;
  26.    
  27.     cout << "\nHow many passwords of " << LENGTH << " characters would you like to generate?  ";
  28.     cin >> AMOUNT;
  29.  
  30.     if( LENGTH > 0 && LENGTH < 51 && AMOUNT > 0 )
  31.     {
  32.         cout << "\nWould you like to include letters in your password? (Y/N):  ";
  33.         cin >> CHA;
  34.         if( CHA == 'y' || CHA == 'Y' )
  35.         {
  36.             letters = true;
  37.         }
  38.  
  39.         cout << "\nWould you like to include CAPITAL letters in your password? (Y/N):  ";
  40.         cin >> CHA;
  41.         if( CHA == 'y' || CHA == 'Y' )
  42.         {
  43.             capitals = true;
  44.         }
  45.  
  46.         cout << "\nWould you like to include numbers in your password? (Y/N):  ";
  47.         cin >> CHA;
  48.         if( CHA == 'y' || CHA == 'Y' )
  49.         {
  50.             numbers = true;
  51.         }
  52.  
  53.         cout << "\nWould you like to include special characters in your password? (Y/N):  ";
  54.         cin >> CHA;
  55.         if( CHA == 'y' || CHA == 'Y' )
  56.         {
  57.             special = true;
  58.         }
  59.  
  60.         if( letters == false && numbers == false && capitals == false && special == false )
  61.         {
  62.             cout << "You haven't selected any characters for your passwords to be generated from.\nProgram terminated.\n";
  63.             cin.get();
  64.             cin.get();
  65.             return 1;
  66.         }
  67.  
  68.         cout << "\n\nPasswords:\n\n";
  69.         for( int X = 0; X != AMOUNT; X++ )
  70.         {
  71.             for( int Y = 0; Y != LENGTH; Y++ )
  72.             {
  73.                 if( letters == true && numbers == false && special == false && capitals == false )
  74.                 {
  75.                     RAND2 = rand() % 31 + 0;
  76.                     cout << STRINGLET[RAND2];
  77.                 }
  78.                 if( numbers == true && letters == false && capitals == false && special == false )
  79.                 {
  80.                     RAND2 = rand() % 31 + 0;
  81.                     cout << STRINGNUM[RAND2];
  82.                 }
  83.                 if( capitals == true && numbers == false && letters == false && special == false )
  84.                 {
  85.                     RAND2 = rand() % 31 + 0;
  86.                     cout << STRINGCAP[RAND2];
  87.                 }
  88.                 if( special == true && capitals == false && numbers == false && letters == false )
  89.                 {
  90.                     RAND2 = rand() % 31 + 0;
  91.                     cout << STRINGSPE[RAND2];
  92.                 }
  93.                 if( letters == true && capitals == true && numbers == false && special == false )
  94.                 {
  95.                     RAND1 = rand() % 2 + 1;
  96.                     RAND2 = rand() % 31 + 0;
  97.                     if ( RAND1 == 1 )
  98.                     {
  99.                         cout << STRINGLET[RAND2];
  100.                     }
  101.                     if ( RAND1 == 2 )
  102.                     {
  103.                         cout << STRINGCAP[RAND2];
  104.                     }
  105.                 }
  106.                 if( letters == true && numbers == true && capitals == false && special == false )
  107.                 {
  108.                     RAND1 = rand() % 2 + 1;
  109.                     RAND2 = rand() % 31 + 0;
  110.                     if ( RAND1 == 1 )
  111.                     {
  112.                         cout << STRINGLET[RAND2];
  113.                     }
  114.                     if ( RAND1 == 2 )
  115.                     {
  116.                         cout << STRINGNUM[RAND2];
  117.                     }
  118.                 }
  119.                 if( letters == true && special == true && numbers == false && capitals == false )
  120.                 {
  121.                     RAND1 = rand() % 2 + 1;
  122.                     RAND2 = rand() % 31 + 0;
  123.                     if ( RAND1 == 1 )
  124.                     {
  125.                         cout << STRINGLET[RAND2];
  126.                     }
  127.                     if ( RAND1 == 2 )
  128.                     {
  129.                         cout << STRINGSPE[RAND2];
  130.                     }
  131.                 }
  132.                 if( capitals == true && special == true && numbers == false && letters == false )
  133.                 {
  134.                     RAND1 = rand() % 2 + 1;
  135.                     RAND2 = rand() % 31 + 0;
  136.                     if ( RAND1 == 1 )
  137.                     {
  138.                         cout << STRINGCAP[RAND2];
  139.                     }
  140.                     if ( RAND1 == 2 )
  141.                     {
  142.                         cout << STRINGSPE[RAND2];
  143.                     }
  144.                 }
  145.                 if( numbers == true && capitals == true && letters == false && special == false )
  146.                 {
  147.                     RAND1 = rand() % 2 + 1;
  148.                     RAND2 = rand() % 31 + 0;
  149.                     if ( RAND1 == 1 )
  150.                     {
  151.                         cout << STRINGCAP[RAND2];
  152.                     }
  153.                     if ( RAND1 == 2 )
  154.                     {
  155.                         cout << STRINGNUM[RAND2];
  156.                     }
  157.                 }
  158.                 if( special == true && numbers == true && capitals == false && letters == false )
  159.                 {
  160.                     RAND1 = rand() % 2 + 1;
  161.                     RAND2 = rand() % 31 + 0;
  162.                     if ( RAND1 == 1 )
  163.                     {
  164.                         cout << STRINGNUM[RAND2];
  165.                     }
  166.                     if ( RAND1 == 2 )
  167.                     {
  168.                         cout << STRINGSPE[RAND2];
  169.                     }
  170.                 }
  171.                 if( letters == true && capitals == true && numbers == true && special == false )
  172.                 {
  173.                     RAND1 = rand() % 3 + 1;
  174.                     RAND2 = rand() % 31 + 0;
  175.                     if ( RAND1 == 1 )
  176.                     {
  177.                         cout << STRINGLET[RAND2];
  178.                     }
  179.                     if ( RAND1 == 2 )
  180.                     {
  181.                         cout << STRINGCAP[RAND2];
  182.                     }
  183.                     if ( RAND1 == 3 )
  184.                     {
  185.                         cout << STRINGNUM[RAND2];
  186.                     }
  187.                 }
  188.                 if( letters == true && capitals == true && special == true && numbers == false )
  189.                 {
  190.                     RAND1 = rand() % 3 + 1;
  191.                     RAND2 = rand() % 31 + 0;
  192.                     if ( RAND1 == 1 )
  193.                     {
  194.                         cout << STRINGLET[RAND2];
  195.                     }
  196.                     if ( RAND1 == 2 )
  197.                     {
  198.                         cout << STRINGCAP[RAND2];
  199.                     }
  200.                     if ( RAND1 == 3 )
  201.                     {
  202.                         cout << STRINGSPE[RAND2];
  203.                     }
  204.                 }
  205.                 if( letters == true && numbers == true && special == true && capitals == false )
  206.                 {
  207.                     RAND1 = rand() % 3 + 1;
  208.                     RAND2 = rand() % 31 + 0;
  209.                     if ( RAND1 == 1 )
  210.                     {
  211.                         cout << STRINGLET[RAND2];
  212.                     }
  213.                     if ( RAND1 == 2 )
  214.                     {
  215.                         cout << STRINGNUM[RAND2];
  216.                     }
  217.                     if ( RAND1 == 3 )
  218.                     {
  219.                         cout << STRINGSPE[RAND2];
  220.                     }
  221.                 }
  222.                 if( numbers == true && capitals == true && special == true && letters == false )
  223.                 {
  224.                     RAND1 = rand() % 4 + 2;
  225.                     RAND2 = rand() % 31 + 0;
  226.  
  227.                     if ( RAND1 == 2 )
  228.                     {
  229.                         cout << STRINGCAP[RAND2];
  230.                     }
  231.                     if ( RAND1 == 3 )
  232.                     {
  233.                         cout << STRINGNUM[RAND2];
  234.                     }
  235.                     if ( RAND1 == 4 )
  236.                     {
  237.                         cout << STRINGSPE[RAND2];
  238.                     }
  239.                 }
  240.                 if( letters == true && capitals == true && numbers == true && special == true )
  241.                 {
  242.                     RAND1 = rand() % 4 + 1;
  243.                     RAND2 = rand() % 31 + 0;
  244.                     if ( RAND1 == 1 )
  245.                     {
  246.                         cout << STRINGLET[RAND2];
  247.                     }
  248.                     if ( RAND1 == 2 )
  249.                     {
  250.                         cout << STRINGCAP[RAND2];
  251.                     }
  252.                     if ( RAND1 == 3 )
  253.                     {
  254.                         cout << STRINGNUM[RAND2];
  255.                     }
  256.                     if ( RAND1 == 4 )
  257.                     {
  258.                         cout << STRINGSPE[RAND2];
  259.                     }
  260.                 }
  261.             }
  262.             cout << "\n";
  263.         }
  264.     }
  265.     else
  266.     {
  267.         cout << "You've entered an incorrect number for the length or amount, the program will now end." << endl;
  268.     }
  269.     cout << "\n\nDone.\n\nPress Enter to Quit!";
  270.     cin.get();
  271.     cin.get();
  272.     return 0;
  273. }
Add Comment
Please, Sign In to add comment