简体   繁体   English

字符加扰(C#和Javascript)

[英]Chars scrambling (C# and Javascript)

I need to scramble chars in word. 我需要用字符来打乱字符。 And this scrambling must not be random. 而且这种加扰不能是随机的。 In other words, each time of scrambling (on the same word) the new result must be equal to last scramble result (on the same word). 换句话说,每次加扰(在同一单词上)时,新结果必须等于上一次加扰结果(在同一单词上)。 Very simple example is XOR. 非常简单的示例是XOR。 But XOR is very easy to decode and I need something more strength. 但是XOR非常容易解码,我需要更多的优势。 Could you recommend library for such purpose that identically works on C# and Javascript? 您是否可以为此目的推荐可在C#和Javascript上完全相同的库?

Thank you for any advice!:) 谢谢您的任何建议!:)

You can use random with fixed seed if you really want to scramble characters: 如果您确实想打乱字符,则可以使用带有固定种子的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);

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). 尽管我不太同意您要执行的操作, 但这是MD5 javascript库的链接 (假设您要执行的操作是某种加密)。 As for the C# part, it's a built in feature . 至于C#部分, 它是一个内置功能

You can use any of the built in .NET classes to generate random numbers and use these to scramble your string. 您可以使用任何内置的.NET类来生成随机数,并使用它们来加扰您的字符串。 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. 假设第一次调用的结果存储在某个地方。

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

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