SSpoke - 8 months ago 54

Vb.net Question

I have a simple function that handles overflows with modulus operator

`Private Function RandomizeBlock(seed As Integer, ByVal block() As Byte) As Byte()`

Dim Generator As System.Random = New System.Random(seed)

Dim newblock(255) As Byte

Dim i As Integer = 0

For i = 0 To block.Length - 1

newblock(i) = (block(i) + Generator.Next(0, 256)) Mod 256

Next

Return newblock

End Function

How do I undo the randomization done to a block?

I know mod works like this:

`253,254,255,0,1,2,3,4`

`0`

Do can I find the inverse of

`reverse`

`rndValue = Generator.Next(0, 256)`

reverse_1 = ((256 - rndValue) + block(i)) Mod 256

reverse_2 = ((256 + rndValue) - block(i)) Mod 256

Answer

If you know the random value, then reconstructing the original value is very simple.

You just have to keep in mind that working modulo `p`

, you don't have actual numbers but remainder classes. The first `p`

natural numbers are usually used as the representatives of these classes. Luckily, subtraction and addition are perfectly compatible with remainder classes.

The `Mod`

implementation of VB converts any positive number to the representative of its remainder class. However, it can not do it on negative numbers. You have to do it on your own.

Long story short, this is the code:

```
Dim reverse As Integer = block(i) - rndValue;
If reverse < 0 Then reverse = reverse + 256 'Convert to representative of remainder class
```