import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CipherMachine { private static PrintWriter pw = null; private static Scanner scn = null; public static boolean setWriter(String fileName) { boolean result = false; try { pw = new PrintWriter( new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "sjis"))); result = true; } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } return result; } public static boolean setScanner(String str) { boolean result = false; try { scn = new Scanner(new File(str)); result = true; } catch (FileNotFoundException e) { System.out.println(e); } return result; } public static boolean setScanner(File inputFile) { boolean result = false; try { scn = new Scanner(inputFile); result = true; } catch (FileNotFoundException e) { System.out.println(e); } return result; } public static boolean closeWrier() { boolean result = false; if (pw != null) { pw.close(); result = true; } return result; } public static boolean closeScanner() { boolean result = false; if (scn != null) { scn.close(); result = true; } return result; } public static boolean encode(Scanner scn) { boolean result = false; if (!setWriter("cipher.txt")) { return false; } while (scn.hasNext()) { String buf = scn.next(); int i = 0; while (true) { pw.printf("%s ", Integer.toBinaryString(buf.charAt(i++))); if (i >= buf.length()) break; pw.printf("0%s ", Integer.toOctalString(buf.charAt(i++))); if (i >= buf.length()) break; pw.printf("%s ", Integer.toString(buf.charAt(i++))); if (i >= buf.length()) break; pw.printf("0x%s ", Integer.toHexString(buf.charAt(i++))); if (i >= buf.length()) break; } pw.printf("%s", "@ "); } closeWrier(); return result; } public static boolean decode(String str){ setScanner(str); return decode(); } public static boolean decode(File inputFile){ setScanner(inputFile); return decode(); } public static boolean decode() { boolean result = false; if (!setWriter("decode.txt")) { return false; } if (scn == null) { return false; } int i = 0; while (scn.hasNext()) { String buf = scn.next(); pw.printf("%c", convert(buf)); System.out.println(i + buf); } closeWrier(); closeScanner(); return result; } private static char convert(String str) { if (str.equals("@")) return ' '; if (str.length() > 2 && str.charAt(1) == 'x') { StringBuilder buf = new StringBuilder(); for(int i = 2; i < str.length(); i++){ buf.append(str.charAt(i)); } return (char) (Integer.parseInt(buf.toString(), 16)); } if (str.charAt(0) == '0') { StringBuilder buf = new StringBuilder(); for(int i = 1; i < str.length(); i++){ buf.append(str.charAt(i)); } return (char) (Integer.parseInt(buf.toString(), 8)); } Pattern p = Pattern.compile("^[01]*$"); Matcher m = p.matcher(str); if (str.length() >= 5 && m.find()) { return (char) (Integer.parseInt(str, 2)); } p = Pattern.compile("^[0-9]*$"); m = p.matcher(str); if (m.find()) { return (char) (Integer.parseInt(str)); } return '\u0020'; } }