using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Ejercicio_04_password { class Program { static void Main(string[] args) { List contrasenyas = new List(); Console.WriteLine("Numero de contraseñas a generar ( >= 1 ): "); int numPw = Int32.Parse(Console.ReadLine()); if (numPw < 1) { numPw = 1; } Console.WriteLine("Longitud de las contraseñas a generar ( >= 8 ): "); int lonPw = Int32.Parse(Console.ReadLine()); if (lonPw < 8) { lonPw = 8;} Console.WriteLine("Generando contraseñas..."); for (int i = 0; i < numPw; i++) { Password pw = new Password(lonPw); contrasenyas.Add(pw); } foreach (Password pw in contrasenyas) { Console.WriteLine("{0} {1}", pw.Contrasenya, pw.esFuerte()); } Console.ReadLine(); } } class Password { public int Longitud { get; set; } public string Contrasenya { get; set; } public Password() { this.Longitud = 8; /* DEFAULT*/ this.Contrasenya = generarPassword(); } public Password(int longitud) { this.Longitud = longitud; this.Contrasenya = generarPassword(); } public bool esFuerte() { bool forte = false; int mays = 0; int mins = 0; int nums = 0; for (int i = 0; i < this.Contrasenya.Length; i++) { if (char.IsUpper(this.Contrasenya[i])) mays++; if (char.IsLower(this.Contrasenya[i])) mins++; if (char.IsNumber(this.Contrasenya[i])) nums++; } //Console.WriteLine("{0} mayusculas, {1} minusculas, {2} numeros", mays, mins, nums); if (mays >= 2 && mins >= 1 && nums >= 5) { forte = true; } return forte; } private string generarPassword() { string validos = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?ABCDEFGHIJKLMNOPQRSTUVWXYZ&"; Thread.Sleep(16); //TODO we should be using RandomNumberGenerator instead Random aleatorio = new Random(); string pw=""; for (int i = 0; i < this.Longitud; i++) { int num = aleatorio.Next(validos.Length); char c = validos[num]; pw += c; } //Console.WriteLine(pw); return pw; } } }