Created
July 12, 2023 15:38
-
-
Save turbanoff/bcd234bd3469e177f5da0e9f3cf85c2a to your computer and use it in GitHub Desktop.
Break BigInteger?
This file contains 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.math.BigInteger; | |
import java.util.Random; | |
public class BigIntegerBreak { | |
static BigInteger breakIt(BigInteger original) { | |
byte[] originalBytes = original.toByteArray(); | |
byte[] newBytes = new byte[originalBytes.length + 1024]; | |
System.arraycopy(originalBytes, 0, newBytes, 1024, originalBytes.length); | |
Thread thread = new Thread(() -> { | |
while (!Thread.interrupted()) { | |
newBytes[0] ^= (byte)1; | |
} | |
}); | |
thread.start(); | |
while (true) { | |
BigInteger s = new BigInteger(newBytes); | |
if (s.toString().startsWith("00000")) { | |
thread.interrupt(); | |
return s; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
BigInteger bigInteger = new BigInteger(65, new Random()); | |
BigInteger broken = breakIt(bigInteger); | |
System.out.println(broken); | |
System.out.println(broken.equals(bigInteger)); // prints false | |
System.out.println(broken.subtract(bigInteger)); // prints 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Breaking java.lang.String
https://twitter.com/WouterCoekaerts/status/1678797190284685313