GithubHelp home page GithubHelp logo

replicatedrandom's People

Contributors

fta2012 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

replicatedrandom's Issues

Get the next random value from the previous random value

I am generating random values from java random() between 1 to 4.

Every time i am getting a random number between 1 to 4.
i want to know the next random number from previous random number which i got.
Is it possible to do so.

Change Numbers

This is a question rather than an issue Mr. Ta. I read your article Predicting the next Math.random() in Java, August 2014. Great work! I am learning about the various techniques, Math Random, Next Int etc and want to know how to use my own numbers.

For example, if I want to use these numbers from my head or through observation; 77, 31, 4, 61, 2, 50, 18, 43, 28, do I remove your numbers below and input my own numbers one at a time and run the information? Or do I combine all of the numbers? My problem is placement, like I stated I'm in the learning phase. What is the best way to structure?

Yours Original:

public class ReplicatedRandom extends Random {
// Replicate the state of a Random using a single value from its nextDouble
public boolean replicateState(double nextDouble) {
    // nextDouble() is generated from ((next(26) << 27) + next(27)) / (1L << 53)
    // Inverting those operations will get us the values of next(26) and next(27)
    long numerator = (long)(nextDouble * (1L << 53));
    int first26 = (int)(numerator >>> 27);
    int last27 = (int)(numerator & ((1L << 27) - 1));
    return replicateState(first26, 26, last27, 27);
}

// Replicate the state of a Random using a single value from its nextLong
public boolean replicateState(long nextLong) {
    int last32 = (int)(nextLong & ((1L << 32) - 1));
    int first32 = (int)((nextLong - last32) >> 32);
    return replicateState(first32, 32, last32, 32);...

Mine # 77:

public class ReplicatedRandom extends Random {
// Replicate the state of a Random using a single value from its nextDouble
public boolean replicateState(double nextDouble) {
    // nextDouble() is generated from ((next(77) <<78) + next(78)) / (1L << 156)
    // Inverting those operations will get us the values of next(77) and next(78)
    long numerator = (long)(nextDouble * (1L << 156));
    int first77 = (int)(numerator >>> 78);
    int last78 = (int)(numerator & ((1L << 78) - 1));
    return replicateState(first77, 77, last78, 78);
}

// Replicate the state of a Random using a single value from its nextLong
public boolean replicateState(long nextLong) {
    int last77= (int)(nextLong & ((1L << 77) - 1));
    int first77 = (int)((nextLong - last77) >> 77);
    return replicateState(first77, 77, last77, 77);
}
// Replicate the state of a Random using two consecutive values from its nextInt
public boolean replicateState(int firstNextInt, int secondNextInt) {
    return replicateState(firstNextInt, 77, secondNextInt, 77);
}

// Replicate the state of a Random using two consecutive values from its nextFloat
public boolean replicateState(float firstNextFloat, float secondNextFloat) {
    return replicateState((int)(firstNextFloat * (1 << 77)), 77, (int)(secondNextFloat * (1 << 77)), 77);
}
public boolean replicateState(int nextN, int n, int nextM, int m) {
    // Constants copied from java.util.Random
    final long multiplier = 0x5DEECE66DL;
    final long addend = 0xBL;
    final long mask = (1L << 48) - 1;

    long upperMOf48Mask = ((1L << m) - 1) << (48 - m);

    // next(x) is generated by taking the upper x bits of 48 bits of (oldSeed * multiplier + addend) mod (mask + 1)
    // So now we have the upper n and m bits of two consecutive calls of next(n) and next(m)
    long oldSeedUpperN = ((long)nextN << (48 - n)) & mask;
    long newSeedUpperM = ((long)nextM << (48 - m)) & mask;

Also, learning about bits as well. In my example of 77 above, I added 77 + 78= 156, do I need to change all of the 48 bit parts to 156 or another number?

Thank you very much for this much needed guidance!

NewtoCScience-

Not finding seed

Hi, I'm trying to run your code as a JUnit Test and it is not working (any ideas?)

Here is the code:

public class Vulnerability_Weak_Crypto {

    @Test
    public void randomTest()
    {
        ReplicatedRandom rr = new ReplicatedRandom();

        System.out.println("Replicating from nextInt");
        for (int i = 0; i < 3; i++) {
            Random r = new Random();
            if (rr.replicateState(r.nextInt(), r.nextInt())) {
                for (int j = 0; j < 3; j++)
                    System.out.println(rr.nextInt() + "\t" + r.nextInt());
                System.out.println();
            }
        }
    }
}


class ReplicatedRandom extends Random {


    // Replicate the state of a Random using two consecutive values from its nextFloat
    public boolean replicateState(float firstNextFloat, float secondNextFloat) {
        return replicateState((int)(firstNextFloat * (1 << 24)), 24, (int)(secondNextFloat * (1 << 24)), 24);
    }

    public boolean replicateState(int nextN, int n, int nextM, int m) {
        // Constants copied from java.util.Random
        final long multiplier = 0x5DEECE66DL;
        final long addend = 0xBL;
        final long mask = (1L << 48) - 1;

        long upperMOf48Mask = ((1L << m) - 1) << (48 - m);

        // next(x) is generated by taking the upper x bits of 48 bits of (oldSeed * multiplier + addend) mod (mask + 1)
        // So now we have the upper n and m bits of two consecutive calls of next(n) and next(m)
        long oldSeedUpperN = ((long)nextN << (48 - n)) & mask;
        long newSeedUpperM = ((long)nextM << (48 - m)) & mask;

        // Bruteforce the lower (48 - n) bits of the oldSeed that was truncated.
        // Calculate the next seed for each guess of oldSeed and check if it has the same top m bits as our newSeed.
        // If it does then the guess is right and we can add that to our candidate seeds.
        ArrayList<Long> possibleSeeds = new ArrayList<Long>();
        for (long oldSeed = oldSeedUpperN; oldSeed <= (oldSeedUpperN | ((1L << (48 - n)) - 1)); oldSeed++) {
            long newSeed = (oldSeed * multiplier + addend) & mask;
            if ((newSeed & upperMOf48Mask) == newSeedUpperM) {
                possibleSeeds.add(newSeed);
            }
        }

        if (possibleSeeds.size() == 1) {
            // If there's only one candidate seed, then we found it!
            System.out.println("found seed: " + possibleSeeds.get(0));
            setSeed(possibleSeeds.get(0) ^ multiplier); // setSeed(x) sets seed to `(x ^ multiplier) & mask`, so we need another `^ multiplier` to cancel it out
            return true;
        }
        if (possibleSeeds.size() >= 1) {
            System.out.println("Didn't find a unique seed. Possible seeds were: " + possibleSeeds);
            return false;

        } else {
            System.out.println("Failed to find seed!");
        }
        return false;
    }
}

here is the result

image

Thx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.