Detailed introduction to the code example of C# batch generating random passwords that must contain numbers and letters and be encrypted with an encryption algorithm.

黄舟
Release: 2017-03-29 11:25:10
Original
1811 people have browsed it

This article mainly introducesC# Random passwords generated in batches must contain numbers and letters and be encrypted with an encryption algorithm. Friends in need can refer to the following

Requirements:

Password must contain numbers and letters

Ideas:

1. List numbers and characters . Composition String: chars

2. Use randrom.Next(int i) to return a non-negative random number that is less than the specified maximum value.

3. Randomly select a random number a that is not less than the length of chars, and select the a-th character of the string chars.

4.Loop 8 times to get the 8-digit password

5. Loop N times to get the password in batches.

The code is implemented as followsMainFunction:

static void Main(string[] args)
    {
      string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      Random randrom = new Random((int)DateTime.Now.Ticks);
      string path1 = @"C:\Users\lenovo\Desktop\pws.txt";
      for (int j = 0; j < 10000;j++ )
      {
        string str = "";
        for (int i = 0; i < 8; i++)
        {
          str += chars[randrom.Next(chars.Length)];//randrom.Next(int i)返回一个小于所指定最大值的非负随机数
        }
        if (IsNumber(str))//判断是否全是数字
          continue;
        if (IsLetter(str))//判断是否全是字母
          continue;
        File.AppendAllText(path1, str);
        string pws = Md5(str,32);//MD5加密
        File.AppendAllText(path1, "," + pws + "\r\n");
      }
      Console.WriteLine("ok");
      Console.Read();
    }
Copy after login

Use the String.trim function skillfully to determine whether it is all numbers and all letters .

Description: string.trim Removes leading and trailing whitespace characters from the String object.

Returns: A copy of the string with all whitespace characters removed from the beginning and end of the string.

There is a overload: string.Trim(params char[] trimChars)

//Remove from the current System.string objectAll leading and trailing matches of a set of characters specified in array

trimChars: Array of characters to remove

The method implements the following code:

 //判断是否全是数字
    static bool IsNumber(string str)
    {
      if (str.Trim("0123456789".ToCharArray()) == "")
        return true;
      return false;
    }
    //判断是否全是字母
    static bool IsLetter(string str)
    {
      if (str.Trim("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray()) == "")
        return true;
      return false;
    }
Copy after login

Use MD5 encryption, the algorithm code is implemented as follows:

/// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="str">加密字元</param>
    /// <param name="code">加密位数16/32</param>
    /// <returns></returns>
    public static string Md5(string str, int code)
    {
      string strEncrypt = string.Empty;
      MD5 md5 = new MD5CryptoServiceProvider();
      byte[] fromData = Encoding.GetEncoding("GB2312").GetBytes(str);
      byte[] targetData = md5.ComputeHash(fromData);
      for (int i = 0; i < targetData.Length; i++)
      {
        strEncrypt += targetData[i].ToString("X2");
      }
      if (code == 16)
      {
        strEncrypt = strEncrypt.Substring(8, 16);
      }
      return strEncrypt;
    }
Copy after login

Generate batch passwords, and the encrypted password is as shown below:

The above is the detailed content of Detailed introduction to the code example of C# batch generating random passwords that must contain numbers and letters and be encrypted with an encryption algorithm.. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template