1

I needed to convert a python script which is a scramble script to a Java class that does the same thing. I've attempted it but I cannot get the correct results.

This i the python code:

class scramble7prng:
    def __init__(self, seed):
        self.ix= 0
        self.buffer= [ 0 for i in range(624) ]
        self.seedbuffer(seed)
    def dumpbuffer(self):
        for x in self.buffer:
            print " %08x" % x,
        print
    def seedbuffer(self, seed):
        for i in range(624):
            self.buffer[i]= seed
            seed= (1812433253 * ((seed ^ rshift(seed, 30)) + 1)) & 0xFFFFFFFF
    def getbyte(self):
        x= self.getint()
        if isneg(x):
            x= negate(x)
        return x % 256
    def getint(self):
        if self.ix==0:
            self.mixbuffer()
        val= self.buffer[self.ix]
        self.ix = (self.ix+1) % 624

        val ^= rshift(val, 11) ^ lshift((val ^ rshift(val, 11)), 7) & 0x9D2C5680;
        return rshift((val ^ lshift(val, 15) & 0xEFC60000), 18) ^ val ^ lshift(val, 15) & 0xEFC60000;
    def mixbuffer(self):
        i=0
        j=0
        while i<624:
            i += 1
            v4= (self.buffer[i%624] & 0x7FFFFFFF) + (self.buffer[j]&0x80000000)
            v6 = rshift(v4,1) ^ self.buffer[(i+396)%624]
            if v4&1:
                v6 ^= 0x9908B0DF
            self.buffer[j] = v6
            j += 1


def newscramble(serverrandom, seed):
    prng= scramble7prng(seed)
    for i in range(100):
        byte100= prng.getbyte()
    return "".join(chr(ord(c)^(prng.getbyte()&byte100)) for c in serverrandom)

And this is my Java code that I tried to convert if from.

public class Scramble {
    public static final int SIZE = 624;
    int seed;
    int[] buffer = new int[SIZE];
    int ix = 0;


    public Scramble(int seed) {
        this.seed = seed;

        for (int i = 0; i < 624; i++) {
            buffer[i] = seed;
            seed = 1812433253 * ((seed ^ (seed >> 30)) + 1) & 0xFFFFFFFF;
        }
    }

    public byte[] getScramble(byte[] serverRandom)
    {
        byte[] result = new byte[serverRandom.length];
        byte byte100 = 0;
        for (int i = 0; i < 100; i++) {
            byte100 = getByte();
        }
        for (int i = 0; i < serverRandom.length; i++) {

            byte b = (byte) (getByte() & byte100);
            result [i] = ((byte) (serverRandom[i] ^ b));
        }
        return result;
    }

    private byte getByte()
    {
        int x = getInt();
        if(isNeg(x))
        {
            x = negate(x);
        }
        return (byte) (x);
    }
    private int getInt()
    {
        if(ix == 0)
        {
            mixBuffer();
        }
        int val = buffer[ix];
        ix = (ix+1) % 624;
        val ^= (val >> 11) ^ ((val ^ (val >> 11)) << 7) & 0x9D2C5680;
        val = (int) (((val ^ (val << 15) & 0xEFC60000) >> 18) ^ val ^ (val << 15) & 0xEFC60000);
        return val;

    }

    private void mixBuffer()
    {

        for (int i = 1, j = 0; i <= SIZE; i++, j++) {
            int v4 = (buffer[i % SIZE] & 0x7FFFFFFF) + (buffer[j] & 0x80000000);
            int v6 = (v4 >> 1) ^ buffer[(i + 396) % 624];

            if( (v4 & 1) == 1 )
            {
                v6 ^= 0x9908B0DF;
            }
            buffer[j] = v6;

        }

    }


    private boolean isNeg(int number)
    {
        return Integer.signum(number) == -1;
    }

    private int negate(int number)
    {
        return Integer.reverse(number) >> 8;
    }


}

The expected result is dependant on other variables that are randomly generated. The script is supposed to take an int and a byte[] and then scramble them using the class and it's methods and return a new byte[] which will be appended to a base key and used as the key for rc4

Shivam Paw
  • 203
  • 3
  • 14
  • Well then. What are the "correct results"? – SBI Aug 18 '15 at 06:29
  • Please describe expected output and error you're getting. – Maciej Lach Aug 18 '15 at 06:29
  • This question is too broad, and is essentially just a "why isn't this code working?" question (see item one [here](http://stackoverflow.com/help/on-topic)). Rather than giving us two programs and asking us to (a) figure out what they do and (b) figure out how they differ, you should put some effort into debugging your Java application based on its expected behavior (ie, ignoring the Python app except insofar as it defines behavior). If you have trouble with a particular aspect of that, please come up with a _specific_ question that hones in on that difficulty. – yshavit Aug 18 '15 at 06:29
  • @SBI The expected result is dependant on other variables that are randomly generated. The script is supposed to take an int and a byte[] and then scramble them using the class and it's methods and return a new byte[] which will be appended to a base key and used as the key for rc4 – Shivam Paw Aug 18 '15 at 06:36
  • @MaciejLach The expected result is dependant on other variables that are randomly generated. The script is supposed to take an int and a byte[] and then scramble them using the class and it's methods and return a new byte[] which will be appended to a base key and used as the key for rc4 – Shivam Paw Aug 18 '15 at 06:36
  • We can see that. But as @yshavit put it a lot more diplomatically, you've made zero effort as to explicitly showing what you've tried. Which leads me to believe that hasn't actually happened. – SBI Aug 18 '15 at 06:40

0 Answers0