home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / prfzjhp6 / flash.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  14.1 KB  |  593 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.io.*;
  4. import java.lang.*;
  5. import java.net.*;
  6. import java.util.Date;
  7.  
  8. // ----------------------------------------------------------------------------
  9. // File: Flash.java
  10. // Date: March 28, 1996
  11. // Purpose:
  12. //  This file contains the public class Flash and its supporting classes
  13. //  (TopPiece and BitString). This code implements the client side of a 
  14. //  client/server application. It handles all user interface resposibilities,
  15. //  including:
  16. //  - receiving and displaying the challenge from the server (both in the
  17. //    form of a bar code and textual (numeric) representation
  18. //  - accepting the users input as to their userid and the response generate
  19. //    by the Access Key II, and passing that information onto the server
  20. //    for validation.
  21. //  - indicating the acceptance or denial of the entered information, if
  22. //    accepted the user is sent on to the next Web page.
  23. //  
  24. //
  25. // Authors:
  26. //  Brian Odeen - Vasco Data Security
  27. //  Roger Chen   - TriNet Services, Inc.
  28. //  Fred Burke   - TriNet Services, Inc.
  29. //
  30. // ----------------------------------------------------------------------------
  31.  
  32.  
  33. // ----------------------------------------------------------------------------
  34. // Class: Flash
  35. // Purpose:
  36. //  
  37. // ----------------------------------------------------------------------------
  38.  
  39. public class Flash extends java.applet.Applet implements Runnable {
  40.  
  41. // The following string is the only piece of code that needs to be altered
  42. // in this file. Set its value to be the portion of the URL path up to but
  43. // not including the directory 'flash'. Note that the trailing '/' will be
  44. // appened below where needed.
  45.  
  46.      String validationURL = "http://vdsi.trinet.com:80/flash/servauth.cgi";
  47.      String successURL = "http://vdsi.trinet.com:80/flash/start.html";
  48.     String title;
  49.     String message;
  50.  
  51.     Font titleFont;
  52.     Font msgfont;
  53.     FontMetrics titleFontMetrics;
  54.  
  55.     int titleHeight = 15;
  56.  
  57. //    ChallengePanel cpnl;
  58.     TopPiece tpx;
  59.     Image picture;
  60.  
  61.  
  62.     Thread kicker = null;
  63.     BitString the_flash;
  64.     int loopslot = 0;
  65.     boolean threadSuspended = false;
  66.     boolean restart = false;
  67.     int period = 10;
  68.     int operiod = 10;
  69.     boolean do_i_paint = true;
  70.     boolean entryallowed = false;
  71.     boolean first = true;
  72.     TextField user, rc;
  73.  
  74.     int            columns = 4;
  75.     int            values[];
  76.     Object        colors[];
  77.     Color        bg_col;
  78.     int            size = 4;
  79.     int            scale = 6;
  80.     int            maxLabelWidth = 0;
  81.     int            max = 0;
  82.  
  83.     Image speed;
  84.     Image wide;
  85.     Image logo;
  86.     Image triLogo;
  87.     Image submit, reset;
  88.  
  89.     int cycle = 75;
  90.     public synchronized void init() {
  91.         String new_url;
  92.  
  93.         do_i_paint = true;
  94.          bg_col = new Color(85, 85, 85);    
  95.         setLayout(new BorderLayout());
  96.         setBackground(bg_col);
  97.         resize(440, 360);
  98.         titleFont = new java.awt.Font("Courier", Font.BOLD, 12);
  99.         titleFontMetrics = getFontMetrics(titleFont);
  100.         title = "Access Key II Demo";
  101.         msgfont = new Font("TimesRoman",Font.BOLD,36);
  102.  
  103.         // Attempt to read the applet URL params from the web page
  104.         new_url = this.getParameter("successURL");
  105.         if(new_url != null)
  106.             successURL = new_url;
  107.         new_url = this.getParameter("validationURL");
  108.         if(new_url != null)
  109.             validationURL = new_url;
  110.     
  111.          picture = getImage(getCodeBase(), "images/authkey.gif");
  112.         speed = getImage(getCodeBase(), "images/rate.gif");
  113.         wide = getImage(getCodeBase(), "images/wide.gif");
  114.         triLogo = getImage(getCodeBase(), "images/trinet.gif");
  115.         logo = getImage(getCodeBase(), "images/VSDIrev.gif");
  116.         submit = getImage(getCodeBase(), "images/submit.gif");
  117.         reset = getImage(getCodeBase(), "images/reset.gif");
  118.  
  119.         message = "Ready";
  120.  
  121.         values = new int[columns];
  122.         colors = new Color[columns];
  123.  
  124.         tpx = new TopPiece();
  125.  
  126.         add("North", tpx);
  127.     }
  128.  
  129.     public void start() {
  130.         if (kicker == null)
  131.         {
  132.             kicker = new Thread(this);
  133.             kicker.start();
  134.         }
  135.     }
  136.  
  137.     public void stop() {
  138.         kicker = null;
  139.     }
  140.  
  141.     public void run() {
  142.         int newdata;
  143.         int dataside = 0;
  144.         int state[] = new int[2];
  145.         int nextbit = 0;
  146.  
  147.         while (kicker != null)
  148.         {
  149.             dataside = 0;
  150.             nextbit = 0;
  151.             state[0] = 0;
  152.             state[1] = 0;
  153.             restart = false;
  154.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  155.             the_flash = new BitString();    
  156.             //bpx.setChallengeText(the_flash.kpstring);
  157.             //bpx.setChallengeText(Integer.toString(the_flash.GetRandnum()));
  158.  
  159.             while (restart == false && kicker != null)
  160.             {
  161.                 newdata = the_flash.flashbits[nextbit];
  162.                 if (newdata == state[dataside])
  163.                 {
  164.                     dataside = 1 - dataside;
  165.                     nextbit++;
  166.                     if (nextbit >= the_flash.length)
  167.                         nextbit = 0;
  168.                 }
  169.                 state[dataside] = 1 - state[dataside];
  170.                   loopslot = (state[0] * 2) + state[1];
  171.                 if (do_i_paint == true)
  172.                     repaint();
  173.  
  174.                 if (entryallowed && !do_i_paint) {
  175.                     repaint();
  176.                     try Thread.sleep(2000);
  177.                     catch (InterruptedException e){}
  178.                     newURL(successURL);
  179.                     // Now reset everything so that when we use the back
  180.                     // buttonto get to the challenge/response page it does
  181.                     // not immediately send you on again.
  182.                     do_i_paint = true;
  183.                     entryallowed = false;
  184.                     tpx.response.setText("");
  185.                 }
  186.                 try {
  187.                   Thread.sleep(10 + period);
  188.                 }
  189.                 catch (InterruptedException e){}
  190.             }
  191.  
  192.         }
  193.     }
  194.  
  195.     public synchronized void update(Graphics g) {
  196.         int center_x = 220;
  197.         int org_x = 50, org_y = 80;
  198.         int x1 = 0, x2 = 7, x3 = 10, x4 = 13;
  199.         int w1 = 7, w2 = 3, w3 = 3, w4 = 7;
  200.         int height = 70;
  201.         char kpstmp[];
  202.         
  203.         org_x = center_x - 10 * scale;
  204.         x1 = scale * x1;
  205.         x2 = scale * x2;
  206.         x3 = scale * x3;
  207.         x4 = scale * x4;
  208.         w1 = scale * w1;
  209.         w2 = scale * w2;
  210.         w3 = scale * w3;
  211.         w4 = scale * w4;
  212.         //height = scale * height;
  213.  
  214.         if (do_i_paint == true)
  215.         {
  216.             switch (loopslot)
  217.             {
  218.                 case 0:
  219.                     // present off/on/on/off
  220.                     g.setColor(Color.black);
  221.                     g.fillRect(org_x + x1, org_y, w1, height);
  222.                     g.fillRect(org_x + x4, org_y, w4, height);
  223.                     g.setColor(Color.white);
  224.                     g.fillRect(org_x + x2, org_y, w2, height);
  225.                     g.fillRect(org_x + x3, org_y, w3, height);
  226.                     break;
  227.                 case 1:
  228.                     // present off/on/off/on
  229.                     g.setColor(Color.black);
  230.                     g.fillRect(org_x + x1, org_y, w1, height);
  231.                     g.fillRect(org_x + x3, org_y, w3, height);
  232.                     g.setColor(Color.white);
  233.                     g.fillRect(org_x + x2, org_y, w2, height);
  234.                     g.fillRect(org_x + x4, org_y, w4, height);
  235.                     break;
  236.                 case 2:
  237.                     // present on/off/on/off
  238.                     g.setColor(Color.black);
  239.                     g.fillRect(org_x + x2, org_y, w2, height);
  240.                     g.fillRect(org_x + x4, org_y, w4, height);
  241.                     g.setColor(Color.white);
  242.                     g.fillRect(org_x + x1, org_y, w1, height);
  243.                     g.fillRect(org_x + x3, org_y, w3, height);
  244.                     break;
  245.                 case 3:
  246.                     // present on/off/off/on
  247.                     g.setColor(Color.black);
  248.                     g.fillRect(org_x + x2, org_y, w2, height);
  249.                     g.fillRect(org_x + x3, org_y, w3, height);
  250.                     g.setColor(Color.white);
  251.                     g.fillRect(org_x + x1, org_y, w1, height);
  252.                     g.fillRect(org_x + x4, org_y, w4, height);
  253.                     break;
  254.             }
  255.         }
  256.         else
  257.         {
  258.             g.setColor(bg_col);
  259.             g.fillRect(org_x,org_y,(w1+w2+w3+w4),height);
  260.             g.setFont(msgfont);
  261.             if (entryallowed == true)
  262.             {
  263.                 //System.out.println("allowed");
  264.                 g.setColor(Color.green);
  265.                 g.fillRect(org_x,org_y,(w1+w2+w3+w4),height);
  266.                 g.setColor(Color.black);
  267.                 g.drawString("Access",org_x+10,org_y+30);
  268.                 g.drawString("Allowed",org_x+10,org_y+60);
  269.             }
  270.             else
  271.             {
  272.                 //System.out.println("denied");
  273.                 g.setColor(Color.red);
  274.                 g.fillRect(org_x,org_y,(w1+w2+w3+w4),height);
  275.                 g.setColor(Color.black);
  276.                 g.drawString("Access",org_x+10,org_y+30);
  277.                 g.drawString("Denied",org_x+10,org_y+60);
  278.             }
  279.             //kicker.stop();
  280.                 
  281.             //kicker = null;
  282.         }
  283.  
  284.         //
  285.         // Draw challenge code on screen
  286.         //
  287.         g.setColor(new Color(0, 85, 85));
  288. //        g.fillRect(100, 55, 100, 15);
  289.         g.setFont(titleFont);
  290.         g.setColor(Color.white);
  291.         g.drawString("Challenge: " + the_flash.kpstring, 40, 57);
  292.  
  293.         if(first || ++cycle == 100) {
  294.             if(first) {
  295.                 g.setColor(new Color(85, 85, 85));
  296.                 g.fillRect(0,0, 440,360);
  297.                 }
  298.             paint(g);
  299.             cycle = 0;
  300.             }
  301.     }
  302.  
  303.  
  304.     //
  305.     // newURL() - Force browser to open a new URL
  306.     //   Written:  27-MAR-96 RTC
  307.     //
  308.     private void newURL(String location) {
  309.         URL currentURL = null;
  310.         try {
  311.             currentURL = new URL(location);
  312.         }
  313.         catch( MalformedURLException e ) {
  314.             return;
  315.         }
  316.         if( currentURL != null ) {
  317.             getAppletContext().showDocument( currentURL );
  318.         }
  319.     }
  320.  
  321.  
  322.     public void paint(Graphics g) {
  323. //        this.setBackground(new Color(85, 85, 85));
  324.         g.drawImage(reset, 210, 40, this);
  325.         g.drawImage(submit, 280, 40, this);
  326.         g.drawImage(picture, 170, 170, this);
  327.         g.drawImage(wide, 40, 80, this);
  328.         g.drawImage(speed, 325, 80, this);
  329.         g.drawImage(logo, 275, 190, this);
  330.         g.drawImage(triLogo, 24, 190, this);
  331.         if(threadSuspended) {
  332.             g.setColor(Color.white);
  333.             g.drawString("Applet Suspended", 160, 110);
  334.             g.drawString("Click Here to resume", 150, 130);
  335.             }
  336.         first = false;
  337.         }
  338.  
  339.     private void do_reset() {
  340.         do_i_paint = true;
  341.         entryallowed = false;
  342.         period = operiod;
  343.         message = "Ready";
  344.         restart = true;
  345.         cycle = 90;
  346.         }
  347.  
  348.     private void do_submit() {
  349.         try {
  350.             String vdsiArg =
  351.               validationURL
  352.               + "?userid=" + tpx.username.getText()
  353.               + "&response=" + tpx.response.getText()
  354.               + "&key=" + the_flash.GetRandnum();
  355.               //+ "?userid=testuser"
  356.               //+ "&response=l5uu94"
  357.               //+ "&key=1532";
  358.  
  359.             URL vdsi = new URL(vdsiArg);
  360.             DataInputStream dis;
  361.             String inputLine;
  362.  
  363.             dis = new DataInputStream(vdsi.openStream());
  364.             inputLine = dis.readLine();
  365.             dis.close();
  366.  
  367.             if ("0".equals(inputLine)) {
  368.               // Access Accepted
  369.               do_i_paint = false;
  370.               entryallowed = true;
  371.             }
  372.             else {
  373.               // Access Denied
  374.               do_i_paint = false;
  375.               entryallowed = false;
  376.             }
  377.           }
  378.           catch (MalformedURLException me) {
  379.             System.out.println("MalformedURLException: " + me);
  380.           }
  381.           catch (IOException ioe) {
  382.             System.out.println("IOException: " + ioe);
  383.           }
  384.  
  385.  
  386.           repaint();
  387.         }
  388.  
  389.  
  390.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  391. //        showStatus("MouseDown: [" + x + "," + y + "]");
  392.  
  393.         if(x > 325 && x < 400 && y > 80 && y < 110) {
  394.             period -= 10;
  395.             if(period < 10) period = 10;
  396.             }
  397.         else if(x > 325 && x < 400 && y > 125 && y < 150) {
  398.             period += 10;
  399.             }
  400.         else if(x > 40 && x < 115 && y > 80 && y < 110) {
  401.             if(++ scale > 20) scale = 20;
  402.             }
  403.         else if(x > 40 && x < 115 && y > 110 && y < 150) {
  404.             scale --;
  405.             if(scale < 2) scale = 1;
  406.             }
  407.         else if(x > 210 && x < 260 && y > 40 && y < 65) {
  408.             this.do_reset();
  409.             }
  410.         else if(x > 280 && x < 330 && y > 40 && y < 65) {
  411.             this.do_submit();
  412.             return true;
  413.             }
  414.         else if(x > 24 && x < 132 && y > 190 && y < 334) {
  415.             newURL("http://www.trinet.com/");
  416.             }
  417.         else if(x > 295 && x < 420 && y > 190 && y < 330) {
  418.             newURL("http://www.vdsi.com/");
  419.             }
  420.         else {
  421.             if (threadSuspended)
  422.                 kicker.resume();
  423.             else
  424.                 kicker.suspend();
  425.             threadSuspended = !threadSuspended;
  426.         }
  427.         first = true;
  428.         repaint();
  429.         return true;
  430.     }
  431. }
  432.  
  433.  
  434. // ----------------------------------------------------------------------------
  435. // Class: TopPiece
  436. // Purpose:
  437. //
  438. // ----------------------------------------------------------------------------
  439.  
  440. class TopPiece extends Panel {
  441.     TextField username, response;
  442.     public TopPiece() {
  443. //        setLayout(new BorderLayout());
  444.  
  445.         this.setForeground(Color.white);
  446.         add(new Label("USERNAME:"));
  447.         add(username = new TextField("", 8));
  448.         username.setForeground(Color.white);
  449.  
  450.         add(new Label("   RESPONSE:"));
  451.         add(response = new TextField("", 8));
  452.         response.setForeground(Color.white);
  453.  
  454.         this.setBackground(new Color(85, 85, 85));
  455.     }
  456. }
  457.  
  458.  
  459. // ----------------------------------------------------------------------------
  460. // Class:
  461. // Purpose:
  462. //
  463. // ----------------------------------------------------------------------------
  464.  
  465. class BitString extends java.util.Date {
  466.       int bits[];
  467.     int flashbits[];
  468.     String kpstring;
  469.     int length = 0;
  470.     int randnum = 0;  /* Random # generated by Random */
  471.  
  472.     BitString() {
  473.         int StimLen=18;  
  474.         int Stim[] = new int[41];  /* plain bit string */
  475.         int Stim2[] = new int[41]; /* stuffed bit string */
  476.         int IStim[] = new int[41];   /* temporary copy if 'Stim'     */
  477.         int Randnum = 0;
  478.         int i, k = 0, j = 0;
  479.  
  480.         bits = new int[30];
  481.         flashbits = new int[30];
  482.         for(i=0; i < 40; i++)
  483.         {
  484.             Stim[i] = 0;
  485.             Stim2[i] = 0;
  486.         }
  487.  
  488.            randnum = (int)(2048 * Math.random());
  489.         Randnum = randnum;
  490.  
  491.         /* Place SYNC pattern into Stimulus vector.    */
  492.         Stim[0] = 1;  
  493.         Stim[1] = 1;  
  494.         Stim[2] = 0;
  495.         Stim[3] = 1;  
  496.         Stim[4] = 0;  
  497.         Stim[5] = 0;
  498.  
  499.         /* Place operation code into stimulus vector.  */
  500.         Stim[6] = 1;
  501.  
  502.         /* Place random number into stimulus vector.   */
  503.         for (i = 7 ; i < 17 ; ++i , Randnum >>=1)
  504.             Stim[i] = Randnum & 1;
  505.  
  506.         /* append the CRC to Stim */
  507.  
  508.         /*  Initialize IStim vector, and clear checksum field.   */
  509.         for (i = 0; i < StimLen ; ++i)
  510.             IStim[i] = Stim[i];
  511.         for (i = StimLen; i < StimLen + 6 ; ++i)
  512.             IStim[i] = 1;
  513.  
  514.         /*  Compute the checksum.  This operation destroys the stimulus.   */
  515.         for (i = 6 ; i < StimLen ; ++i) {
  516.              if ( IStim[i] == 1 )
  517.             {
  518.                 IStim[i+5] = 1 - IStim[i+5];
  519.                 IStim[i+6] = 1 - IStim[i+6];
  520.             }
  521.         }
  522.  
  523.         /*  Copy CRC to parameter.     */
  524.         for (i = StimLen; i < StimLen + 6; ++i) Stim[i] = IStim[i];
  525.  
  526.         for (i=0;i<24;i++)
  527.             bits[i] = Stim[i];
  528.         //PrintBitStr(bits, 24);
  529.  
  530.         /* add stuffing if necessary */
  531.  
  532.         StimLen = 24;
  533.  
  534.         for (i = 0 ; i < 6; ++i)
  535.             Stim2[i] = Stim[i];
  536.  
  537.         for (i = 6 ,j = 6 , k = 0; i < StimLen; ++i)
  538.         {    /* once thru loop for each stimulus bit  */
  539.             Stim2[j++] = Stim[i];
  540.             if ( Stim2[k] != Stim[i] )
  541.                 k = 2 * Stim[i]; /* bit does not match sync -- reset 'k'  */
  542.             else /* bit matches sync -- increment 'k'     */
  543.             {
  544.                 if (++k == 5)
  545.                 {         /* insert extra bit                      */
  546.                     k = 1;
  547.                     Stim2[j++] = 1;
  548.                 }  
  549.             }
  550.         }                           /* once thru loop for each stimulus bit  */
  551.  
  552.  
  553.         for(i=0;i<j;i++)
  554.             flashbits[i] = Stim2[i];
  555.         length = j;
  556.  
  557.         char kpstring2[] = new char[20];
  558.         int Digit;
  559.  
  560.         for (i = 0; i < 20; i++)
  561.             kpstring2[i] = 0;
  562.         kpstring2[0] = '9';
  563.         j = 1;
  564.         k = 6;
  565.  
  566.  
  567.         for (i = (length - 4) / 3; i > 0; i--)
  568.         {
  569.             if ((i%4) == 0)
  570.                 kpstring2[j++] = '-';
  571.             Digit = (Stim2[k] * 2 + Stim2[k+1]) * 2 + Stim2[k+2];
  572.             kpstring2[j++] = (char)('1' + Digit);
  573.             k = k + 3;
  574.         }
  575.         kpstring2[j] = 0;
  576.         kpstring = new String(kpstring2);
  577.         //System.out.println(kpstring2);
  578.     }
  579.  
  580.     int GetRandnum() {
  581.       return randnum;
  582.     }
  583.  
  584.     void PrintBitStr(int data[], int count)
  585.     {
  586.         int i;
  587.  
  588.         for (i=0; i<count; i++)
  589.             System.out.print(data[i]);
  590.         System.out.println(" ");
  591.     }
  592. }
  593.