From: Simon F. <sim...@us...> - 2005-03-17 00:10:04
|
Update of /cvsroot/jcrypt/JCrypt/cs199mbc/plugins In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31974/cs199mbc/plugins Added Files: RealPlayfair.java Log Message: More Plugins!!! --- NEW FILE: RealPlayfair.java --- /* RealPlayfair Class Copyright (C) 2005 Silas Snider This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package cs199mbc.plugins; import cs199mbc.Machine; public class RealPlayfair implements Machine { private boolean encoder = false; public static final String toName = "Playfair", toString = "Playfair"; private char[][] square = new char[5][5]; private String alpha = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; public String encode(String text) { boolean t = encoder; encoder = true; String temp = doStuff(text); encoder = t; return temp; } public String decode(String text) { boolean t = encoder; encoder = false; String temp = doStuff(text); encoder = t; return temp; } public void setOptionKeyword(String s) { s = s.toUpperCase(); s = s.replace('J', 'I'); int realLength = 0; for (int i = 0; i < s.length(); i++) { // Writes the keyword across the // top if (s.indexOf(s.charAt(i)) == i) { realLength++; square[i % 5][i / 5] = s.charAt(i); } } int j = 0; for (int i = realLength; i < 25; i++) { j++; if (s.indexOf(alpha.charAt(j)) != -1) { i--; } else { square[i % 5][i / 5] = alpha.charAt(j); } } for (int i = 0; i < 5; i++) { for (int k = 0; k < 5; k++) { System.out.print(square[k][i]); } System.out.println(""); } } public void setOptionEncode(boolean b) { encoder = b; } private int findLetterIndex(char c) { int ret = -1; if (c == 'J') c = 'I'; for (int i = 0; i < 25; i++) { if (square[i % 5][i / 5] == c) { ret = i; } } return ret; } private String elimWhite(String inp) { StringBuffer ret = new StringBuffer(); for (int i = 0; i < inp.length(); i++) { if (Character.isWhitespace(inp.charAt(i))) { continue; } ret.append(inp.charAt(i)); } return ret.toString(); } public String doStuff(String s) { StringBuffer result = new StringBuffer(); if (encoder) { s = elimWhite(s); s = s.toUpperCase(); if ((s.length() % 2) != 0) { s = s + "X"; } String digram; for (int i = 1; i < s.length(); i += 2) { if (s.charAt(i - 1) == s.charAt(i)) { if (s.charAt(i - 1) == 'X') { digram = s.charAt(i - 1) + "Q"; } else { digram = s.charAt(i - 1) + "X"; } i--; } else { digram = ("" + s.charAt(i - 1)) + s.charAt(i); } int[] ind = findDigramIndices(digram); char[] ret = new char[2]; if ((ind[0] / 5) == (ind[1] / 5)) { if ((ind[0] % 5) == 4) { ind[0] = ind[0] - 4; } else { ind[0]++; } if ((ind[1] % 5) == 4) { ind[1] = ind[1] - 4; } else { ind[1]++; } ret[0] = square[ind[0] % 5][ind[0] / 5]; ret[1] = square[ind[1] % 5][ind[1] / 5]; } else if ((ind[0] % 5) == (ind[1] % 5)) { ind[0] = (ind[0] + 5) % 25; ind[1] = (ind[1] + 5) % 25; ret[0] = square[ind[0] % 5][ind[0] / 5]; ret[1] = square[ind[1] % 5][ind[1] / 5]; } else { ret[0] = square[ind[0] % 5][ind[1] / 5]; ret[1] = square[ind[1] % 5][ind[0] / 5]; } result.append(ret[0]); result.append(ret[1]); result.append(" "); } } else { s = elimWhite(s); s = s.toUpperCase(); if ((s.length() % 2) != 0) { s = s + "X"; } String digram; for (int i = 1; i < s.length(); i += 2) { if (s.charAt(i - 1) == s.charAt(i)) { if (s.charAt(i - 1) == 'X') { digram = s.charAt(i - 1) + "Q"; } else { digram = s.charAt(i - 1) + "X"; } i--; } else { digram = ("" + s.charAt(i - 1)) + s.charAt(i); } int[] ind = findDigramIndices(digram); char[] ret = new char[2]; if ((ind[0] / 5) == (ind[1] / 5)) { if ((ind[0] % 5) == 0) { ind[0] = ind[0] + 4; } else { ind[0]--; } if ((ind[1] % 5) == 0) { ind[1] = ind[1] + 4; } else { ind[1]--; } ret[0] = square[ind[0] % 5][ind[0] / 5]; ret[1] = square[ind[1] % 5][ind[1] / 5]; } else if ((ind[0] % 5) == (ind[1] % 5)) { if ((ind[0] - 5) < 0) { ind[0] += 25; } if ((ind[1] - 5) < 0) { ind[1] += 25; } ind[0] = (ind[0] - 5) % 25; ind[1] = (ind[1] - 5) % 25; ret[0] = square[ind[0] % 5][ind[0] / 5]; ret[1] = square[ind[1] % 5][ind[1] / 5]; } else { ret[0] = square[ind[0] % 5][ind[1] / 5]; ret[1] = square[ind[1] % 5][ind[0] / 5]; } result.append(ret[0]); result.append(ret[1]); result.append(" "); } } return result.toString(); } private int[] findDigramIndices(String digram) { int[] ret = new int[2]; ret[0] = findLetterIndex(digram.charAt(0)); ret[1] = findLetterIndex(digram.charAt(1)); return ret; } } |