?? newaccountapplet.java
字號:
// Exit to login screen and destroy applet. try { getAppletContext().showDocument(new URL(hushHome)); } catch (MalformedURLException me) { System.out.println("Malformed URL"); } stop(); destroy(); } return true; } /** * This method sets up a text field for a username. */ private void enterUsername(String prompt) { removeAll(); add(this,new Label(prompt),0,0,1,1,0,0,0,0,N,NO,0,0,0,0); add(this, new Label( "Your HushMail email address will be <your-username>@hushmail.com."), 0,1,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label( "Your username should be at least 6 characters but no more than 32 characters."), 0,2,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label( "It should include only letters, numbers, hyphens(-), underscores(_), and dots(.)"), 0,3,1,1,0,0,0,0,N,NO,0,0,0,0); format (usernameField = new TextField(32)); add(this,usernameField,0,4,1,1,0,0,0,0,N,NO,4,0,4,0); format2(submitUsernameButton = new Button("Submit")); add(this,submitUsernameButton,0,5,1,1,0,0,0,1,N,NO,4,0,4,0); paintAll(getGraphics()); usernameField.requestFocus(); } private boolean checkUsername(String name) { if (name.length()< 6) return false; if (name.length()>32) return false; if (!Character.isLetter(name.charAt(0))) return false; for (int x=1; x<name.length(); x++) { char c=name.charAt(x); if (!(Character.isLetterOrDigit(c)||c=='_'||c=='-'||c=='.')) return false; } return true; } /** * This method sets up a text field for a passphrase. */ private void enterPassphrase(String prompt) { removeAll(); add(this,new Label (prompt),0,0,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label ("Remember that your account is only as secure as your passphrase."), 0,1,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label ( "Be sure it's long and contains an unpredictable variety of characters."), 0,2,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label ( "If you lose or forget your passphrase your account will not be accessible."), 0,3,1,1,0,0,0,0,N,NO,0,0,0,0); add(this,new Label ( "WE CANNOT RETRIEVE YOUR PASSPHRASE. So be sure to write it down!"), 0,4,1,1,0,0,0,0,N,NO,0,0,0,0); format (passphraseField=new TextField (64)); passphraseField.setEchoCharacter('*'); passphraseField.setEditable(true); add(this,passphraseField,0,5,1,1,0,0,0,0,N,NO,4,0,4,0); format2(submitPassphraseButton = new Button("Submit")); add(this,submitPassphraseButton,0,6,1,1,0,0,0,1,N,NO,4,0,4,0); paintAll(getGraphics()); passphraseField.requestFocus(); } /** * This method sets up a text field for passphrase confirmation. */ private void confirmPassphrase() { passphraseField.setEditable(false); remove(passphraseField); remove(submitPassphraseButton); add(this,passphraseField,0,5,1,1,0,0,0,0,N,NO,4,0,4,0); add(this,new Label("Please re-enter your passphrase for confirmation"), 0,6,1,1,0,0,0,0,N,NO,0,0,0,0); format(passphraseConfirmField = new TextField(64)); passphraseConfirmField.setEditable(true); passphraseConfirmField.setEchoCharacter('*'); add(this,passphraseConfirmField,0,7,1,1,0,0,0,0,N,NO,4,0,4,0); format2(confirmPassphraseButton = new Button("Submit")); add(this,confirmPassphraseButton,0,8,1,1,0,0,0,1,N,NO,4,0,4,0); paintAll(getGraphics()); passphraseConfirmField.requestFocus(); } /** * This method opens a socket connection and sends a message to the * server to create the new account. */ private void createAccount() { String passphrase = passphraseField.getText(); removeAll(); if (!keysGenerated) { // Do a SHA1 hash of all collected random bytes in blocks of twenty at a time byte[] privKeyBytes = bytes.dequeueBytes(KEYSTRENGTH / 8); int bytesHashed = 0; int remaining = privKeyBytes.length; while ((remaining = privKeyBytes.length-bytesHashed) > 0 ) { byte[] beingHashed = new byte[ (remaining >= 20) ? 20 : remaining ]; System.arraycopy(privKeyBytes,bytesHashed,beingHashed,0,beingHashed.length); byte[] hashedBlock = new HushSHA1().SHA1Hash(beingHashed); System.arraycopy(hashedBlock,0,privKeyBytes,bytesHashed,beingHashed.length); bytesHashed = bytesHashed + beingHashed.length; } ElGamalKeys keys = new ElGamalKeys(privKeyBytes); // wipe array containing source of private key for (int x = 0; x < privKeyBytes.length; x++) privKeyBytes[x]=0; privKeyBytes = null; BlowfishCipher encPriv = new BlowfishCipher(); encPriv.setKey(passphrase.getBytes()); publicKey = Conversions.bytesToHexString(keys.publicKey()); encPrivateKey = Conversions.bytesToHexString(encPriv.encrypt(keys.privateKey())); encPriv = null; /* Pause here while key generation is completed. * After key generation: * publicKey = <a hex string representing the users public key> * encPrivateKey = <a hex string representing the users private key blowfish * encrypted by the passphrase> */ keysGenerated = true; } byte[] passphraseHashBytes = new HushSHA1().SHA1Hash(passphrase); byte[] halfPassphraseHashBytes = new byte[passphraseHashBytes.length/2]; System.arraycopy(passphraseHashBytes, 0, halfPassphraseHashBytes, 0, halfPassphraseHashBytes.length); String passphraseHash = Conversions.bytesToHexString(halfPassphraseHashBytes); try { Socket s = new Socket(serverAddress,serverPort); s.setSoTimeout(60000); eIn = new BufferedInputStream(s.getInputStream()); out = new BufferedOutputStream(s.getOutputStream()); /* Write session ID unencrypted so the server * can find the session key. */ out.write(sessionIDBytes); int infoCount = 4; for (int x=0; x<userinfo.length(); x++) if (userinfo.regionMatches(x,"\n",0,1)) infoCount++; // Send encrypted user information to the server encryptAndWrite("NEWACCOUNT\n" + username +"\n"+ infoCount +"\n"+ publicKey +"\n"+ passphraseHash +"\n"+ encPrivateKey +"\n"+ diskspace +"\n"+ userinfo); readAndDecrypt(); eIn.close(); out.close(); s.close(); if (!in.readLine().equals("OK")) error(); String response = in.readLine(); if (response.equals("NOT APPROVED")) enterUsername("The username you selected is unavailable. " + "Please enter a different username."); else if (response.equals("APPROVED")) { // Wipe Passphrase // Wipe passphrase fields passphraseField.setText(""); passphraseConfirmField.setText(""); passphrase = " "+ " "+ " "+ " "; passphrase = null; removeAll(); add(this, new Label("Your account has been created!"), 0,0,2,1,0,0,0,0,N,NO,0,0,0,0); add(this, new Label("Your username is:"),0,1,1,1,0,0,.5,0,N,NE,0,0,0,0); Label usernameLabel = new Label(username); usernameLabel.setForeground(color); add(this, usernameLabel,1,1,1,1,0,0,.5,0,N,NW,0,0,0,0); add(this, new Label("Your email address is:"),0,2,1,1,0,0,.5,0,N,NE,0,0,0,0); Label emailAddressLabel=new Label(username+"@hushmail.com"); emailAddressLabel.setForeground(color); add(this, emailAddressLabel,1,2,1,1,0,0,.5,0,N,NW,0,0,0,0); format2(loginButton = new Button("Click here to login")); add(this, loginButton,0,4,2,1,0,0,0,1,N,NO,4,0,0,0); paintAll(getGraphics()); } else error(); } catch (IOException ie) {error();} } private void error() { removeAll(); add(this, new Label( "I'm sorry. There has been an error."), 0,0,1,1,0,0,0,0,N,NO,0,0,0,0); add(this, new Label("Please try again later."),0,1,1,1,0,0,0,0,N,NO,0,0,0,0); format2(loginButton = new Button("Return home")); add(this,loginButton,0,2,1,1,0,0,0,1,N,NO,4,0,0,0); paintAll(getGraphics()); } private void format(Component c) { c.setForeground(Color.black); c.setBackground(Color.white); c.setFont(new Font("Helvetica",Font.PLAIN,12)); } private void format2(Component c) { c.setForeground(Color.black); c.setBackground(new Color(192,192,192)); c.setFont(new Font("Helvetica",Font.PLAIN,12)); } /** * Variables and methods for handling GridBagLayouts */ private int C = GridBagConstraints.CENTER; private int NW = GridBagConstraints.NORTHWEST; private int NE = GridBagConstraints.NORTHEAST; private int NO = GridBagConstraints.NORTH; private int B = GridBagConstraints.BOTH; private int H = GridBagConstraints.HORIZONTAL; private int N = GridBagConstraints.NONE; private int R = GridBagConstraints.REMAINDER; /** * This method adds a Component to a Container which uses a GridBagLayout. */ private void add(Container cc, Component comp, int x, int y, int w, int h, int ipx, int ipy, double wtx, double wty, int fill, int anchor, int t, int l, int b, int r) { GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = x; constraints.gridy = y; constraints.gridwidth = w; constraints.gridheight = h; constraints.ipadx = ipx; constraints.ipady = ipy; constraints.weightx = wtx; constraints.weighty = wty; constraints.fill = fill; constraints.anchor = anchor; constraints.insets = new Insets(t,l,b,r); LayoutManager ll = cc.getLayout(); GridBagLayout g = (GridBagLayout)ll; g.setConstraints(comp, constraints); cc.add(comp); } /** * This methods send the parameter string to the * server, calling appropriate encryption methods * and indicating the length. */ private void encryptAndWrite(String output) { try { // Send 16 bytes indicating the length of the output to follow byte[] encOutputBytes = blowfishPipe.stringEncrypt(output); int outLength = encOutputBytes.length; out.write(blowfishPipe.encrypt(Conversions.intToBytes(outLength))); out.write(encOutputBytes); out.flush(); } catch (IOException e) {System.out.println("IOException");} } /** * This method reads in a set of bytes from the server * calls appropriate decryption methods, and stores a resulting * string in a buffer for later access. */ private void readAndDecrypt() throws IOException { // read in 16 bytes indicating the length of the input to follow byte lengthBytes[] = new byte[16]; int bytesRead = eIn.read(lengthBytes); while (bytesRead<16) bytesRead = bytesRead + eIn.read(lengthBytes, bytesRead, 16-bytesRead); int inLength = Conversions.bytesToInt(blowfishPipe.decrypt(lengthBytes)); // read and decrypt the requested number of bytes byte[] eBuf = new byte[inLength]; bytesRead = eIn.read(eBuf); while (bytesRead<inLength) bytesRead = bytesRead + eIn.read(eBuf, bytesRead, inLength-bytesRead); String inString = blowfishPipe.stringDecrypt(eBuf); byte[] trimmedBytes = new byte[inString.length()]; inString.getBytes(0, inString.length(), trimmedBytes, 0); in = new DataInputStream(new ByteArrayInputStream(trimmedBytes)); }} // end NewAccountApplet
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -