【问题标题】:C# Scramble & Descramble TextC# 加扰和解扰文本
【发布时间】:2017-03-22 17:59:50
【问题描述】:

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

        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();

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

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

//编辑:

-我怎样才能创建一个加扰或随机函数,用特定数字随机播放文本,以便每次都以相同的方式加扰文本?

-在那之后我该如何解开它?

【问题讨论】:

  • 您不能只是从空中提取随机字符,然后期望能够撤消操作。您必须设计一种特定的方式来编码(scramble)您知道如何反转的它们。没有数字和计算可以让您撤消随机替换值。
  • 但我在其他帖子上读到它可以用种子完成?因为如果我声明一个种子而不是一个随机数,它总是以同样的方式加扰它
  • 当然。您可以以相同的顺序获得相同的随机数,但您无法恢复到您刚刚丢弃的原始值。 您需要设计一种特定的加扰方法,该方法具有撤消它的算法。 我在第一条评论中说过。我再说一遍会有帮助吗?
  • 你能举个例子让我看看吗?就像我在帖子中说的那样,我对这些计算的东西不太好
  • @RaINi im not that good with this calculation stuff 这才是真正的编程发挥作用的地方...您可以使用自己的混淆算法,而不仅仅是复制别人的

标签: c# text scramble


【解决方案1】:

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

        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);

【讨论】:

  • 我喜欢原始帖子上的所有 cmets 都说这是不可能的,然后发布了正确的答案。 +1
【解决方案2】:

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

    /// <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 "";
        }
    }

【讨论】:

    猜你喜欢
    • 2013-06-27
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2013-08-19
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多