Playfair cipher: Difference between revisions

Content deleted Content added
Line 337:
 
private static String decode(String out) {
return codecc(new StringBuilder(out), 4);
}
 
private static String codecc(StringBuilder txtstring, int dirdirection) {
int len = txtstring.length();
for (int i = 0; i < len; i += 2) {
char a = txt.charAt(i);
char b = txt.charAt(i + 1);
 
int r1rowOne = positions[a - 'A'].y;
int r2rowTwo = positions[b - 'A'].y;
int c1columnOne = positions[a - 'A'].x;
int c2columnTwo = positions[b - 'A'].x;
 
if (r1rowOne == r2rowTwo) {
c1columnOne = (c1columnOne + dirdirection) % 5;
c2columnTwo = (c2columnTwo + dirdirection) % 5;
 
} else if (c1columnOne == c2) {
r1rowOne = (r1rowOne + dirdirection) % 5;
r2rowTwo = (r2rowTwo + dirdirection) % 5;
 
} else {
int tmp = c1columnOne;
c1columnOne = c2columnTwo;
c2columnTwo = tmp;
}
 
txtstring.setCharAt(i, charTable[r1rowOne][c1columnOne]);
txtstring.setCharAt(i + 1, charTable[r2rowTwo][c2columnTwo]);
}
return txtstring.toString();
}
}</lang>