Random PW Generator
main.cpp@0:4b30ded991e6, 2020-05-03 (annotated)
- Committer:
- stefanblack
- Date:
- Sun May 03 14:28:47 2020 +0000
- Revision:
- 0:4b30ded991e6
Nr74
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefanblack | 0:4b30ded991e6 | 1 | /* |
stefanblack | 0:4b30ded991e6 | 2 | |
stefanblack | 0:4b30ded991e6 | 3 | C++ Kurs 6ABELI SM |
stefanblack | 0:4b30ded991e6 | 4 | Aufgabe 3 Übung NR 74 -- Passwortgenerator |
stefanblack | 0:4b30ded991e6 | 5 | Stefan Schwarz |
stefanblack | 0:4b30ded991e6 | 6 | |
stefanblack | 0:4b30ded991e6 | 7 | Übung Nr.74: |
stefanblack | 0:4b30ded991e6 | 8 | |
stefanblack | 0:4b30ded991e6 | 9 | Erstelle ein Programm/Funktion welche/s ein zufälliges Passwort erzeugt. |
stefanblack | 0:4b30ded991e6 | 10 | Als Parameter soll die Länge sowie die Art der darin vorkommenden Zeichen übergeben werden können. |
stefanblack | 0:4b30ded991e6 | 11 | Zur Auswahl sollen stehen: Kleinbuchstaben, Großbuchstaben, Zahlen und Sonderzeichen. |
stefanblack | 0:4b30ded991e6 | 12 | Jede beliebige Kombination der Parameter soll möglich sein. |
stefanblack | 0:4b30ded991e6 | 13 | |
stefanblack | 0:4b30ded991e6 | 14 | */ |
stefanblack | 0:4b30ded991e6 | 15 | |
stefanblack | 0:4b30ded991e6 | 16 | |
stefanblack | 0:4b30ded991e6 | 17 | #include <algorithm> |
stefanblack | 0:4b30ded991e6 | 18 | #include <stdio.h> |
stefanblack | 0:4b30ded991e6 | 19 | #include <iostream> |
stefanblack | 0:4b30ded991e6 | 20 | #include <cstdlib> |
stefanblack | 0:4b30ded991e6 | 21 | #include <ctime> |
stefanblack | 0:4b30ded991e6 | 22 | |
stefanblack | 0:4b30ded991e6 | 23 | |
stefanblack | 0:4b30ded991e6 | 24 | using namespace std; |
stefanblack | 0:4b30ded991e6 | 25 | |
stefanblack | 0:4b30ded991e6 | 26 | // function generates new password based on given parameters |
stefanblack | 0:4b30ded991e6 | 27 | string genpw(int lengh, string number, string smallletter, string bigletter, string signs); |
stefanblack | 0:4b30ded991e6 | 28 | |
stefanblack | 0:4b30ded991e6 | 29 | |
stefanblack | 0:4b30ded991e6 | 30 | int main() |
stefanblack | 0:4b30ded991e6 | 31 | { |
stefanblack | 0:4b30ded991e6 | 32 | int lengh; |
stefanblack | 0:4b30ded991e6 | 33 | string numbers; |
stefanblack | 0:4b30ded991e6 | 34 | string smallletter; |
stefanblack | 0:4b30ded991e6 | 35 | string bigletter; |
stefanblack | 0:4b30ded991e6 | 36 | string signs; |
stefanblack | 0:4b30ded991e6 | 37 | |
stefanblack | 0:4b30ded991e6 | 38 | //setting parameters via Console |
stefanblack | 0:4b30ded991e6 | 39 | cout<<" Wilkommen im Passwortgenerator !"<< endl << endl; |
stefanblack | 0:4b30ded991e6 | 40 | again: |
stefanblack | 0:4b30ded991e6 | 41 | cout << "Wie lange soll das Passwort sein? " ; |
stefanblack | 0:4b30ded991e6 | 42 | cin >> lengh; |
stefanblack | 0:4b30ded991e6 | 43 | cout << "Ziffern? J/N "; |
stefanblack | 0:4b30ded991e6 | 44 | cin.get(); |
stefanblack | 0:4b30ded991e6 | 45 | cin >> numbers; |
stefanblack | 0:4b30ded991e6 | 46 | cin.get(); |
stefanblack | 0:4b30ded991e6 | 47 | cout << "Kleinbuschstaben? J/N " ; |
stefanblack | 0:4b30ded991e6 | 48 | cin >> smallletter; |
stefanblack | 0:4b30ded991e6 | 49 | cout << "Großbuschstaben? J/N " ; |
stefanblack | 0:4b30ded991e6 | 50 | cin >> bigletter; |
stefanblack | 0:4b30ded991e6 | 51 | cout << "Sonderzeichen? J/N " ; |
stefanblack | 0:4b30ded991e6 | 52 | cin >> signs; |
stefanblack | 0:4b30ded991e6 | 53 | |
stefanblack | 0:4b30ded991e6 | 54 | // check if theres at least one chosen parameter |
stefanblack | 0:4b30ded991e6 | 55 | if(numbers!="J" && smallletter!="J" && bigletter!="J" && signs!="J") |
stefanblack | 0:4b30ded991e6 | 56 | { |
stefanblack | 0:4b30ded991e6 | 57 | cout << "Min 1 Parameter muss gewählt werden! Bitte erneute Eingabe:"<<endl; |
stefanblack | 0:4b30ded991e6 | 58 | goto again; |
stefanblack | 0:4b30ded991e6 | 59 | } |
stefanblack | 0:4b30ded991e6 | 60 | |
stefanblack | 0:4b30ded991e6 | 61 | //print newly generated password |
stefanblack | 0:4b30ded991e6 | 62 | cout << endl << genpw(lengh,numbers,smallletter,bigletter,signs); |
stefanblack | 0:4b30ded991e6 | 63 | |
stefanblack | 0:4b30ded991e6 | 64 | |
stefanblack | 0:4b30ded991e6 | 65 | } |
stefanblack | 0:4b30ded991e6 | 66 | |
stefanblack | 0:4b30ded991e6 | 67 | |
stefanblack | 0:4b30ded991e6 | 68 | // genpw function generates new password based on given parameters |
stefanblack | 0:4b30ded991e6 | 69 | string genpw(int lengh, string number, string smallletter, string bigletter, string signs ){ |
stefanblack | 0:4b30ded991e6 | 70 | |
stefanblack | 0:4b30ded991e6 | 71 | string newpw = ""; |
stefanblack | 0:4b30ded991e6 | 72 | string coderes = ""; |
stefanblack | 0:4b30ded991e6 | 73 | string numb = "123456789"; |
stefanblack | 0:4b30ded991e6 | 74 | string slbs = "abcdefghijklmnopqrstuvwxyz"; |
stefanblack | 0:4b30ded991e6 | 75 | string blbs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
stefanblack | 0:4b30ded991e6 | 76 | string sgn = "@!§$%&/()=?*':;"; |
stefanblack | 0:4b30ded991e6 | 77 | |
stefanblack | 0:4b30ded991e6 | 78 | if (number == "J") |
stefanblack | 0:4b30ded991e6 | 79 | coderes += numb; |
stefanblack | 0:4b30ded991e6 | 80 | if (smallletter == "J") |
stefanblack | 0:4b30ded991e6 | 81 | coderes += slbs; |
stefanblack | 0:4b30ded991e6 | 82 | if (bigletter == "J") |
stefanblack | 0:4b30ded991e6 | 83 | coderes += blbs; |
stefanblack | 0:4b30ded991e6 | 84 | if (signs == "J") |
stefanblack | 0:4b30ded991e6 | 85 | coderes += sgn; |
stefanblack | 0:4b30ded991e6 | 86 | |
stefanblack | 0:4b30ded991e6 | 87 | srand(time(0)); |
stefanblack | 0:4b30ded991e6 | 88 | |
stefanblack | 0:4b30ded991e6 | 89 | for (unsigned int i = 0; i < lengh; i++) |
stefanblack | 0:4b30ded991e6 | 90 | { |
stefanblack | 0:4b30ded991e6 | 91 | random_shuffle(coderes.begin(), coderes.end()); //shuffeling available characters in addition to randomly picked ones |
stefanblack | 0:4b30ded991e6 | 92 | |
stefanblack | 0:4b30ded991e6 | 93 | int sizeres= sizeof(coderes); // |
stefanblack | 0:4b30ded991e6 | 94 | newpw+=coderes[rand()% sizeres]; //building newpw string randomly |
stefanblack | 0:4b30ded991e6 | 95 | } |
stefanblack | 0:4b30ded991e6 | 96 | return "Das generierte Passwort lautet: "+ newpw; |
stefanblack | 0:4b30ded991e6 | 97 | |
stefanblack | 0:4b30ded991e6 | 98 | |
stefanblack | 0:4b30ded991e6 | 99 | } |