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
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine(CreateRandomPassword(10));
            Console.WriteLine(CreateRandomSalt());
            Console.Read();
        }

        public static string CreateRandomPassword(int PasswordLength)
        {
            String _allowedChars = "abcdefghijkmnGHJKLMNOPQRSTUVWXYZ234567opqrstuoM4jBpAw39Qoo3aSGyLiYnFqi5wYSpL2vwxyzABCDEF89";
            Byte[] randomBytes = new Byte[PasswordLength];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(randomBytes);
            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();
            rng.GetBytes(_saltBytes);

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

Best Luck,
Megha
}

By Perry    Popularity  (2663 Views)