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;
}