Sometimes it’s helpful to simply have a random password.
In this small group of code you can define what parameters to use, I have taken the liberty to remove some of the characters that are usually mistaken for a different character like an upper case I and the number 1.
private static int DEFAULT_MAX_PASSWORD_LENGTH = 10; private static int DEFAULT_MIN_PASSWORD_LENGTH = 8; private static string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz"; private static string PASSWORD_CHARS_NUMERIC = "23456789"; private static string PASSWORD_CHARS_SPECIAL = "*$+?&!%"; private static string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
Full Source Code
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; using System.Security.Cryptography; namespace Project1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show(RandomPassword.Generate()); } } public class RandomPassword { private static int DEFAULT_MAX_PASSWORD_LENGTH = 10; private static int DEFAULT_MIN_PASSWORD_LENGTH = 8; private static string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz"; private static string PASSWORD_CHARS_NUMERIC = "23456789"; private static string PASSWORD_CHARS_SPECIAL = "*$+?&!%"; private static string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ"; public static string Generate() { return Generate(DEFAULT_MIN_PASSWORD_LENGTH, DEFAULT_MAX_PASSWORD_LENGTH); } public static string Generate(int length) { return Generate(length, length); } public static string Generate(int minLength, int maxLength) { if (((minLength <= 0) || (maxLength <= 0)) || (minLength > maxLength)) { return null; } char[][] chArray = new char[][] { PASSWORD_CHARS_LCASE.ToCharArray(), PASSWORD_CHARS_UCASE.ToCharArray(), PASSWORD_CHARS_NUMERIC.ToCharArray(), PASSWORD_CHARS_SPECIAL.ToCharArray() }; int[] numArray = new int[chArray.Length]; for (int i = 0; i < numArray.Length; i++) { numArray[i] = chArray[i].Length; } int[] numArray2 = new int[chArray.Length]; for (int j = 0; j < numArray2.Length; j++) { numArray2[j] = j; } byte[] data = new byte[4]; new RNGCryptoServiceProvider().GetBytes(data); int seed = ((((data[0] & 0x7f) << 0x18) | (data[1] << 0x10)) | (data[2] << 8)) | data[3]; Random random = new Random(seed); char[] chArray2 = null; if (minLength < maxLength) { chArray2 = new char[random.Next(minLength, maxLength + 1)]; } else { chArray2 = new char[minLength]; } int maxValue = numArray2.Length - 1; for (int k = 0; k < chArray2.Length; k++) { int num4; int num6; if (maxValue == 0) { num6 = 0; } else { num6 = random.Next(0, maxValue); } int index = numArray2[num6]; int num7 = numArray[index] - 1; if (num7 == 0) { num4 = 0; } else { num4 = random.Next(0, num7 + 1); } chArray2[k] = chArray[index][num4]; if (num7 == 0) { numArray[index] = chArray[index].Length; } else { if (num7 != num4) { char ch = chArray[index][num7]; chArray[index][num7] = chArray[index][num4]; chArray[index][num4] = ch; } numArray[index]--; } if (maxValue == 0) { maxValue = numArray2.Length - 1; } else { if (maxValue != num6) { int num10 = numArray2[maxValue]; numArray2[maxValue] = numArray2[num6]; numArray2[num6] = num10; } maxValue--; } } return new string(chArray2); } } }
Original Resource Unknown
Last Updated on April 9, 2019