C# :: Aufgabe #77
14 Lösungen

Passwortgenerator mit Parametern
Anfänger - C#
von pocki
- 27.11.2014 um 18:11 Uhr
Erstelle ein Programm/Funktion welche/s ein zufälliges Passwort erzeugt.
Als Parameter soll die Länge sowie die Art der darin vorkommenden Zeichen übergeben werden können.
Zur Auswahl sollen stehen: Kleinbuchstaben, Großbuchstaben, Zahlen und Sonderzeichen.
Jede beliebige Kombination der Parameter soll möglich sein.
Als Parameter soll die Länge sowie die Art der darin vorkommenden Zeichen übergeben werden können.
Zur Auswahl sollen stehen: Kleinbuchstaben, Großbuchstaben, Zahlen und Sonderzeichen.
Jede beliebige Kombination der Parameter soll möglich sein.
Lösungen:

using System; using System.Text; namespace trainYourProgrammer { class MainClass { private static string createPassword(bool lowerCase, bool upperCase, bool digits, bool specialChar, int length) { string alphabet = ""; if (lowerCase) alphabet += "abcdefghijklmnopqrstuvwxyz"; if (upperCase) alphabet += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (digits) alphabet += "0123456789"; if (specialChar) alphabet += "^!\"§$%&/()=?²³{[]}\\`´+*~#',.-;:_<>|"; if (alphabet.Length == 0) return "error"; StringBuilder result = new StringBuilder (); Random r = new Random (); while (result.Length < length) result.Append (alphabet [r.Next (alphabet.Length)]); return result.ToString (); } static void Main(string[] args) { Console.Write ("Länge des Passworts: "); int length = int.Parse (Console.ReadLine ()); Console.Write ("Großbuchstaben verwenden (j/n): "); bool upperCase = Console.ReadLine () == "j"; Console.Write ("Kleinbuchstaben verwenden (j/n): "); bool lowerCase = Console.ReadLine () == "j"; Console.Write ("Ziffern verwenden (j/n): "); bool digits = Console.ReadLine () == "j"; Console.Write ("Sonderzeichen verwenden (j/n): "); bool specialChars = Console.ReadLine () == "j"; Console.WriteLine (createPassword (lowerCase, upperCase, digits, specialChars, length)); } } }

using System; namespace ConsoleApplication10 { class Program { static void Main(string[] args) { string chars = "ADEqrstuvwxyz01&2345FGHIJKLMNOPQ+*%.RSZabhBCijklmnop6TUVcdefgWXY789?/$!_-"; Char[] stringChars = new Char[8]; Random random = new Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; //returns random number less than maximum } string finalystring = new string(stringChars); Console.WriteLine(finalystring); Console.Read(); } } }
Lösung zu Passwortgenerator als Form umgesetzt:
C#-Code
mit zusätzlicher Klasse:
C#-Code

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace pwd1 { public partial class Form1 : Form { public Form1 ( ) { InitializeComponent (); } private void button1_Click ( object sender, EventArgs e ) { Close (); } private void button2_Click ( object sender, EventArgs e ) { if (numTextBox1.TextLength != 0) try { int laenge = Convert.ToInt32(numTextBox1.Text); //a=1; A=2; 1=4 char[] lower_case = new char[] { 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', }; char[] upper_case = new char[] { 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', }; char[] numbers = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; char[] symbols = new char[] { '²', '³', '{', '[', ']', '}', '\\', '^', '°', '!', '"', '§', '$', '%', '&', '/', '(', ')', '=', '?', '`', '´', '+', '*', '~', '#', '\'', '-', '_', '.', ':', ',', ';', '<', '>', '|' }; System.Text.StringBuilder pool = new System.Text.StringBuilder(); if (checkBox1.Checked) pool.Append(lower_case); if (checkBox2.Checked) pool.Append(upper_case); if (checkBox3.Checked) pool.Append(numbers); if (checkBox4.Checked) pool.Append(symbols); if ((!checkBox1.Checked) && (!checkBox2.Checked) && (!checkBox3.Checked) && (!checkBox4.Checked)) { MessageBox.Show(String.Format("Nothing selected!"), "Results", MessageBoxButtons.OK); Close(); } Random rnd = new Random(); string passwd = ""; for (int i = 1; i <= laenge; i++) { char passwd_tmp = pool[rnd.Next(pool.Length)]; passwd = passwd + passwd_tmp; } textBox1.Text = passwd; } catch { Close(); } } } }
mit zusätzlicher Klasse:

using System; using System.Collections.Generic; using System.Text; namespace pwd1 { /// class downloaded at http://dotnet-snippets.de/snippet/numtextbox-kurzversion/1444 2014.12.05 /// class created by Martin Dauskardt class NumTextBox:System.Windows.Forms.TextBox { //Constructor public NumTextBox() { //Aufruf von Context Menü unterbinden. this.ShortcutsEnabled = false; //KeyPress Ereignis anmelden this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(NumTextBox_KeyPress); } void NumTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { //Prüfung, ob eine Zahl oder BackSpace ausgelöst wurde if ("1234567890\b".IndexOf(e.KeyChar.ToString()) < 0) { //Bei Abweichung Ereignis verwerfen e.Handled = true; } } } }

/* * Erstellt mit SharpDevelop. * Benutzer: especk * Datum: 27.01.2015 * Zeit: 09:32 * * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern. */ using System; using System.Collections.Generic; namespace Passwortgenerator { class Program { public static string Parameter(string parameter) { Console.WriteLine("Geben Sie bitte folgende parameter ein: "); Console.WriteLine(""); Console.WriteLine("Kleinbuchstaben:\t1"); Console.WriteLine("Großbuchstaben:\t\t2"); Console.WriteLine("Ziffern:\t \t3"); Console.WriteLine("Sonderzeichen:\t\t4"); Console.WriteLine(""); try { parameter = Console.ReadLine(); if(parameter.Length > 4 || parameter.Length < 1){ Console.WriteLine("Falsche bzw. keine Parameterangabe"); Console.ReadKey(true); Environment.Exit(0);} return parameter; } catch (Exception) { Console.WriteLine("Falsche Parameterangabe!"); parameter = ""; Console.ReadKey(true); Environment.Exit(0);return parameter; } } public static int Laenge(int laenge) { Console.WriteLine("Geben Sie die Passwortlänge (max 25 Zeichen) ein:"); try { laenge = Convert.ToInt16(Console.ReadLine()); if (laenge > 25 || laenge < 1) { Console.WriteLine("Falsche Längenangabe!"); Console.ReadKey(true); Environment.Exit(0);; } return laenge; } catch (Exception) { Console.WriteLine("Falsche Längenangabe!"); laenge = 0; Console.ReadKey(true); Environment.Exit(0);return laenge; } } public static char[] PasswortZeichenArray(string parameter, int laenge) { string klein = "abcdefghijklmnopqrstuvwxyz"; string groß ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string zahl ="0123456789"; string zeichen = "!$§%&/()=?/*-+"; string par1 = "1"; string par2 = "2"; string par3 = "3"; string par4 = "4"; int test=0; int test1; int test2=0; int test3=0; int test4=0; for (int i = 0; i < parameter.Length; i++) { test1=parameter.IndexOf(par1,i); if(test1>=0){test=1;} test1=parameter.IndexOf(par2,i); if(test1>=0){test2=2;} test1=parameter.IndexOf(par3,i); if(test1>=0){test3=3;} test1=parameter.IndexOf(par4,i); if(test1>=0){test4=4;} } char[] kleinArray = klein.ToCharArray(); char[] großArray = groß.ToCharArray(); char[] zahlArray = zahl.ToCharArray(); char[] sonderArray = zeichen.ToCharArray(); List<char> zeichenListe = new List<char>(); if(test == 1){ for (int i = 0; i < kleinArray.Length; i++) { zeichenListe.Add(kleinArray[i]); } } if(test2 == 2){ for (int i = 0; i < großArray.Length; i++) { zeichenListe.Add(großArray[i]); } } if(test3 == 3){ for (int i = 0; i < zahlArray.Length; i++) { zeichenListe.Add(zahlArray[i]); } } if(test4 == 4){ for (int i = 0; i < sonderArray.Length; i++) { zeichenListe.Add(sonderArray[i]); } } Random random = new Random(); int listenLänge = zeichenListe.Count; char[] passwortArray = new char[laenge]; for (int i = 0; i < laenge; i++) { int zufallsZahl = random.Next(1,listenLänge); passwortArray[i] = zeichenListe[zufallsZahl]; } return passwortArray; } public static void Main(string[] args) { Console.WriteLine("Passwortgenerator"); Console.WriteLine(""); string parameter=""; parameter = Parameter(parameter); int laenge = 0; laenge = Laenge(laenge); Console.WriteLine(""); char[] passwort = PasswortZeichenArray(parameter, laenge); foreach (char element in passwort) { Console.Write(element); } Console.WriteLine(""); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }

namespace TrainYourProgrammer { using System; using System.Collections.Generic; internal class Generator { public string pwNumbers = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789^!\"§$%&/()=?{[]}\\`´+*~#',.-;:_<>|°äöüÄÖÜß"; private readonly Random random = new Random(); public string passwort(int passwortLength) { string finalPassword = string.Empty; var passwordList = new List<string>(); for (int i = 0; i < passwortLength; i++) { passwordList.Add(pwNumbers[random.Next(pwNumbers.Length)].ToString()); } foreach (string PasswordItem in passwordList) { finalPassword = finalPassword + PasswordItem; } return finalPassword; } } internal class Program { private static void Main(string[] args) { bool state = false; int result; var generator = new Generator(); while (!state) { Console.WriteLine("Password Länge eingeben:"); if (int.TryParse(Console.ReadLine(), out result)) { string generatedPassword = generator.passwort(result); Console.WriteLine(generatedPassword); } else { Console.WriteLine("Bitte nur ganze Zahl eingeben! (max: 9999"); state = false; } } } } }
Funktionalität für ein Zufälliges Passwort ohne irreführende Zeichen wie I und l.
C#-Code

void Main() { GeneratePassword(16, PasswordType.UpperLetter | PasswordType.LowerLetter | PasswordType.Numerics | PasswordType.SpecialCharacter).Dump(); } public string GeneratePassword(int length, PasswordType type) { char[] upperLetter = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; char[] lowerLetter = new[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; char[] numeris = new[] { '2', '3', '4', '5', '6', '7', '8', '9' }; char[] specialCharacters = new[] { '!', '$', '%', '#', '?', '+', '-', '*', '/', '_', ':', '.' }; string password = string.Empty; List<char[]> allowedCharacters = new List<char[]>(); if ((type & PasswordType.UpperLetter) == PasswordType.UpperLetter) allowedCharacters.Add(upperLetter); if ((type & PasswordType.LowerLetter) == PasswordType.LowerLetter) allowedCharacters.Add(lowerLetter); if ((type & PasswordType.Numerics) == PasswordType.Numerics) allowedCharacters.Add(numeris); if ((type & PasswordType.SpecialCharacter) == PasswordType.SpecialCharacter) allowedCharacters.Add(specialCharacters); Random random = new Random(); for (int i = 0; i < length; i++) { char[] characters = allowedCharacters[random.Next(0, allowedCharacters.Count)]; password += characters[random.Next(0, characters.Length)].ToString(); } return password; } [Flags] public enum PasswordType { UpperLetter = 0x01, LowerLetter = 0x02, Numerics = 0x04, SpecialCharacter = 0x08 }
Todesumständlich, aber funktioniert prima.
Leider nur 'ne Konsolenanwendung, aber es wurden daran ja keine Anforderungen gestellt
C#-Code
Leider nur 'ne Konsolenanwendung, aber es wurden daran ja keine Anforderungen gestellt

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PasswortGenerator { class Program { /* * ASCII CODE: * 33-126, falls alles erlaubt * 48-57 Zahlen * 97-122 Kleinbuchstaben * 65-90 Großbuchstaben * Rest Sonderzeichen */ public static string Generiere(int l, bool s, bool g, bool k, bool z) { Random rnd = new Random(); string ausgabe = ""; bool case1 = (!s && !g && !k && !z); // 0000 bool case2 = (!s && !g && !k && z); // 0001 bool case3 = (!s && !g && k && !z); // 0010 bool case4 = (!s && !g && k && z); // 0011 bool case5 = (!s && g && !k && !z); // 0100 bool case6 = (!s && g && !k && z); // 0101 bool case7 = (!s && g && k && !z); // 0110 bool case8 = (!s && g && k && z); // 0111 bool case9 = ( s && !g && !k && !z); // 1000 bool case10 = ( s && !g && !k && z); // 1001 bool case11 = ( s && !g && k && !z); // 1010 bool case12 = ( s && !g && k && z); // 1011 bool case13 = ( s && g && !k && !z); // 1100 bool case14 = ( s && g && !k && z); // 1101 bool case15 = ( s && g && k && !z); // 1110 bool case16 = ( s && g && k && z); // 1111 // nichts erlaubt if (case1) { return ""; } // nur Zahlen else if (case2) { for (int i = 0; i < l; i++) { char character = (char)rnd.Next(48, 58); ausgabe += character.ToString(); } return ausgabe; } // nur Kleinbuchstaben else if (case3) { for (int i = 0; i < l; i++) { char character = (char)rnd.Next(97, 123); ausgabe += character.ToString(); } return ausgabe; } // Kleinbuchstaben & Zahlen else if (case4) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 58; // wegen while-schleife while ((asciizahl > 57 && asciizahl < 97)) { asciizahl = rnd.Next(48, 123); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // nur Großbuchstaben else if (case5) { for (int i = 0; i < l; i++) { char character = (char)rnd.Next(65, 91); ausgabe += character.ToString(); } return ausgabe; } // Großbuchstaben & Zahlen else if (case6) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 58; // wegen while-schleife while ((asciizahl > 57 && asciizahl < 65)) { asciizahl = rnd.Next(48, 91); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Großbuchstaben & Kleinbuchstaben else if (case7) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 91; // wegen while-schleife while ((asciizahl > 90 && asciizahl < 97)) { asciizahl = rnd.Next(65, 123); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Großbuchstaben, Kleinbuchstaben & Zahlen else if (case8) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 58; // wegen while-schleife while ((asciizahl > 57 && asciizahl < 65) || (asciizahl > 90 && asciizahl < 97)) { asciizahl = rnd.Next(48, 123); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // nur Sonderzeichen else if (case9) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 57; // wegen while-schleife while ((asciizahl > 47 && asciizahl < 58) || (asciizahl > 64 && asciizahl < 91) || (asciizahl > 96 && asciizahl < 123)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen & Zahlen else if (case10) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 90; // wegen while-schleife while ((asciizahl > 64 && asciizahl < 91) || (asciizahl > 96 && asciizahl < 123)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen & Kleinbuchstaben else if (case11) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 57; // wegen while-schleife while ((asciizahl > 47 && asciizahl < 58) || (asciizahl > 64 && asciizahl < 91)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen, Kleinbuchstaben & Zahlen else if (case12) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 65; // wegen while-schleife while ((asciizahl > 64 && asciizahl < 91)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen & Großbuchstaben else if (case13) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 57; // wegen while-schleife while ((asciizahl > 47 && asciizahl < 58) || (asciizahl > 96 && asciizahl < 123)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen, Großbuchstaben & Zahlen else if (case14) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 97; // wegen while-schleife while ((asciizahl > 96 && asciizahl < 123)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Sonderzeichen, Großbuchstaben & Kleinbuchstaben else if (case15) { int asciizahl; for (int i = 0; i < l; i++) { asciizahl = 57; // wegen while-schleife while ((asciizahl > 47 && asciizahl < 58)) { asciizahl = rnd.Next(33, 127); } char character = (char)asciizahl; ausgabe += character.ToString(); } return ausgabe; } // Alles erlaubt else if (case16) { for (int i = 0; i < l; i++) { char character = (char)rnd.Next(33, 127); ausgabe += character.ToString(); } return ausgabe; } else { throw new Exception("Something went horribly wrong"); } } static void Main(string[] args) { int laenge; bool sonder = false, gross = false, klein = false, zahlen = false; char antwort; // Länge setzen do { Console.WriteLine("Wie lang soll das Passwort werden?"); } while (!int.TryParse(Console.ReadLine(), out laenge)); // Parameter setzen ANFANG Console.WriteLine("Sind Sonderzeichen erlaubt? (Y/N)"); antwort = Console.ReadLine().ToUpper()[0]; if (antwort == 'Y') { sonder = true; } Console.WriteLine("Sind Großbuchstaben erlaubt? (Y/N)"); antwort = Console.ReadLine().ToUpper()[0]; if (antwort == 'Y') { gross = true; } Console.WriteLine("Sind Kleinbuchstaben erlaubt? (Y/N)"); antwort = Console.ReadLine().ToUpper()[0]; if (antwort == 'Y') { klein = true; } Console.WriteLine("Sind Zahlen erlaubt? (Y/N)"); antwort = Console.ReadLine().ToUpper()[0]; if (antwort == 'Y') { zahlen = true; } // Parameter setzen ENDE // Passwort generieren und ausgeben Console.Clear(); Console.WriteLine("Dein Passwort: \n{0}", Generiere(laenge, sonder, gross, klein, zahlen)); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Passwortgenerator { class Program { static void Main(string[] args) { int cross = 0, entrieStart = 0. helpme = 0; string[] entries = new string[4]; entries[0] = "abcdefghijklmnopqrstuvwxyz"; entries[1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; entries[2] = "12345678901234567890135790"; entries[3] = "!§%$&/()=?*&_:;!§$%&/()=?*"; Console.WriteLine("Please enter a string of numbers. It will generate your Password: "); Console.Write("Please beware. Do not enter number greater\nthen 2.147.483.647, for it will crash\n"); string into = Console.ReadLine(); //Eingabe einer numerischen Passphrase int intoInt = Convert.ToInt32(into); //string wird in Int konvertiert Random rnd = new Random(); int rando = rnd.Next(intoInt); //Ermitteln der Zufallszahl aus der entgegengenommenen Zahl string randoStr = Convert.ToString(rando); for (int count = 0;count < randoStr.Length;count++) { //Schleife zum rrrechnen der Quersumme der Zufallszahl cross += Int32.Parse(randoStr[count].ToString()); //Durchlaufen des Strings um die einzelnen Digits aufzuaddieren. (Quersumme) } for (int i = 0;i < cross;i++) { helpme = Convert.ToInt32(randoStr.Length); Console.Write(entries[rnd.Next(0, 4)][rnd.Next(1, 25)]); Thread.Sleep(200); } Console.ReadLine(); } } }
Länger als erwartet :D
C#-Code

class PasswordGenerator { static void main(string[] args) { string kleinbuchstaben, großbuchstaben, zahlen, sonderzeichen; int length; Console.WriteLine("Bitte geben sie ihre Parameter an (y = Ja; n = Nein)"); Console.Write("Länge: "); length = Convert.ToInt32(Console.ReadLine()); Console.Write("Kleinbuchstaben: "); kleinbuchstaben = Console.ReadLine(); Console.Write("Großbuchstaben: "); großbuchstaben = Console.ReadLine(); Console.Write("Zahlen: "); zahlen = Console.ReadLine(); Console.Write("SonderZeichen: "); sonderzeichen = Console.ReadLine(); } string Shuffle(string text) { string newtext = null; Random rand = new Random(); int index = 0; while(text.Length > 0) { index = rand.Next(0, text.Length); newtext += text[index]; text = text.Remove(index, 1); } return newtext; } string BuildPassword(string kleinbuchstaben, string großbuchstaben, string zahlen, string sonderzeichen,int length) { string password = null; string Kleinbuchstaben ="abcdefghijklmnopqrstuvwxyz"; string Großbuchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string Zahlen = "123456789" string Sonderzeichen = "@!§$%&/()=?*':;"; string alphabet = null; if(kleinbuchstaben == "y") alphabet+= Kleinbuchstaben; if(großbuchstaben == "y") alphabet+= Großbuchstaben; if(zahlen == "y") alphabet+=Zahlen; if(sonderzeichen == "y") alphabet+= Sonderzeichen alphabet = Shuffle(alphabet); //to make it more random Random rand = new Random(); for(int i = 0; i < length;i++) { password+= alphabet[rand.Next(0,alphabet.Length)]; } } }
Klasse Creator, enthält die Methode / Funktionen für die Erstellung der Pwd
C#-Code
--> Ausführung
C#-Code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PasswordCreator { class Creator { public const int START_A = 65; public const int END_Z = 91; public const int START_a = 97; public const int END_z = 123; public const int START_0 = 48; public const int END_9 = 58; /// <summary> /// Methode gibt ein Array mit den Sonderzeichen aus /// </summary> /// <returns>Sonderzeichen</returns> public static string GetSpezialCharacter(int anzahl) { char[] c = { '!', '§', '$', '%', '&', '/', '(', ')', '[', ']', '=', '+', '-', '?', '#', '<', '>' }; char[] temp = new char[anzahl]; Random r = new Random(); StringBuilder b = new StringBuilder(); int n = 0; for (int i = 0; i < anzahl; i++) { n = r.Next(0, c.Length); b.Append(temp[n]); } return b.ToString(); } /// <summary> /// Methode liefert die verfügbaren Chars in einem Char Array /// </summary> /// <param name="startAscii">Start in der Ascii Tabelle</param> /// <param name="endAscii"> Ende in der Ascii Tabelle</param> /// <returns>verfügbare Zeichen</returns> public static char[] GetAvailableChars(int startAscii, int endAscii) { char[] c = new char[endAscii % startAscii]; for(int i = startAscii,j = 0; i < endAscii; i++, j++) { c[j] = Convert.ToChar(i); } return c; } /// <summary> /// Methode verbindet zwei array miteinander und gibt dieses zurück /// </summary> /// <param name="first">erstes char array</param> /// <param name="secound">zweites char array</param> /// <returns>char array</returns> public static char[] ConcatChars(char[] first, char[] secound) { char[] c = first.Concat(secound).ToArray(); return c; } /// <summary> /// Methode liefert das zufällige Passwort /// </summary> /// <param name="availableChars">verfügbare Zeichen</param> /// <param name="anzahlZeichen">anzahl der zeichen</param> /// <returns>passwort</returns> public static string GetPasswordWithoutSpezialCharacter(char[] availableChars, int anzahlZeichen) { Random r = new Random(); StringBuilder b = new StringBuilder(); int n = 0; for (int i = 0; i < anzahlZeichen; i++) { n = r.Next(0, availableChars.Length); b.Append(availableChars[n]); } return b.ToString(); } } }
--> Ausführung

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PasswordCreator { class Program { static int anzahlZeichen; static int auswahl; static void Main(string[] args) { Console.WriteLine("Bitte geben Sie die Anzahl der Zeichen ein: "); anzahlZeichen = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Bitte geben Sie an welche Zeichen verwendet werden dürfen."); Console.WriteLine("1. nur Großbuchstanben\n2. nur Kleinbuchstaben\n3. nur Zahlen\n4. nur Sonderzeichen\n5. Groß & Kleinbuchstaben\n6. Buchstaben mit Zahlen\n"); auswahl = Convert.ToInt16(Console.ReadLine()); Console.WriteLine(GetPasswort()); Console.ReadKey(); } static string GetPasswort() { switch (auswahl) { case 1: return Creator.GetPasswordWithoutSpezialCharacter(Creator.GetAvailableChars(Creator.START_A, Creator.END_Z), anzahlZeichen); case 2: return Creator.GetPasswordWithoutSpezialCharacter(Creator.GetAvailableChars(Creator.START_a, Creator.END_z), anzahlZeichen); case 3: return Creator.GetPasswordWithoutSpezialCharacter(Creator.GetAvailableChars(Creator.START_0, Creator.END_9), anzahlZeichen); case 4: return Creator.GetSpezialCharacter(anzahlZeichen); case 5: return Creator.GetPasswordWithoutSpezialCharacter(Creator.ConcatChars(Creator.GetAvailableChars(Creator.START_A, Creator.END_Z), Creator.GetAvailableChars(Creator.START_a, Creator.END_z)),anzahlZeichen); case 6: return Creator.GetPasswordWithoutSpezialCharacter(Creator.ConcatChars(Creator.ConcatChars(Creator.GetAvailableChars(Creator.START_A, Creator.END_Z), Creator.GetAvailableChars(Creator.START_a, Creator.END_z)), Creator.GetAvailableChars(Creator.START_0, Creator.END_9)), anzahlZeichen); default: Console.WriteLine("Bitte treffen Sie eine Auswahl zwischen 1 und 7"); return null; } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer71 { class Program { static void Main(string[] args) { int laenge, kleinbuchstaben, grossbuchstaben, zahlen, sonderzeichen; Console.WriteLine("PASSWORTGENERATOR"); Console.Write("Wie lang soll das Passwort sein: "); while (Int32.TryParse(Console.ReadLine(), out laenge) == false) { Console.WriteLine("Bitte geben Sie nur Zahlen ein!\n"); Console.Write("Wie lang soll das Passwort sein: "); } Console.Clear(); Console.WriteLine("Geben Sie an, was in dem {0} Zeichen langem Passwort vorkommen soll: ", laenge); Console.Write("Kleinbuchstaben: "); while (Int32.TryParse(Console.ReadLine(), out kleinbuchstaben) == false) { Console.WriteLine("Bitte geben Sie nur Zahlen ein!\n"); Console.Write("Kleinbuchstaben: "); } Console.Write("Grossbuchstaben: "); while (Int32.TryParse(Console.ReadLine(), out grossbuchstaben) == false) { Console.WriteLine("Bitte geben Sie nur Zahlen ein!\n"); Console.Write("Grossbuchstaben: "); } Console.Write("Zahlen: "); while (Int32.TryParse(Console.ReadLine(), out zahlen) == false) { Console.WriteLine("Bitte geben Sie nur Zahlen ein!\n"); Console.Write("Zahlen: "); } Console.Write("Sonderzeichen: "); while (Int32.TryParse(Console.ReadLine(), out sonderzeichen) == false) { Console.WriteLine("Bitte geben Sie nur Zahlen ein!\n"); Console.Write("Sonderzeichen: "); } if ((kleinbuchstaben + grossbuchstaben + zahlen + sonderzeichen) <= laenge) { Console.WriteLine(passwortGenerieren(laenge, kleinbuchstaben, grossbuchstaben, zahlen, sonderzeichen)); } else { Console.WriteLine("Sie haben zu viele Zeichenwünsche für die ausgewählte Passwortlänge eingegeben. Das Programm wird beendet."); } Console.ReadKey(); } static string passwortGenerieren(int länge, int kleinbuchstaben, int grossbuchstaben, int zahlen, int sonderzeichen) { Random zufall = new Random(); string passwort = ""; string ausgabe = ""; char buchstabe; int zaehler = 0; string[] sonder = new string[] { "^", "!", "§", "$", "%", "&", "/", "(", ")", "=", "?", "`", "´", "+", "*", "~", "#", ".", ",", ";", ":", "-", "_", "<", ">", "|", "°" }; while(passwort.Length < länge) { if (passwort.Length < kleinbuchstaben) { buchstabe = Convert.ToChar(zufall.Next(97, 123)); passwort += buchstabe; } else if ((passwort.Length - kleinbuchstaben) < grossbuchstaben) { buchstabe = Convert.ToChar(zufall.Next(65, 91)); passwort += buchstabe; } else if ((passwort.Length - kleinbuchstaben - grossbuchstaben) < zahlen) { passwort += zufall.Next(0, 10); } else { zaehler = zufall.Next(0, sonder.Length + 1); passwort += sonder[zaehler]; } } Console.WriteLine(passwort); for (int i = passwort.Length; i > 0; i--) { zaehler = zufall.Next(0, i); ausgabe += passwort[zaehler]; passwort = passwort.Remove(zaehler, 1); } return ausgabe; } } }

/* * Password Generator * Date: 25042018 * Author: art1808 * * Inputs: * Length * Char Upper * Char Lower * Special Char * Button Generate PW * Button Copy to Clipboard * Button Clear */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PasswordGenerator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Input public int Len { get; set; } public bool Numbers { get; set; } public bool CharUpper { get; set; } public bool CharLower { get; set; } public bool SpecialChar { get; set; } public int CurrentChecks { get; set; } //Password public string Password { get; set; } private void btnCreate_Click(object sender, EventArgs e) { if (Numbers == true || CharUpper == true || CharLower == true || SpecialChar == true) { Password = ""; CurrentChecks = 0; char[] passwordChar = new char[105]; int counter = 0; int.TryParse(numLenNum.Value.ToString(), out int len); Len = len; if (Numbers) CurrentChecks++; if (CharUpper) CurrentChecks++; if (CharLower) CurrentChecks++; if (SpecialChar) CurrentChecks++; Random rnd = new Random(); int rndNo = 0; int rndCharU = 0; int rndCharL = 0; int rndSpeChar = 0; int rndPW = 0; while (len > 0) { if (Numbers == true) { rndNo = rnd.Next(0, 10); switch (rndNo) { case 0: passwordChar[counter] = '0'; counter++; break; case 1: passwordChar[counter] = '1'; counter++; break; case 2: passwordChar[counter] = '2'; counter++; break; case 3: passwordChar[counter] = '3'; counter++; break; case 4: passwordChar[counter] = '4'; counter++; break; case 5: passwordChar[counter] = '5'; counter++; break; case 6: passwordChar[counter] = '6'; counter++; break; case 7: passwordChar[counter] = '7'; counter++; break; case 8: passwordChar[counter] = '8'; counter++; break; case 9: passwordChar[counter] = '9'; counter++; break; } } if (CharUpper == true) { rndCharU = rnd.Next(0, 26); switch (rndCharU) { case 0: passwordChar[counter] = 'A'; counter++; break; case 1: passwordChar[counter] = 'B'; counter++; break; case 2: passwordChar[counter] = 'C'; counter++; break; case 3: passwordChar[counter] = 'D'; counter++; break; case 4: passwordChar[counter] = 'E'; counter++; break; case 5: passwordChar[counter] = 'F'; counter++; break; case 6: passwordChar[counter] = 'G'; counter++; break; case 7: passwordChar[counter] = 'H'; counter++; break; case 8: passwordChar[counter] = 'I'; counter++; break; case 9: passwordChar[counter] = 'J'; counter++; break; case 10: passwordChar[counter] = 'K'; counter++; break; case 11: passwordChar[counter] = 'L'; counter++; break; case 12: passwordChar[counter] = 'M'; counter++; break; case 13: passwordChar[counter] = 'N'; counter++; break; case 14: passwordChar[counter] = 'O'; counter++; break; case 15: passwordChar[counter] = 'P'; counter++; break; case 16: passwordChar[counter] = 'Q'; counter++; break; case 17: passwordChar[counter] = 'R'; counter++; break; case 18: passwordChar[counter] = 'S'; counter++; break; case 19: passwordChar[counter] = 'T'; counter++; break; case 20: passwordChar[counter] = 'U'; counter++; break; case 21: passwordChar[counter] = 'V'; counter++; break; case 22: passwordChar[counter] = 'W'; counter++; break; case 23: passwordChar[counter] = 'X'; counter++; break; case 24: passwordChar[counter] = 'Y'; counter++; break; case 25: passwordChar[counter] = 'Z'; counter++; break; } } if (CharLower == true) { rndCharL = rnd.Next(0, 26); switch (rndCharU) { case 0: passwordChar[counter] = 'a'; counter++; break; case 1: passwordChar[counter] = 'b'; counter++; break; case 2: passwordChar[counter] = 'c'; counter++; break; case 3: passwordChar[counter] = 'd'; counter++; break; case 4: passwordChar[counter] = 'e'; counter++; break; case 5: passwordChar[counter] = 'f'; counter++; break; case 6: passwordChar[counter] = 'g'; counter++; break; case 7: passwordChar[counter] = 'h'; counter++; break; case 8: passwordChar[counter] = 'i'; counter++; break; case 9: passwordChar[counter] = 'j'; counter++; break; case 10: passwordChar[counter] = 'k'; counter++; break; case 11: passwordChar[counter] = 'l'; counter++; break; case 12: passwordChar[counter] = 'm'; counter++; break; case 13: passwordChar[counter] = 'n'; counter++; break; case 14: passwordChar[counter] = 'o'; counter++; break; case 15: passwordChar[counter] = 'p'; counter++; break; case 16: passwordChar[counter] = 'q'; counter++; break; case 17: passwordChar[counter] = 'r'; counter++; break; case 18: passwordChar[counter] = 's'; counter++; break; case 19: passwordChar[counter] = 't'; counter++; break; case 20: passwordChar[counter] = 'u'; counter++; break; case 21: passwordChar[counter] = 'v'; counter++; break; case 22: passwordChar[counter] = 'w'; counter++; break; case 23: passwordChar[counter] = 'x'; counter++; break; case 24: passwordChar[counter] = 'y'; counter++; break; case 25: passwordChar[counter] = 'z'; counter++; break; } } if (SpecialChar == true) { rndSpeChar = rnd.Next(0, 11); switch (rndSpeChar) { case 0: passwordChar[counter] = '?'; counter++; break; case 1: passwordChar[counter] = '!'; counter++; break; case 2: passwordChar[counter] = '$'; counter++; break; case 3: passwordChar[counter] = '%'; counter++; break; case 4: passwordChar[counter] = '='; counter++; break; case 5: passwordChar[counter] = '*'; counter++; break; case 6: passwordChar[counter] = '+'; counter++; break; case 7: passwordChar[counter] = ';'; counter++; break; case 8: passwordChar[counter] = ':'; counter++; break; case 9: passwordChar[counter] = '-'; counter++; break; case 10: passwordChar[counter] = '_'; counter++; break; } } len -= CurrentChecks; } len = Len; while (len > 0) { rndPW = rnd.Next(0, Len); Password += passwordChar[rndPW].ToString(); len -= 1; } txtbPassword.Text = Password; } else MessageBox.Show("Keine Wertebereiche ausgewählt"); } private void cbxNumbers_CheckedChanged(object sender, EventArgs e) { if (cbxNumbers.Checked == true) Numbers = true; else Numbers = false; } private void cbxCharactersUpper_CheckedChanged(object sender, EventArgs e) { if (cbxCharactersUpper.Checked == true) CharUpper = true; else CharUpper = false; } private void cbxCharactersLower_CheckedChanged(object sender, EventArgs e) { if (cbxCharactersLower.Checked == true) CharLower = true; else CharLower = false; } private void cbxSpecialCharacter_CheckedChanged(object sender, EventArgs e) { if (cbxSpecialCharacter.Checked == true) SpecialChar = true; else SpecialChar = false; } private void btnClear_Click(object sender, EventArgs e) { txtbPassword.Clear(); } private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetText(txtbPassword.Text); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Passwort_Generrator { class Program { static void Main(string[] args) { Console.Title = "Passwort Generrator"; string ziffern = "0AaBbCc1DdEeFf2GgHhIi3JjKkLl4MmNnOo5PpQqRr6SsTtUu7VvWwXx8YyZz@9!-/"; Console.WriteLine("Wie viele stellen soll ihr Passwort beinhalten?"); Console.Write("\n="); int stellen = Convert.ToInt32(Console.ReadLine()); string passwort = ""; Random Rnd = new Random(); for (int i = 0; i < stellen; i++) { int x = Rnd.Next(66); passwort += ziffern[x]; } Console.Clear(); Console.WriteLine("Ihr Passwort ist = " + passwort); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Passwort_Generrator_mit_Parametern { class Program { static void Main(string[] args) { Console.Title = "Passwort Generrator"; Console.WriteLine("Welche länge soll ihr Passwort haben?\n"); string passwort = ""; string stufe1 = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyz"; string stufe2 = "Aa1Bb2Cc2Dd3Ee3Ff4Gg4Hh5Ii5Jj6Kk6Ll7Mm7Nn8Oo8Pp9Qq9Rr0Ss0Tt1U2u3V4v5W6w7X8x9Y0y1z"; string stufe3 = "+-!#/%&$_@"; Random Rnd = new Random(); int Random; int länge = Convert.ToInt32(Console.ReadLine()); Console.Clear(); Console.WriteLine("Welche Sicherheitsstufe soll ihr Passwort bekommen?\n"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("1. Stufe = nur Groß und Klein Buchstaben"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("2. Stufe = Groß und Kleine Buchstaben + min. 1. Zahl"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("3. Stufe = Groß und Kliene Buchstaben + min. 1. Zahl + min. 1. Zusatzzeichen\n"); Console.ResetColor(); int Sicherheitsstufe = Convert.ToInt32(Console.ReadLine()); Console.Clear(); bool key = true; while (key) { if (Sicherheitsstufe == 1) { for(int i = 0; i < länge; i++) { Random = Rnd.Next(1, 51); passwort += stufe1[Random]; } key = false; } else if (Sicherheitsstufe == 2) { for (int i = 0; i < länge; i++) { Random = Rnd.Next(1, 81); passwort += stufe2[Random]; } key = false; } else if (Sicherheitsstufe == 3) { Random = Rnd.Next(1,10); passwort += stufe3[Random]; for (int i = 1; i < länge; i++) { Random = Rnd.Next(1, 81); passwort += stufe2[Random]; } key = false; } } Console.WriteLine("Ihr Passwort mit der länge von "+ länge +"\nund der Sicherheitsstufe "+Sicherheitsstufe+". lautet:\n\n"); Console.ResetColor(); Console.WriteLine(passwort); Console.ReadLine(); } } }