字符置乱(C# 和 Javascript)

发布于 2024-11-02 08:43:38 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁我热情 2024-11-09 08:43:38

如果您确实想打乱字符,可以将 random 与固定种子一起使用:

string input = "hello";
char[] chars = input.ToArray();
Random r = new Random(2011); // Random has a fixed seed, so it will always return same numbers (within same input)
for (int i = 0 ; i < chars.Length ; i++)
{
    int randomIndex = r.Next(0, chars.Length);
    char temp = chars[randomIndex];
    chars[randomIndex] = chars[i];
    chars[i] = temp;
}
return new string(chars);

You can use random with fixed seed if you really want to scramble characters:

string input = "hello";
char[] chars = input.ToArray();
Random r = new Random(2011); // Random has a fixed seed, so it will always return same numbers (within same input)
for (int i = 0 ; i < chars.Length ; i++)
{
    int randomIndex = r.Next(0, chars.Length);
    char temp = chars[randomIndex];
    chars[randomIndex] = chars[i];
    chars[i] = temp;
}
return new string(chars);
柠檬 2024-11-09 08:43:38

Although I don't really agree about what you were trying to do, here's a link to MD5 javascript library (assuming what you're trying to do is some kind of encryption). As for the C# part, it's a built in feature.

束缚m 2024-11-09 08:43:38

您可以使用任何内置的 .NET 类来生成随机数并使用它们来打乱您的字符串。对于所有后续的打乱尝试,您可以使用第一次打乱操作的结果。这假设第一次调用的结果存储在某处。

You can use any of the built in .NET classes to generate random numbers and use these to scramble your string. For all the subsequent scramble attempts you can use the result from the first scramble operation. This assumes that the result from the first call is stored somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文