class SimpleAffine {
private static int[] aValues = {1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25};
private static int[] aInvValues = {1, 9, 21, 15, 3, 19, 7, 23, 11, 5, 17, 25};
private static int val(String str, int pos) {
return str.charAt(pos) - 'A';
}
private static char chr(int val) {
return (char)(val + 'A');
}
public static String decrypt(String cipher, int a, int b) {
int aInv = 0;
int c;
String str = "";
for(int i = 0; i < aValues.length; i++) {
if(aValues[i] == a) {
aInv = aInvValues[i];
}
}
for(int i = 0; i < cipher.length(); i++) {
c = (aInv * (val(cipher, i) - b)) % 26;
if(c < 0) c += 26;
str += chr(c);
}
return str;
}
public static void bruteforce(String cipher) {
String d;
for(int a: aValues) {
for(int b = 0; b < 26; b++) {
d = decrypt(cipher, a, b);
if(d.indexOf("ODER") > 0)
System.out.println(d + " k = (" + a + ", " + b + ")");
}
}
}
public static void main(String[] args) {
System.out.println("1.) " + decrypt("GWDPCTLSDJWGJBNWJLKOJLJWEPBDBGEDJSHJBLCT", 5, 15));
System.out.print("2.) ");
bruteforce("BLUWXTSBMSBZVXUWZMSRWLUAZMUXZKNARSZMNHBWWRABZMA");
}
}