0

I'm relatively new to this I have a random function that uses a keyword as a seed to encrypted some data i.e. have plain-text data 'abcd' and this function will randomly change the order 'dcab'. I am struggling to think of a way to reverse this function so I am able to retrieve back the original plain-text i.e. transform 'dcab' back to 'abcd' using the keyword. I have been looking at other post on here but I am still finding this difficult.

Here is the function:

Random random = new Random(q.GetHashCode());

            for (int i = 0; i < data.Length; i++)
            {
                int idx = random.Next(i, data.Length);

                //swap elements
                byte tmp = data[i];
                data[i] = data[idx];
                data[idx] = tmp;
            }

Any suggestions please?

user2520014
  • 73
  • 2
  • 11
  • So you made a random permutation of the letters. How do you expect to be able to retrieve it back? (hint: it's impossible). – Victor Zakharov Nov 09 '14 at 19:11
  • 1
    As long as he knows the seed it isn't random for him! – TaW Nov 09 '14 at 19:20
  • 1
    @Neolisk: it would be impossible if the permutation was truly random. But the `Random` class is a `pseudo-random number generator` (hence, the seed). Given that, it's trivial to undo any random permutation. – Peter Duniho Nov 09 '14 at 19:20
  • 1
    Unless this is purely an academic exercise, I would recommend taking advantage of the many "real" encryption functions in .net, which are supported. See http://stackoverflow.com/questions/165808/simple-two-way-encryption-for-c-sharp/212707#212707 or http://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata(v=vs.110).aspx – Mitch Nov 09 '14 at 21:35

1 Answers1

2

This should work fine:

static void Unpermute(byte[] data, string seed)
{
    Random random = new Random(seed.GetHashCode());
    List<int> swapNumbers = new List<int>(Enumerable.Range(0, data.Length)
        .Select(i => random.Next(i, data.Length)));

    for (int i = data.Length - 1; i >= 0; i--)
    {
        byte temp = data[i];
        data[i] = data[swapNumbers[i]];
        data[swapNumbers[i]] = temp;
    }
}

If this is a homework assignment, let us know if you get a good grade. :)

CAUTION: both the Random class and the GetHashCode() method are documented specifically as being platform-dependent. That is, the implementation of each can and sometimes does vary from one version of .NET to another. The above solution assumes that the same implementation of each is used for both the original permutation and the un-permutation.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Hehe, the last time I was there I was happy as a clam until the customer demanded that the unscramling should happen with a VB program and I found that the Random generators we nor 'compatible'.. (Had to write one myself.. Ach the 20th century ;-) – TaW Nov 09 '14 at 19:23
  • ahhhhhhhhhh yes! thank you I've spent hours on this p.s I will ha :) – user2520014 Nov 09 '14 at 19:44
  • 4
    Note that `Random` is not guaranteed to generate the same output for the same seed across .NET versions. It says so right in the "Notes to Callers" section for the class [on the MSDN](http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx) So permutating in one .NET version may not unpermuate in another .NET version. – Scott Chamberlain Nov 09 '14 at 20:09
  • @ScottChamberlain: yes, absolutely. This only is reliable if one is using identical PRNG _and_ hash code implementations for shuffling and unshuffling. I'll add a note to the answer. – Peter Duniho Nov 09 '14 at 21:27
  • I did not even notice the `GetHashCode()`, `GetHashCode()` is even worse than `Random` for consistency. It is not guaranteed to be the same across AppDomains, so you could run the program once, permuate, save the result, then run the program again, and when you go to un-permeate you can get a different value for `seed.GetHashCode()` with the same string. See the comment in the DEBUG section of GetHashCode for string from [the reference source](http://referencesource.microsoft.com/#mscorlib/system/string.cs,855) – Scott Chamberlain Nov 09 '14 at 21:47