home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / www / auth / basic.java < prev   
Encoding:
Java Source  |  1997-01-27  |  4.6 KB  |  192 lines

  1. /*
  2.  * @(#)basic.java    1.10 95/08/29 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net.www.auth;
  21.  
  22. import java.io.*;
  23.  
  24. /**
  25.  *  From RFC 1421
  26.  *
  27.  * @version 1.10 08/29/95
  28.  * @author Jonathan Payne
  29. *
  30.  * This class has a lowercase name because we usually create an
  31.  * instance of it by catenating the name of the current
  32.  * authentication type with the package name.  In order to guarantee
  33.  * a consistent pattern we always convert the authentication type to
  34.  * lower case.
  35.  */
  36. public class basic extends Authenticator {
  37.     static private byte six2print[] = {
  38.     'A','B','C','D','E','F','G','H','I','J','K','L','M',
  39.     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  40.     'a','b','c','d','e','f','g','h','i','j','k','l','m',
  41.     'n','o','p','q','r','s','t','u','v','w','x','y','z',
  42.     '0','1','2','3','4','5','6','7','8','9','+','/'
  43.     };
  44.  
  45.     static private byte print2six[] = new byte[128];
  46.     static final int    ILLEGAL = -1;
  47.  
  48.     static {
  49.     int i, limit;
  50.  
  51.     limit = six2print.length;
  52.  
  53.     for (i = print2six.length; --i >= 0; ) {
  54.         print2six[i] = (byte) ILLEGAL;
  55.     }
  56.  
  57.     for (i = 0; --limit >= 0; i++) {
  58.         print2six[six2print[i]] = (byte) i;
  59.     }
  60.     }
  61.  
  62.     public void encrypt(InputStream is, OutputStream os) throws IOException {
  63.     int chunk;
  64.     int c = -1;
  65.     int lineCnt = 64;
  66.     int shift;
  67.  
  68. loop:
  69.     do {
  70.         shift = 24;
  71.         chunk = 0;
  72.  
  73.         while ((shift -= 8) >= 0) {
  74.         switch (c = is.read()) {
  75.         case -1:
  76.             break loop;
  77.  
  78.         default:
  79.             chunk |= (c << shift);
  80.             break;
  81.         }
  82.         }
  83.         shift = 24;
  84.         while ((shift -= 6) >= 0) {
  85.         os.write(six2print[(chunk >> shift) & 0x3f]);
  86.         }
  87.         if ((lineCnt -= 4) == 0) {
  88.         os.write('\n');
  89.         lineCnt = 64;
  90.         }
  91.     } while (true);
  92.  
  93.     switch (shift) {
  94.     case 16:    /* 0 bits left to process */
  95.         break;
  96.  
  97.     case 8:        /* 8 bits (1 byte) left to process */
  98.         os.write(six2print[(chunk >> 18) & 0x3f]);
  99.         os.write(six2print[(chunk >> 12) & 0x3f]);
  100.         os.write('=');
  101.         os.write('=');
  102.         break;
  103.  
  104.     case 0:        /* 16 bits (2 bytes) left to process */
  105.         os.write(six2print[(chunk >> 18) & 0x3f]);
  106.         os.write(six2print[(chunk >> 12) & 0x3f]);
  107.         os.write(six2print[(chunk >> 6) & 0x3f]);
  108.         os.write('=');
  109.         break;
  110.     }
  111.     }
  112.  
  113.     public void decrypt(InputStream is, OutputStream os) throws IOException {
  114.     int c;
  115.     int shift;
  116.     int chunk;
  117.     int charCnt = 64;
  118.  
  119. loop:
  120.     do {
  121.         shift = 24;
  122.         chunk = 0;
  123.         while ((shift -= 6) >= 0) {
  124.         switch (c = is.read()) {
  125.         case '=':
  126.             break loop;
  127.  
  128.         case '\n':
  129.             if (charCnt == 0) {
  130.             charCnt = 64;
  131.             continue loop;
  132.             }
  133.             throw new IOException("Invalid encryption");
  134.  
  135.         case -1:
  136.             if (shift == 18) {
  137.             throw new IOException("Invalid encryption");
  138.             }
  139.             break loop;
  140.  
  141.         default:
  142.             {
  143.             int cc = print2six[c];
  144.  
  145.             switch (cc) {
  146.             case ILLEGAL:
  147.                 throw new IOException("Invalid encryption");
  148.  
  149.             default:
  150.                 chunk |= (cc << shift);
  151.                 break;
  152.             }
  153.             }
  154.             break;
  155.         }
  156.         }
  157.         shift = 24;
  158.         while ((shift -= 8) >= 0) {
  159.         os.write((chunk >> shift) & 0xff);
  160.         }
  161.         charCnt -= 4;
  162.     } while (true);
  163.  
  164.     switch (shift) {
  165.     case 18:    /* nothing to process */
  166.         break;
  167.  
  168.     case 12:    /* one 6 bit entity is illegal */
  169.         throw new IOException("Invalid encryption");
  170.  
  171.     case 6:        /* two 6 bit entities = one 8 bit byte */
  172.         os.write((chunk >> 16) & 0xff);
  173.         break;
  174.  
  175.     case 0:
  176.         os.write((chunk >> 16) & 0xff);
  177.         os.write(chunk & 0xff);
  178.         break;
  179.     }        
  180.     }
  181.  
  182.     static public void main(String args[]) throws IOException {
  183.     Authenticator a = new basic();
  184.     FileOutputStream os = new FileOutputStream("/home/jpayne/livejava/test.uu");
  185.  
  186.     a.encrypt(new FileInputStream("/home/jpayne/livejava/test.ascii"), os);
  187.     os.close();
  188.     a.decrypt(new FileInputStream("/home/jpayne/livejava/test.uu"), System.out);
  189.     System.out.flush();
  190.     }
  191. }
  192.