Create random Password and hash Salt

When we generates the some signature to the some file like XML or generate unique key for any reason, to help reduce the risk of dictionary attacks, the code that appends to generated random bytes is called salt to the original plain text before generating hashes. Please keep in mind that salt can only help against prebuilt dictionaries. If an intruder gets access to your system and uses a brute force attack, salt/randome password bytes will not provide much value.

The output will be in following form:
1. Password length given 10

Random Password: aSLuf2kRoo
Random Salt: 168924129

2. Password length given 15.

Random Password: pkPySZpYbrapurs
Random Salt: 1174425405

Complete C# Code:

using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
using System.IO;

namespace HashPassword
    class TestApplication
        static void Main(string[] args)

        public static string CreateRandomPassword(int PasswordLength)
            String _allowedChars = "abcdefghijkmnGHJKLMNOPQRSTUVWXYZ234567opqrstuoM4jBpAw39Qoo3aSGyLiYnFqi5wYSpL2vwxyzABCDEF89";
            Byte[] randomBytes = new Byte[PasswordLength];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            char[] chars = new char[PasswordLength];
            int allowedCharCount = _allowedChars.Length;

            for (int i = 0; i < PasswordLength; i++)
                chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];

            return new string(chars);

        public static int CreateRandomSalt()
            Byte[] _saltBytes = new Byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            return ((((int)_saltBytes[0]) << 24) + (((int)_saltBytes[1]) << 16) +
              (((int)_saltBytes[2]) << 8) + ((int)_saltBytes[3]));

Best Luck,

By Perry    Popularity  (3039 Views)