Playfair cipher: Difference between revisions

Content deleted Content added
→‎{{header|Java}}: more verbose variable names (please test changed code before posting)
Line 281: Line 281:
String jti = prompt("Replace J with I? y/n: ", sc, 1);
String jti = prompt("Replace J with I? y/n: ", sc, 1);


boolean changeChar = jti.equalsIgnoreCase("y");
boolean changeJtoI = jti.equalsIgnoreCase("y");


createTable(key, JtoI);
createTable(key, changeJtoI);


String enc = encode(prepareText(txt, changeChar));
String enc = encode(prepareText(txt, changeJtoI));


System.out.printf("%nEncoded message: %n%s%n", enc);
System.out.printf("%nEncoded message: %n%s%n", enc);
Line 300: Line 300:
}
}


private static String prepareText(String s, boolean changeChar) {
private static String prepareText(String s, boolean changeJtoI) {
s = s.toUpperCase().replaceAll("[^A-Z]", "");
s = s.toUpperCase().replaceAll("[^A-Z]", "");
return changeChar ? s.replace("J", "I") : s.replace("Q", "");
return changeJtoI ? s.replace("J", "I") : s.replace("Q", "");
}
}


private static void createTable(String key, boolean changeChar) {
private static void createTable(String key, boolean changeJtoI) {
charTable = new char[5][5];
charTable = new char[5][5];
positions = new Point[26];
positions = new Point[26];


String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeChar);
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeJtoI);


int len = s.length();
int len = s.length();
Line 322: Line 322:
}
}


private static String encode(String in) {
private static String encode(String s {
StringBuilder sb = new StringBuilder(in);
StringBuilder sb = new StringBuilder(s);


for (int i = 0; i < sb.length(); i += 2) {
for (int i = 0; i < sb.length(); i += 2) {
Line 336: Line 336:
}
}


private static String decode(String out) {
private static String decode(String s) {
return c(new StringBuilder(out), 4);
return codec(new StringBuilder(s), 4);
}
}


private static String c(StringBuilder string, int direction) {
private static String codec(StringBuilder text, int direction) {
int len = string.length();
int len = text.length();
for (int i = 0; i < len; i += 2) {
for (int i = 0; i < len; i += 2) {
char a = txt.charAt(i);
char a = text.charAt(i);
char b = txt.charAt(i + 1);
char b = text.charAt(i + 1);


int rowOne = positions[a - 'A'].y;
int row1 = positions[a - 'A'].y;
int rowTwo = positions[b - 'A'].y;
int row2 = positions[b - 'A'].y;
int columnOne = positions[a - 'A'].x;
int col1 = positions[a - 'A'].x;
int columnTwo = positions[b - 'A'].x;
int col2 = positions[b - 'A'].x;


if (rowOne == rowTwo) {
if (row1 == row2) {
columnOne = (columnOne + direction) % 5;
col1 = (col1 + direction) % 5;
columnTwo = (columnTwo + direction) % 5;
col2 = (col2 + direction) % 5;


} else if (columnOne == c2) {
} else if (col1 == col2) {
rowOne = (rowOne + direction) % 5;
row1 = (row1 + direction) % 5;
rowTwo = (rowTwo + direction) % 5;
row2 = (row2 + direction) % 5;


} else {
} else {
int tmp = columnOne;
int tmp = col1;
columnOne = columnTwo;
col1 = col2;
columnTwo = tmp;
col2 = tmp;
}
}


string.setCharAt(i, charTable[rowOne][columnOne]);
text.setCharAt(i, charTable[row1][col1]);
string.setCharAt(i + 1, charTable[rowTwo][columnTwo]);
text.setCharAt(i + 1, charTable[row2][col2]);
}
}
return string.toString();
return text.toString();
}
}
}</lang>
}</lang>