Skip to content
Snippets Groups Projects
BruteForce.java 1.16 KiB
Newer Older
20041679 .'s avatar
20041679 . committed
import it.uniupo.byteLib.BitStringGenerator;
import it.uniupo.byteLib.NonPrintableCharactersException;
import it.uniupo.byteLib.Tools;

import java.util.Arrays;

public class BruteForce {
    public static void main(String[] args) throws NonPrintableCharactersException {
        {
            System.out.println("OTP-3");
            var c = Tools.readByteFlow("OTP-3");
            bruteforce(c, 3);
        }
        {
            System.out.println("keystr-1x3");
            var c = Tools.readByteFlow("keystr-1x3");
            bruteforce(c, 1);
        }
    }

    private static void bruteforce(byte[] c, int n) throws NonPrintableCharactersException {
        var bsGen = new BitStringGenerator(n);
        bsGen.zeroInit();
        while (!bsGen.isMax()) {
            var repetition = bsGen.nextVal();
            var key = Arrays.copyOf(repetition, n);
            for (int i = repetition.length - 1; i < n; i++) {
                key[i] = repetition[i % repetition.length];
            }
            var attempt = Xor.xorCipher(c, key);
            if (Tools.isUText(attempt)) {
                System.out.println(Tools.bytesToString(attempt));
            }
        }
    }
}