Loading color scheme

[Source Code] C# True Random Password Generator

password

If you need to generate a bulk amount of passwords you can use one of the widely available websites, use a console generator program, or if you don't trust anyone, you can quickly write your own program. Below is the source code of a True Random password generator written entirely in C#. Compared to similar programs available on the net, which use clock-dependent C# System.Random Number Generator, this code sample uses RNGCryptoServiceProvider which is far more secure and provides unique numbers over a long period of time. Even if you won't notice the difference in generating 1-2 passwords, the RNGCryptoServiceProvider will have more even distribution for thousands of iterations.

If you are looking for Online Strong Random Password Generator, click here.

Let's get closer to to the code. The user enters the number of generated passwords and the length of passwords. Also, the available character sets are specified inside the code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace StrongPasswordGen
{
    class Program
    {
        static RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();

        static void Main(string[] args)
        {

            int PasswordAmount = 0;
            int PasswordLength = 0;

            string CapitalLetters = "QWERTYUIOPASDFGHJKLZXCVBNM";
            string SmallLetters = "qwertyuiopasdfghjklzxcvbnm";
            string Digits = "0123456789";
            string SpecialCharacters = "!@#$%^&*()-_=+<,>.";
            string AllChar = CapitalLetters + SmallLetters + Digits + SpecialCharacters;

            

            Console.WriteLine("\nHow many passwords should be generated?:");
            PasswordAmount = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the password length (chars):");
            PasswordLength = int.Parse(Console.ReadLine());

            string[] AllPasswords = new string[PasswordAmount];


            for (int i = 0; i < PasswordAmount; i++)
            {
                StringBuilder sb = new StringBuilder();
                for (int n = 0; n < PasswordLength; n++)
                {
                    sb = sb.Append(GenerateChar(AllChar));
                }

                AllPasswords[i] = sb.ToString();
            }

            Console.WriteLine("Generated passwords:");

            foreach (string singlePassword in AllPasswords)
            {
                Console.WriteLine(singlePassword);
            }


        }

        private static char GenerateChar(string availableChars)
        {
            var byteArray = new byte[1];
            char c;
            do
            {
                provider.GetBytes(byteArray);
                c = (char)byteArray[0];

            } while (!availableChars.Any(x => x == c));
                
            return c;
        }
    }

}

Used materials from RNGCryptoServiceProvider Class (MSDN)

Get all interesting articles to your inbox
Please wait