Playfair cipher: Difference between revisions
Content added Content deleted
Line 337: | Line 337: | ||
private static String decode(String out) { |
private static String decode(String out) { |
||
return |
return c(new StringBuilder(out), 4); |
||
} |
} |
||
private static String |
private static String c(StringBuilder string, int direction) { |
||
int len = |
int len = string.length(); |
||
for (int i = 0; i < len; i += 2) { |
for (int i = 0; i < len; i += 2) { |
||
char a = txt.charAt(i); |
char a = txt.charAt(i); |
||
char b = txt.charAt(i + 1); |
char b = txt.charAt(i + 1); |
||
int |
int rowOne = positions[a - 'A'].y; |
||
int |
int rowTwo = positions[b - 'A'].y; |
||
int |
int columnOne = positions[a - 'A'].x; |
||
int |
int columnTwo = positions[b - 'A'].x; |
||
if ( |
if (rowOne == rowTwo) { |
||
columnOne = (columnOne + direction) % 5; |
|||
columnTwo = (columnTwo + direction) % 5; |
|||
} else if ( |
} else if (columnOne == c2) { |
||
rowOne = (rowOne + direction) % 5; |
|||
rowTwo = (rowTwo + direction) % 5; |
|||
} else { |
} else { |
||
int tmp = |
int tmp = columnOne; |
||
columnOne = columnTwo; |
|||
columnTwo = tmp; |
|||
} |
} |
||
string.setCharAt(i, charTable[rowOne][columnOne]); |
|||
string.setCharAt(i + 1, charTable[rowTwo][columnTwo]); |
|||
} |
} |
||
return |
return string.toString(); |
||
} |
} |
||
}</lang> |
}</lang> |