在 c# 中生成随机字母数字字符串
本教程将讨论在 c# 中生成随机字母数字字符串的方法。
在 c# 中使用 random
类生成随机的字母数字字符串
random
类在 c# 中生成随机数。random.next()
方法生成一个随机整数值。我们可以声明一个包含所有字母数字字符的常量字符串变量,并根据 random.next()
方法生成的索引从字符串变量中选择字符。
以下代码示例向我们展示了如何使用 c# 中的 random
类生成随机的字母数字字符串。
using system;
using system.linq;
namespace random_alphanumeric_strings {
class program {
static void main(string[] args) {
var characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789";
var charsarr = new char[8];
var random = new random();
for (int i = 0; i < charsarr.length; i ) {
charsarr[i] = characters[random.next(characters.length)];
}
var resultstring = new string(charsarr);
console.writeline(resultstring);
}
}
}
输出:
vl8m6bnr
我们在 c# 中使用 random
类生成了一个长度为 8 个字符的随机字母数字字符串。然后,我们初始化了包含所有所需字母数字字符的字符串 characters
。我们创建了字符数组 charsarr
,长度为 8
个字符。然后,我们初始化了 random
类 random
的实例。我们遍历 charsarr
数组,并每次将字符从 charsarr
的 characters
字符串中随机保存到索引中。然后,我们将字符数组 charsarr
转换为字符串变量 resultstring
,并显示结果。
在 c# 中使用 linq 方法生成随机的字母数字字符串
对 c# 中的数据结构执行查询功能。我们可以将 linq 与 random
类一起使用,以改善先前方法的性能。下面的代码示例向我们展示了如何使用 c# 中的 linq 方法生成随机的字母数字字符串。
using system;
using system.linq;
namespace random_alphanumeric_strings {
class program {
private static random random = new random();
public static string method2(int length) {
const string characters = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(
enumerable.repeat(characters, length).select(s => s[random.next(s.length)]).toarray());
}
static void main(string[] args) {
string finalstring = method2(8);
console.writeline(finalstring);
}
}
}
输出:
sxuxk2p8
在上面的代码中,我们使用 c# 中的 linq 和 random
类创建了一个随机的字母数字字符串。该方法比上面讨论的方法更有效。
在 c# 中使用 rngcryptoserviceprovider
类生成随机的字母数字字符串
不建议使用上面讨论的方法来生成密码,因为它们并不是真正随机的并且遵循线性模式。对于安全密码,应使用 rngcryptoserviceprovider
类。rngcryptoserviceprovider
类在 c# 中生成加密随机数。rngcryptoserviceprovider
类的 getbytes()
方法用随机值填充字节数组。我们可以将 getbytes()
方法与 [convert.tobase64string()
方法一起使用,以从填充的字节数组中获取字符串。以下代码示例向我们展示了如何使用 c# 中的 rngcryptoserviceprovider
类生成安全的随机字母数字字符串。
using system;
using system.linq;
using system.security.cryptography;
namespace random_alphanumeric_strings {
class program {
static string method3(int length) {
using (var crypto = new rngcryptoserviceprovider()) {
var bits = (length * 6);
var byte_size = ((bits 7) / 8);
var bytesarray = new byte[byte_size];
crypto.getbytes(bytesarray);
return convert.tobase64string(bytesarray);
}
}
static void main(string[] args) {
string finalstring = method3(8);
console.writeline(finalstring);
}
}
}
输出:
jgc42ug3
在上面的代码中,我们使用 c# 中的 rngcryptoserviceprovider
类生成了一个由 8 个字符组成的安全随机字母数字字符串。如果我们要生成随机密码,则建议使用此方法,因为它比前两种方法相对安全且不可预测。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 c# 中,有两种主要方法可用于将 list
转换为字符串变量,linq 方法和 string.join()函数。
在 c# 中发出 http post web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 c# 中,可以使用 3 种主要方法来发出 http post web 请求:webclient 类,httpwebrequest 类和 httpclient 类。本教程将讨论在 c# 中发出 http post web 请求的方法。使用 c# 中的 webclient 类发出 http post web 请求
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
process 类可用于在 c# 中运行命令提示符命令。在 c# 中使用 process.start() 函数运行命令提示符命令
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 c# 中调整图像的大小,bitmap 类构造函数和 graphics.drawimage()函数。在本教程中,我们将讨论在c#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 c# 中的图片,webclient.downloadfile()函数,bitmap 类和 image.fromstream()函数。在 c# 中使用 webclient 类下载图片 webclient 类提供了用于向 c# 中的 url 发送数据和从 url 接收数据
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 stopwatch 类来计算 c# 中的经过时间。使用 c# 中的秒表类计算经过时间 stopwatch 类在 c# 中准确测量经过的时间。
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 c# 中程序的可执行路径,即 assembly 类,appdomain 类和 path 类。本教程将介绍获取 c# 代码的可执行路径的方法。使用 c# 中的 assembly 类获取可执行路径