2

I already got this method to scramble my text but I have no clue on how to unscramble it.

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

How can I make a scramble or shuffle function that shuffles the text with a specific number so it scrambles the text the same way every time and how can I unshuffle it after that?

RaINi
  • 53
  • 1
  • 5
  • You can't just pull random characters out of the air and then expect to be able to undo things. You have to design a specific way to encode (*scramble*) them that you know how to reverse. There is no *numbers and calculations* that will let you undo random replacement of values. – Ken White Nov 08 '16 at 23:43
  • 1
    but i read on other posts that it can be done with the seed? cause if i declare a seed and not a random number it always scrambles it the same way – RaINi Nov 08 '16 at 23:44
  • Sure. You can get the same random numbers in the same order, but you've got no way to get back to the original value you've simply thrown away. *You need to design a specific means of scrambling that has an algorithm to undo it.* I said that in my first comment. Is it going to help that I've said it again? – Ken White Nov 08 '16 at 23:47
  • 1
    can you give me an example for this so i can look at it ? like i say in my post im not that good with this calculation stuff – RaINi Nov 08 '16 at 23:49
  • @RaINi `im not that good with this calculation stuff` this is where real *programming* comes in... You can device your own obfuscation algorithm instead of just copying someone else's – L.B Nov 08 '16 at 23:53
  • I think you are looking for encryption and decryption. I'd advise to use one of the many secure algorithms, AES is one such example. – Lukazoid Nov 09 '16 at 00:05
  • @Lukazoid its not for an encryption. its just a fun project for sending scrambled messages – RaINi Nov 09 '16 at 00:06

2 Answers2

9

Just do the same thing you already did backwards (you need to use the same random numbers from the same seed in reverse order, so get those in a list first):

        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);
Sean K
  • 774
  • 7
  • 10
2

I have used my own scramble/unscramble methods in the past that did it another way but recently discovered some string just don't scramble I did not use swapping but a form of encoding. I found this thread and have taken the last provided solution and made it more useable by encoding the seed into the scrambled string so the seed is not required to decode. This allows a scrambled string to be used in a database to obfuscate it from easy reading. Unless you know where the seed is imbedded in the string (currently the last chars but you could make it any part of the string if needed)

    /// <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 "";
        }
    }
TruAG
  • 41
  • 4