# Need help scrambling an image

**URL:** <https://discourse.processing.org/t/need-help-scrambling-an-image/2667>\
**Category:** Coding Questions\
**Created:** [August 15, 2018, 6:10pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667 "2018-08-15T18:10:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yxzageo](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@yxzageo](https://discourse.processing.org/u/yxzageo)\
**Post date:** [August 15, 2018, 6:10pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/1 "2018-08-15T18:10:06Z")

</div>

Hello, I posed a question about a code snippet a few days ago and got a quick and understandable answer. I have this piece of code that I’m trying to figure out what it does and how to reverse the action. Unfortunately I’m way out on a limb.  
The code scrambles an image and I wonder if anyone can give me some help to reverse the process. This is the code:

```auto

```

PImage image;

long seed=6229274;

void setup() {  
size(640, 320);

image = loadImage(“coordinates.jpg”);

scrambleImage(image);  
image(image, 0, 0);

image.save(“scrambled.jpg”);  
}

void scrambleImage(PImage image) {  
int n = image.width \* image.height;

image.loadPixels();

for (int i = n-1; i \> 0; i–) {  
int j = rand()%(i+1);  
image.pixels[i]^=image.pixels[j];  
image.pixels[j]^=image.pixels[i];  
image.pixels[i]^=image.pixels[j];  
}

image.updatePixels();  
}

int rand() {  
long a = 16807;  
long m = 2147483647;

seed = (a \* seed) % m;

return int(seed);  
}

```auto

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 15, 2018, 6:18pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/2 "2018-08-15T18:18:32Z")

</div>

If it’s code, please format it like code. Edit your post, select the code, and hit the please-format-this-as-code button, which looks like this: \</\>

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 15, 2018, 8:45pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/3 "2018-08-15T20:45:17Z")

</div>

It’s hard to help with general “how do I do this” type questions. Which part of this are you stuck on?

Please read this guide:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [August 16, 2018, 5:05pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/4 "2018-08-16T17:05:12Z")

</div>

So first off, two things that kind of caught my attention:

> [@yxzageo](#):
>
> reverse the process

and

`int j = rand()%(i+1);`

Now, that doesn’t mean that it is impossible to do what it is you want to do, but the way I would begin approaching your problem, would involve loop through the original picture, and tagging each pixel with some sort of identifier, and then having them be scrambled as you have it here, and then “de-scrambling” the image, by putting the pixels back in the original order that they were tagged in. I hope that makes sense 😃

Now I don’t know exactly what you mean by reversing the process. Do you want to scramble an image, then “de-scramble” it with the same piece of code? Or do you want to have this code here that you posted, where an image is scrambled, and then you want a separate piece of code to de-scramble it?

Because if THAT is the case, then an idea I would give you, (which I’m sure would be a bit data heavy) and not efficient at all, is that you get your scrambled picture, scramble it, and check if it’s the same as the original picture, and literally do that millions of times until you get it right 😂 Obviously, to do that in a time span that isn’t days would require a much smaller image, preferably one that is about 2x2 pixels 😄

So those are my two ideas so far, but as mentioned, could you please elaborate a bit on how you would want to do this? What are you trying out and what isn’t working? Also, do you get how this code works? Or would you like some more explanation on that?

Hope you are able to get started on this project,

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![yxzageo](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@yxzageo](https://discourse.processing.org/u/yxzageo)\
**Post date:** [August 16, 2018, 5:06pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/5 "2018-08-16T17:06:00Z")

</div>

> [@yxzageo](#):
>
> void scrambleImage(PImage image) {  
> int n = image.width \* image.height;
> 
> image.loadPixels();
> 
> for (int i = n-1; i \> 0; i–) {  
> int j = rand()%(i+1);  
> image.pixels[i]^=image.pixels[j];  
> image.pixels[j]^=image.pixels[i];  
> image.pixels[i]^=image.pixels[j];  
> }

It’s primarily about how I reverse this scrambling of the image.  
Hans

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 17, 2018, 4:22am UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/6 "2018-08-17T04:22:28Z")

</div>

Start by breaking this into a smaller problem - lets say we have a 1x1 picture, and we put it through the scrambler. Or even simpler, lets say we took an int, between 0 and 255, and added a random number to it, like you do above

`int j = rand()%(i+1);`

If we took a number, for instance, 99, and made a random number with rand(), for instance, 3, we would add them together and get 102.

Now, with this “scrambled” int, how could we “unscramble” it? How would we “reverse” it?

---

<div class="post-metadata">

**Author:** ![yxzageo](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@yxzageo](https://discourse.processing.org/u/yxzageo)\
**Post date:** [August 17, 2018, 6:19pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/7 "2018-08-17T18:19:39Z")

</div>

I see your point. But the question is if this code really returns a true random value.  
(snippet)  
long seed=6229274;  
…  
int rand() {  
long a = 16807;  
long m = 2147483647;

seed = (a \* seed) % m;

return int(seed);

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 17, 2018, 7:18pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/8 "2018-08-17T19:18:47Z")

</div>

It is not possible to unscramble the image created with this algorithm based on the information in the scrambled image alone. Under normal circumstances it is easy to reverse an XOR (exclusive-or) operation for instance

`scrambled = original ^ n`  
`original = scrambled ^ n`

but this reversal depends on knowing `n`.

In the algorithm above `image.pixels[j]` is different in the original and the scrambled image so the scrambling operation is irreversable.

The only way to unscramble the image is to use the original image to get the original value of `image.pixels[j]` but that rather defeats the purpose.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 17, 2018, 7:22pm UTC](https://discourse.processing.org/t/need-help-scrambling-an-image/2667/9 "2018-08-17T19:22:09Z")

</div>

BTW it is not very important if the `rand()` method is truly random provided it gives a wide range of values. In this situation it is important that for a given _seed value_ it produces the _same sequence of numbers_.
