0

this is the question Breaking code Brute force is an important, and basic, algorithmic paradigm. In a brute-force algorithm, a problem is solved in the most straightforward manner based on the statement of the problem and the definitions of terms. Brute-force algorithms are designed to solve problems without regard to the computing resources required, where in some you examine every possible solution until you find one that works. Break the key (find a and b) used for encryption using brute force, where you know plaintext and ciphertext. Assume that it has been encrypted using affine ciphers f(p) = (a.p + b).

and this is my code for, is there any other way of doing it ?

public static int[] findKeys(String plaintext, String ciphertext){
    int[] keys = new int[2];
    int temp, a = 0, b = 0;
    char p, c;

    for (int i = 0; i < plaintext.length(); i++) {
        p = plaintext.charAt(i);
        c = ciphertext.charAt(i);

        for (int x = 0; x <= 9; x++) {
            a = a + x;

            for (int y = 0; y <= 9; y++) {
                b = b + y;
                temp = (int) p - 65; //convert to int
                temp = Math.floorMod((a * temp + b), 26);
                p = (char) (temp + 65); //convert to char

                if (p == c) {
                    keys[0] = a;
                    keys[1] = b;
                }
            }
        }
    }

    return keys;
}
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Shoug
  • 1
  • 1
    Edit more info into your question. It's great that you've written code -- does it work? If yes, what's your question? If not, what did you expect to happen, versus what actually happens? – k_ssb May 11 '18 at 23:01
  • It is hard to understand what you are asking ... and why. Yes obviously, there are an infinite number of "other ways" to code this. But what are you actually looking for? A faster way? A simpler way? A more elegant way? A more shiny way? Or are you asking us to find the bugs in your code? Or tell you if it is correct? – Stephen C May 12 '18 at 00:49
  • what I did was trying all possible numbers till I get the ones that fit and yes it works, is there other methods to find inverse other than trying all possible numbers ? – Shoug May 12 '18 at 09:10
  • Question doesn't make sense to me. What sets are a, b, and p from? Is the "." supposed to be multiplication? Is "f" supposed to the the encryption function? If the operation is mod 2 then you will not be able to decrypt unless "p" is a single bit and "a" is 1 mod 2. The code doesn't seem to reflect the question. – President James K. Polk May 12 '18 at 15:11

0 Answers0