Skip to content

Instantly share code, notes, and snippets.

@turbanoff
Created July 12, 2023 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turbanoff/bcd234bd3469e177f5da0e9f3cf85c2a to your computer and use it in GitHub Desktop.
Save turbanoff/bcd234bd3469e177f5da0e9f3cf85c2a to your computer and use it in GitHub Desktop.
Break BigInteger?
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
}
}
@turbanoff
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment