如何生成具有给定长度的密码学强度随机字符串

huangapple go评论67阅读模式
英文:

How can I generate cryptographically strong random strings with a given length

问题

需要生成指定长度的具有密码强度的随机字母数字字符串,只使用以下字符:

  • A-Z
  • a-z
  • 0-9

在C#中是否有实现这个功能的方法?

英文:

I need to generate cryptographically strong random alphanumeric strings with a specified length, only using the following characters.

  • A-Z
  • a-z
  • 0-9

Is there a way to accomplish this in C#?

答案1

得分: 3

你可以使用 class RandomNumberGenerator 来生成具有密码学安全性的随机数,以执行这项任务,例如:

string allowed = "ABCDEFGHIJKLMONOPQRSTUVWXYZabcdefghijklmonopqrstuvwxyz0123456789";
int strlen = 10; // 或其他值
char[] randomChars = new char[strlen];

for (int i = 0; i < strlen; i++)
{
    randomChars[i] = allowed[RandomNumberGenerator.GetInt32(0, allowed.Length)];
}

string result = new string(randomChars);

Console.WriteLine(result);
英文:

You can use class RandomNumberGenerator to generate cryptographically-secure random numbers to do this, for example:

string allowed = &quot;ABCDEFGHIJKLMONOPQRSTUVWXYZabcdefghijklmonopqrstuvwxyz0123456789&quot;;
int strlen = 10; // Or whatever
char[] randomChars = new char[strlen];

for (int i = 0; i &lt; strlen; i++)
{
    randomChars[i] = allowed[RandomNumberGenerator.GetInt32(0, allowed.Length)];
}

string result = new string(randomChars);

Console.WriteLine(result);

答案2

得分: -3

int randomArrayLength = 256;
char[] characters = {
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

int numberOfChar = characters.Length;

Random rand = new Random();
string results = string.Join("", Enumerable.Range(0, randomArrayLength).Select(x => characters[rand.Next(numberOfChar)]));
英文:

Try following :

<!-- begin snippet: js hide: false console: true babel: false -->

            int randomArrayLength = 256;
            char[] characters = {
                &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;,
                &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;,
                &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;,
                &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;,
                &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;};

            int numberOfChar = characters.Length;

            Random rand = new Random();
            string results = string.Join(&quot;&quot;, Enumerable.Range(0, randomArrayLength).Select(x =&gt; characters[rand.Next(numberOfChar)]));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月10日 23:25:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定