/* * TextWidget - Visual Java Cipher text deciphering widget * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.applet.Applet; import java.awt.*; import util.comm.*; import java.util.Vector; import java.net.URL; import java.io.DataInputStream; /** * Display a decrypted text area on the screen. */ public class TextWidget extends Applet implements Runnable { private DataChannel dc; private Thread updater; private String myChannel; private int rows, cols; TextArea txtArea; Color bgColor; int getInteger(String t, int d) { String x = getParameter(t); if (x == null) return d; try { return (Integer.parseInt(x)); } catch (NumberFormatException e) { return d; } } String id; /** * return one of the "primary" colors. */ Color getPrimaryColor(String xcolor) { if (xcolor.equalsIgnoreCase("red")) { return Color.red; } else if (xcolor.equalsIgnoreCase("green")) { return Color.green; } else if (xcolor.equalsIgnoreCase("yellow")) { return Color.yellow; } else if (xcolor.equalsIgnoreCase("blue")) { return Color.blue; } else if (xcolor.equalsIgnoreCase("cyan")) { return Color.cyan; } else if (xcolor.equalsIgnoreCase("gray")) { return Color.gray; } else if (xcolor.equalsIgnoreCase("magenta")) { return Color.magenta; } else if (xcolor.equalsIgnoreCase("orange")) { return Color.orange; } else if (xcolor.equalsIgnoreCase("pink")) { return Color.pink; } return null; } /** * Return a Color value from the parameter list. Colors are * specified using HEX notation as in NetScape. If there is any * error, return the defaultColor instead. */ public Color getColor(String name, Color defaultColor) { String x = getParameter(name); if (x != null) { if (x.startsWith("0x")) { int z; try { z = Integer.parseInt(x.substring(2), 16); return( new Color((z >>> 16) & 0xff, (z >>> 8) & 0xff, (z & 0xff))); } catch (NumberFormatException e) { return defaultColor; } } else if (x.startsWith("#")) { int z; try { z = Integer.parseInt(x.substring(1), 16); return( new Color((z >>> 16) & 0xff, (z >>> 8) & 0xff, (z & 0xff))); } catch (NumberFormatException e) { return defaultColor; } } else if (x.substring(0, 4).equalsIgnoreCase("dark")) { Color y = getPrimaryColor(x.substring(4)); if (y != null) return y.darker(); return defaultColor; } else if (x.substring(0, 5).equalsIgnoreCase("light")) { Color y = getPrimaryColor(x.substring(5)); if (y != null) { return y.brighter(); } } else { Color y = getPrimaryColor(x); if (y != null) return y; return defaultColor; } } return defaultColor; } /** * Parameters : * id = the identifier for our cipher text. * datachannel = the channel that will get the password. * rows = number of rows in the text area; * cols = number of columns. */ public void init() { String x; int w = size().width; int h = size().height; id = getParameter("id"); x = getParameter("datachannel"); if (x == null) myChannel = "test"; else myChannel = x; rows = getInteger("rows", h / 20); cols = getInteger("cols", w / 8); bgColor = getColor("bgcolor", Color.lightGray); txtArea = new TextArea(rows, cols); add(txtArea); } public void paint(Graphics g) { g.setColor(bgColor); g.fillRect(0, 0, size().width, size().height); } public void start() { updater = new Thread(this); dc = DataChannel.getChannel(myChannel, updater, 8); updater.start(); } public void stop() { updater = null; } /** * Return a byte from hex digits. */ byte makeByte(char msd, char lsd) { int m, l; m = Character.toUpperCase(msd) - '0'; if (m > 9) m -= 7; l = Character.toUpperCase(lsd) - '0'; if (l > 9) l -= 7; return (byte)((m << 4) + l); // XXX doesn't detect bogus data } /** * Retrieve the cipher text from the .html file. Normally the * HTML file would have been generated by a cgi script and the * cipher would be triple des or something. However, for our demo * We are using a simple XOR cipher to demonstrate the concept without * running afoul of the ITAR regulations. */ byte [] getCipherText() { Vector data = new Vector(); String s; byte oneLine[]; DataInputStream dis; URL myDoc; boolean capture = false; int totalLen = 0; byte result[]; try { myDoc = getDocumentBase(); dis = new DataInputStream(myDoc.openStream()); // Normally I'd use one of the CharacterDecoder streams while (true) { s = dis.readLine(); if (s == null) break; if (capture && (s.compareTo("-->") == 0)) break; if (! capture) { capture = s.compareTo("