Created
March 28, 2013 09:43
-
-
Save 123jimin/5262003 to your computer and use it in GitHub Desktop.
Enter a 6-byte string, then this will emit a Java program printing the string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
public class SeedGenerator { | |
public static void main(String[] args) throws Exception{ | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Write a 6-byte string: "); | |
String string = scanner.next(); | |
byte[] b = string.getBytes("UTF-8"); | |
if(b.length != 6){ | |
// FIXME: when b.length<6? | |
System.out.println("Error: Invalid Length ("+b.length+")"); | |
return; | |
} | |
long a = b[0]+b[1]*256L+b[2]*65536L+b[3]*16777216L; | |
a = a*65536L; | |
byte[] c = new byte[2]; | |
Random random; | |
for(long i=0;i<65536;i++){ | |
long seed = a+i; | |
random = new Random(seed ^ 0x5DEECE66DL); | |
random.nextBytes(c); | |
if(c[0] == b[4] && c[1] == b[5]){ | |
seed = ((seed-0xBL)*0xDFE05BCB1365L)&((1L<<48) - 1); | |
seed ^= 0x5DEECE66DL; | |
String aa = "0x"+Long.toHexString(seed).toUpperCase(); | |
System.out.println("Seed value: "+aa); | |
System.out.println("\nimport java.util.Random;"); | |
System.out.println("public class X {"); | |
System.out.println("\tpublic static void main(String[] args) {"); | |
System.out.println("\t\tbyte[] b = new byte[6];"); | |
System.out.println("\t\tnew Random("+aa+"L).nextBytes(b);"); | |
System.out.println("\t\tSystem.out.println(new String(b));\n\t}\n}"); | |
return; | |
} | |
} | |
System.out.println("The seed could not be found... :("); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment