简体   繁体   English

检查字符“完整性”的好方法

[英]A good way to check a chars 'integrity'

I'm working on making my own password generator;我正在制作自己的密码生成器; and I want it to create passwords that contain numbers, lowercase-letters, and uppercase-letters.我希望它创建包含数字、小写字母和大写字母的密码。

I am writing the program in C#, using Visual Studio.我正在使用 Visual Studio 在 C# 中编写程序。
Say I use the code below:假设我使用下面的代码:

Random Rand = new Random();

Char C = Convert.ToChar(Rand.Next(0x30, 0x5A));

How could I make my program check whether (C) is equal to a number or a uppercase-letter?如何让我的程序检查 (C) 是否等于数字或大写字母?

Use Char.IsDigit (for decimal digits), or Char.IsNumber and Char.IsUpper .使用Char.IsDigit (用于十进制数字)或Char.IsNumberChar.IsUpper

These are all static methods defined on the Char structure.这些都是在Char结构上定义的 static 方法。


An example, as requested:一个例子,根据要求:

if(Char.IsNumber(C))
{
  // Character is marked as a unicode number
}

you can check if:您可以检查是否:

int ordinal = (int)C;
if(( ordinal >= 65 && ordinal <= 90 ) || (ordinal >= 48 && ordinal <= 57))
{
Console.WriteLine( C + " is an uppercase A-Z or 0-9!" );
}

Probably just use Char.IsNumber and Char.IsUpper methods: see: http://msdn.microsoft.com/en-us/library/9s91f3by.aspx http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx可能只使用 Char.IsNumber 和 Char.IsUpper 方法:参见: http://msdn.microsoft.com/en-us/library/9s91f3by.aspx http://msdn.microsoft.com/en-us/library/yk2b3 .aspx

if (C.IsNumber || C.IsUpper) { // password code! if (C.IsNumber || C.IsUpper) { // 密码! } }

(don't write your own password generator, unless it's just for fun though). (不要编写自己的密码生成器,除非只是为了好玩)。

A better (and standard) way, is to create an 'alphabet' string of allowable characters and then generate a random index into the string, to select an allowable character.更好(和标准)的方法是创建一个允许字符的“字母”字符串,然后在字符串中生成一个随机索引,select 是一个允许的字符。

Much better than first generating a random character and then checking if it is allowable.比首先生成一个随机字符然后检查它是否允许要好得多。

"0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Note the removed lowercase 'l' (looks like 1).请注意删除的小写“l”(看起来像 1)。 You should remove any others such as zero versus 'O' possibly...or whatever your password rules allow)您应该删除任何其他内容,例如零与“O”可能...或您的密码规则允许的任何内容)

Here's an example of using this technique to generate random strings:以下是使用此技术生成随机字符串的示例:

public static class RandomUtils
{
    private static readonly Random random = new Random();

    public static string GenerateRandomAlphaNumericString(int length)
    {
        const string alpha = 
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        return GenerateRandomString(length, alpha);
    }

    public static string GenerateRandomString(int length, string alphabet)
    {
        int maxlen = alphabet.Length;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++)
        {
            sb.Append(alphabet[random.Next(0, maxlen)]);
        }

        return sb.ToString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM