?? newaccountapplet.java
字號:
/* * File: NewAccountApplet.java * * The main applet code that creates a new hushmail account. * * version 1.02 v1a * Copyright 1998, 1999 by Hush Communications Corporation, BWI *//* * Applet parameters: * * username = < the users HushMail account > * userinfo = < a list of items of data on the user seperated by & > * diskspace = < the diskspace to be allocated to the user in bytes > * loginpage = < URL of HushMail login page > * sessionID = < a 64 bit hex number represented as a string > * sessionKey = < a 128 bit hex number represented as a string > * R0 = < red value for applet color > * G0 = < green value for applet color > * B0 = < blue value for applet color > * R1 = < red value for applet color > * G1 = < green value for applet color > * B1 = < blue value for applet color > */package hushcode;import java.awt.*;import java.applet.*;import java.net.*;import java.io.*;import java.util.*;import hushcode.HushSHA1;import hushcode.BlowfishCipher;import hushcode.ElGamalKeys;import hushcode.Conversions;import hushcode.ByteQueue;final public class NewAccountApplet extends Applet{ private static final int KEYSTRENGTH = 1024; private boolean keysGenerated = false; private String encPrivateKey; private String publicKey; /** * Variables concerning obtaining random numbers from mouse movement. */ // Coordinates of the top left corner of the main 256X256 square int mainx; int mainy; int mainwidth = 256; int mainheight = 256; // Previous x and y coordinates int px; int py; // Arrays of x and y defining several squares int numberOfSquares = 128; int[] sx = new int[numberOfSquares]; int[] sy = new int[numberOfSquares]; int squares[][] = new int[mainwidth][mainheight]; int sw = 12; int sh = 12; // True until random squares have been generated private boolean initializing; /* To track which squares have been hit * numberOfsqares (128) indicates that square has not yet been hit (initialized state) */ int square0=numberOfSquares; //current square int square1=numberOfSquares; //previous squares int square2=numberOfSquares; // Every ten zone changes get system time. Track with this variable. int squareCounter = 0; // Indicates whether mouse movements still need to be used to queue bits. private boolean getMouse = true; private ByteQueue bytes; private int bitsNeeded = KEYSTRENGTH; private int nextGraphNotchAt = bitsNeeded/32; /** * Other variables */ private String serverAddress; private int serverPort = 21; private String hushHome = ""; private Random rand; long randSeed = 0; private Graphics g; private Color color; private TextField usernameField; private String username; private String userinfo; private String diskspace; private TextField passphraseField; private TextField passphraseConfirmField; private Label aLabel; private Label bLabel; private Button loginButton; private Button submitPassphraseButton; private Button confirmPassphraseButton; private Button submitUsernameButton; // The pseudo stream cipher for communication between applet and server private BlowfishCipher blowfishPipe; // The sessionID. Used by server to locate sessionKey. private byte[] sessionIDBytes; // An output stream to the socket private BufferedOutputStream out; // An input stream from the socket. private BufferedInputStream eIn; /* An input stream from a decrypted String which is used as a buffer * to store data from eIn after it has been decrypted with blowfishPipe */ private DataInputStream in; public void init() { username = getParameter("username"); userinfo = getParameter("userinfo").replace('&','\n')+"\n"; diskspace= getParameter("diskspace"); hushHome = getParameter("loginpage"); color = new Color(Integer.parseInt(getParameter("R1")),Integer.parseInt(getParameter("G1")),Integer.parseInt(getParameter("B1"))); serverAddress = getCodeBase().getHost(); bytes = new ByteQueue(); // Convert the sessionID from a hex string to a byte array sessionIDBytes = Conversions.hexStringToBytes(getParameter("sessionID")); /* Convert the sessionKey from a hex string to a byte array and use * it to instantiate the CBC Blowfish cipher which will encrypt * communication between applet and server. */ byte[] sessionKeyBytes = Conversions.hexStringToBytes(getParameter("sessionKey")); blowfishPipe = new BlowfishCipher(); blowfishPipe.setKey(sessionKeyBytes); setFont(new Font("Helvetica",Font.PLAIN,12)); setBackground(new Color(Integer.parseInt(getParameter("R0")),Integer.parseInt(getParameter("G0")),Integer.parseInt(getParameter("B0")))); setForeground(Color.white); setLayout(new GridBagLayout()); aLabel = new Label("You must now generate some random numbers to create your keys."); add(this,aLabel,0,0,1,1,0,0,0,0,N,NO,0,0,0,0); bLabel = new Label("Move the mouse in the box until the graph fills"); add(this,bLabel,0,1,1,1,0,0,0,1,N,NO,0,0,0,0); add(bLabel); g = this.getGraphics(); initializing = true; } public void paint(Graphics g) { paintComponents(g); /* Draw a rectangle for the user to move the mouse in, and a bar graph * to track the random bits enqueued. */ if (getMouse) { mainx=bounds().x+(bounds().width-mainwidth)/2; mainy=bounds().y+aLabel.bounds().height+bLabel.bounds().height+10; Rectangle r = new Rectangle(mainx,mainy,mainwidth,mainheight); g.setColor(color); g.fillRect(r.x,r.y,r.width,r.height); // Set up graph based on the length of the desired bit array for (int x=mainx; x<mainx+mainwidth; x=x+8) { Rectangle br = new Rectangle(x+2, mainy+mainheight+10, 4, 20); g.fillRect(br.x, br.y, br.width, br.height); } } g.setColor(Color.white); } public boolean mouseMove(Event e, int x, int y) { if (x<mainx || y<mainy || x>=mainx+mainwidth || y>=mainy+mainheight) return true; else if (initializing) { // Seed the random number generator randSeed = randSeed ^ ((long)x<<40)+((long)y<<32)+new Date().getTime(); rand = new Random(randSeed); // Randomly generate a number of squares in the main square boolean goodSquare = true; for (int n=0; n<numberOfSquares; n++) for(; ;) { goodSquare = true; sx[n] = Math.abs(rand.nextInt())%(mainwidth-sw); sy[n] = Math.abs(rand.nextInt())%(mainheight-sh); for (int nn=0;nn<n;nn++) if (!(sx[n]<sx[nn]-sw||sy[n]<sy[nn]-sh||sx[n]>sx[nn]+sw||sy[n]>sy[nn]+sh)) { goodSquare = false; break; } if (goodSquare) break; } // Clear the squares squares = new int[mainheight][mainwidth]; for (int n=0; n<numberOfSquares; n++) for (int xx=sx[n]; xx<sx[n]+sw; xx++) for (int yy=sy[n]; yy<sy[n]+sh; yy++) squares[xx][yy] = n+1; initializing = false;/* // For debugging, show squares. Rectangle r = new Rectangle(mainx,mainy,mainwidth,mainheight); g.setColor(color); g.fillRect(r.x,r.y,r.width,r.height); g.setColor(Color.white); for (int n=0; n<numberOfSquares; n++) g.fillRect(mainx+sx[n],mainy+sy[n],sw,sh);*/ } else if (getMouse) { boolean inSquare = false; int n = squares[x-mainx][y-mainy]-1; if (n!=-1) { //System.out.println("Hit square "+n); square2 = square1; square1 = square0; square0 = n; /* Exit if square is one of past two hit, or this is one of first * three squares hit (initialized state - no history) */ if (square0==square1 || square0==square2 || square1==numberOfSquares || square2==numberOfSquares) return true; inSquare = true; } if (!inSquare) return true; // Get first five bits (5 LSB's of square #) bytes.enqueueBits(square0,5); /* Split square in half vertically (and then horizontally) * and take one bit based on side mouse is on. */ bytes.enqueueBits( (x-sx[square0] < (sw/2) ) ? 0:1 , 1); bytes.enqueueBits( (y-sy[square0] < (sh/2) ) ? 0:1 , 1); /* Get eighth bit * (left or right of square) xor (top or bottom of square) xor (MSB of square #) */ if ( ((x-sx[square0] < (sw/2)) ^ (y-sy[square0] < (sh/2) ) ^ (square0 < (numberOfSquares / 2)))) bytes.enqueueBits(0,1); else bytes.enqueueBits(1,1); if (squareCounter++ == 8) { initializing = true; squareCounter = 0; } if (bytes.bitsEnqueued()-1 >= nextGraphNotchAt) { // Fill another notch in the graph Rectangle br = new Rectangle( mainx + ((bytes.bitsEnqueued()-1) / (bitsNeeded/32)-1) * 8 + 2, mainy + mainheight + 10, 4, 20 ); g.fillRect(br.x, br.y, br.width, br.height); nextGraphNotchAt = nextGraphNotchAt + bitsNeeded/32; } if (bitsNeeded<bytes.bitsEnqueued()) { getMouse = false; // Erase rectangle and graph. g.setColor(Color.black); Rectangle ar = this.bounds(); g.fillRect(ar.x, ar.y, ar.width, ar.height); repaint(); enterPassphrase("Now please enter a passphrase."); } } return true; } /** * Handle button clicks and returns. */ public boolean action(Event e, Object o) { if ((e.target==passphraseField || e.target==submitPassphraseButton) && passphraseField.getText().length()>0) confirmPassphrase(); else if ((e.target==passphraseConfirmField || e.target==confirmPassphraseButton) && passphraseConfirmField.getText().length()>0) { // Check if user entered the same passphrase in both fields if (passphraseField.getText().equals(passphraseConfirmField.getText())) createAccount(); else enterPassphrase("Your passphrase didn't match. Try again."); } else if ((e.target==usernameField || e.target==submitUsernameButton) && usernameField.getText().length()>0) { // Check for validity of entered username username = usernameField.getText().toLowerCase(); if (!checkUsername(username)) enterUsername("You entered a bad username. Try again."); else createAccount(); } else if (e.target==loginButton) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -