package newbiescontest; import java.io.Serializable; public final class PloufCipher implements Serializable, Cloneable { protected Plouf keyPlouf; public PloufCipher(Plouf plouf) { if(plouf != null) { keyPlouf = (Plouf)plouf.clone(); } else { keyPlouf = new Plouf(true); } } public Object clone() { return new PloufCipher(keyPlouf); } public Plouf getPlouf() { return (Plouf)keyPlouf.clone(); } protected static int getCardValue(byte byte0) { if(Plouf.isJoker(byte0)) { return 53; } if(Plouf.isClub(byte0)) { return Plouf.getFaceValue(byte0); } if(Plouf.isDiamond(byte0)) { return 13 + Plouf.getFaceValue(byte0); } if(Plouf.isHeart(byte0)) { return 26 + Plouf.getFaceValue(byte0); } if(Plouf.isSpade(byte0)) { return 39 + Plouf.getFaceValue(byte0); } else { return 0; } } protected static int getCharValue(char c) { if(c >= 'A' && c <= 'Z') { return (c - 65) + 1; } if(c >= 'a' && c <= 'z') { return (c - 97) + 1; } else { return 0; } } protected static char getValueChar(int i) { if(i >= 1 && i <= 26) { return (char)((i - 1) + 65); } else { return '*'; } } protected int nextKeyStream() { byte byte0 = -98; byte byte1 = -97; keyPlouf.moveDown(keyPlouf.findTop(byte0), 1); keyPlouf.moveDown(keyPlouf.findTop(byte1), 2); int i = keyPlouf.findBottom(byte0); int j = keyPlouf.findBottom(byte1); keyPlouf.tripleCut(Math.min(i, j), Math.max(i, j) + 1); byte byte2 = keyPlouf.peekBottom(0); keyPlouf.tripleCut(1, keyPlouf.count() - getCardValue(byte2)); keyPlouf.cutTop(1); byte byte3 = keyPlouf.peekTop(getCardValue(keyPlouf.peekTop(0))); int k = getCardValue(byte3); if(k == 53) { return nextKeyStream(); } if(k > 26) { k -= 26; } return k; } public String encrypt(String s) { return encrypt(s, true); } public String encrypt(String s, boolean flag) { StringBuffer stringbuffer = new StringBuffer(); for(int i = 0; i < s.length(); i++) { int j = getCharValue(s.charAt(i)); if(j <= 0) { continue; } j += nextKeyStream(); if(j > 26) { j -= 26; } stringbuffer.append(getValueChar(j)); } if(flag) { for(; stringbuffer.length() % 5 != 0; stringbuffer.append(encrypt("X", false))) { } } return stringbuffer.toString(); } public int check(String s) { return encrypt(s).compareTo("POSGHMRROQZICZZKFUAXLPEFCORHHZSOGCW") == 0 ? 1 : 0; } }