2

我已经有了这种方法来扰乱我的文本,但我不知道如何解读它?

        string input = "https://www.google.de/";
        char[] chars = input.ToArray();
        Random r = new Random(259);
        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;
        }
        new string(chars);

        Console.WriteLine(chars);
        Console.ReadKey();

我不擅长这类有数字和计算的事情,如果有人能帮助我,这样的事情会很好。

我已经搜索了函数,但找不到一个我理解的例子。

//编辑:

- 我如何制作一个扰乱或随机播放函数,用特定数字随机播放文本,以便每次都以相同的方式对文本进行扰乱?

- 在那之后我怎么能解开它?

4

2 回答 2

9

只需执行您已经反向执行的相同操作(您需要以相反的顺序使用来自同一种子的相同随机数,因此首先将它们放在列表中):

        string input = "https://www.google.de/";
        char[] chars = input.ToArray();
        Random r = new Random(259);
        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;
        }
        string scrambled = new string(chars);

        Console.WriteLine(chars);
        Console.ReadKey();

        r = new Random(259);
        char[] scramChars = scrambled.ToArray();
        List<int> swaps = new List<int>();
        for (int i = 0; i < scramChars.Length; i++)
        {
            swaps.Add(r.Next(0, scramChars.Length));
        }
        for (int i = scramChars.Length - 1; i >= 0; i--)
        {
            char temp = scramChars[swaps[i]];
            scramChars[swaps[i]] = scramChars[i];
            scramChars[i] = temp;
        }

        string unscrambled = new string(scramChars);

        Console.WriteLine(scramChars);
于 2016-11-08T23:52:53.123 回答
0

我过去曾使用自己的加扰/解扰方法以另一种方式进行,但最近发现一些字符串只是不加扰我没有使用交换,而是一种编码形式。我找到了这个线程并采用了最后提供的解决方案,并通过将种子编码到加扰字符串中使其更有用,因此不需要对种子进行解码。这允许在数据库中使用加扰字符串以使其易于阅读。除非您知道种子在字符串中的嵌入位置(当前是最后一个字符,但如果需要,您可以将其设为字符串的任何部分)

    /// <summary>
    /// Mix up a string (re-order character positions) using a random number set based from a seed value
    /// </summary>
    /// <param name="StringToScramble"></param>
    /// <returns>Scrambled String with seed encoded</returns>
    public static string RandomizeString(string StringToScramble)
    {
        int seed = RandomNumber(0, 250);  // Get a random number (will be encoded into string , this will be the seed
        Random r = new Random(seed);
        char[] chars = StringToScramble.ToArray();

        for (int i = 0; i < StringToScramble.Length; i++)
        {
            int randomIndex = r.Next(0, StringToScramble.Length);   // Get the next random number from the sequence
            Debug.Print(randomIndex.ToString());
            char temp = chars[randomIndex]; // Copy the character value
            // Swap them around
            chars[randomIndex] = chars[i];
            chars[i] = temp;

        }
        // Add the seed            
        return new string(chars) + seed.ToString("X").PadLeft(2, '0');
    }
    /// <summary>Unscramble a string that was previously scrambled </summary>
    /// <param name="ScrambledString">String to Unscramble</param>
    /// /// <returns>Unscrambled String</returns>
    public static string UnRandomizeString(string ScrambledString)
    {
        try
        { 
            // Get the last character from the string as this is the random number seed
            int seed = int.Parse(ScrambledString.Substring(ScrambledString.Length - 2), System.Globalization.NumberStyles.HexNumber);              
            Random r = new Random(seed);
            char[] scramChars = ScrambledString.Substring(0, ScrambledString.Length - 2).ToArray();
            List<int> swaps = new List<int>();
            for (int i = 0; i < scramChars.Length ; i++)
            {
                int randomIndex = r.Next(0, scramChars.Length );
                swaps.Add(randomIndex);
            }
            for (int i = scramChars.Length - 1; i >= 0; i--)
            {
                char temp = scramChars[swaps[i]];
                scramChars[swaps[i]] = scramChars[i];
                scramChars[i] = temp;
            }

            return new string(scramChars); 
            
        }

        catch (System.Exception ex)
        {
            return "";
        }
    }
于 2022-02-18T10:26:12.650 回答