Guest
Public paste!

Untitled

By: a guest | Mar 19th, 2010 | Syntax: Java | Size: 5.88 KB | Hits: 58 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. package beast;
  2.  
  3. import java.lang.reflect.MalformedParameterizedTypeException;
  4. /**
  5.  * Represents a dynamic rotor capable of simple substitution in an enigma machine.
  6.  * A rotor has 40 positions.
  7.  * @author Colter LeSage
  8.  */
  9. public class Rotor {
  10.         private static final String string2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ .?!";
  11.         /**
  12.          * One of many standard rotors ready to use right out of the box.
  13.          */
  14.         public static final Rotor ROTOR_I = new Rotor(1,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. !?");
  15.         /**
  16.          * One of many standard rotors ready to use right out of the box.
  17.          */
  18.         public static final Rotor ROTOR_II = new Rotor(8,"0P9OL8IK7UJM6YHN5TGB4?RFV3!EDC2WSX1QAZ. ");
  19.         /**
  20.          * One of many standard rotors ready to use right out of the box.
  21.          */
  22.         public static final Rotor ROTOR_III = new Rotor(13,"QWERTPOIUY1029384756ASDFLKJHG. !?ZXCMNBV");
  23.         /**
  24.          * One of many standard rotors ready to use right out of the box.
  25.          */
  26.         public static final Rotor ROTOR_IV = new Rotor(6,"ISX1QJM6YHN5TGAZ. K7U?RFV3!EDC2WB0P9OL84");
  27.         /**
  28.          * One of many standard rotors ready to use right out of the box.
  29.          */
  30.         public static final Rotor ROTOR_V = new Rotor(2,"WA2Q1GV6PL. N8UHX40OK?M9B7YESZ3TFC5RDIJ!");
  31.        
  32.         private String string1;
  33.         private int rotations;
  34.         private int frequency;
  35.        
  36.         /**
  37.          * @param i determines how often the rotor rotates.  Variety is desired for more robust encryptions.
  38.          * @param a a String containing all letters of the alphabet, the numbers from 0 to 9, a space, a period, an exclamation point, and a question mark
  39.          */
  40.         public Rotor(int i,String a) throws MalformedParameterizedTypeException{
  41.                 string1 = a.toUpperCase();rotations = 0;frequency = i;
  42.                 for(char x = 'A'; x < 'Z' + 1; x++){
  43.                         if(string1.indexOf(x) < 0)
  44.                                 throw new MalformedParameterizedTypeException();
  45.                 }
  46.                 for(char x = '0'; x < '9' + 1; x++){
  47.                         if(string1.indexOf(x) < 0)
  48.                                 throw new MalformedParameterizedTypeException();
  49.                 }
  50.                 if(string1.indexOf('.') < 0)throw new MalformedParameterizedTypeException();
  51.                 if(string1.indexOf(' ') < 0)throw new MalformedParameterizedTypeException();
  52.                 if(string1.indexOf('?') < 0)throw new MalformedParameterizedTypeException();
  53.                 if(string1.indexOf('!') < 0)throw new MalformedParameterizedTypeException();
  54.                 if(frequency < 1)throw new MalformedParameterizedTypeException();
  55.         }
  56.  
  57.         /**
  58.          * Positions the rotor at a desired location
  59.          * @param i an int typically between 0 an 39
  60.          */
  61.         public void position(int i){
  62.                 for(int x = 0; x < rotations; x++)rotate(-1);
  63.                 for(int x = 0; x < i; x++)rotate(1);
  64.         }
  65.        
  66.         /**
  67.          * Sends a char through the substitution algorhythm
  68.          * @param c the char to be converted
  69.          * @param direction 1 or -1 depending on whether moving toward or away from the inverter
  70.          * @return an encrypted char
  71.          */
  72.         public char chug(char c, int direction){
  73.                 if(direction == 1)
  74.                         return string2.charAt(string1.indexOf(c));
  75.                 else
  76.                         return string1.charAt(string2.indexOf(c));
  77.         }
  78.        
  79.         /**
  80.          * rotates the rotor once
  81.          */
  82.         public void rotate(){
  83.                 rotate(1);
  84.         }
  85.  
  86.         private void rotate(int direction){
  87.                 if(direction == 1){
  88.                         string1.replace('Z', '~');
  89.                         string2.replace('Z', '~');
  90.                         for(char x = 'A'; x < 'Z'; x++){
  91.                                 string1.replace(x, (char)(x + 1));
  92.                                 string2.replace(x, (char)(x + 1));
  93.                         }
  94.                         string1.replace('~', 'A');
  95.                         string2.replace('~', 'A');
  96.                         rotations += 1;
  97.                 }
  98.                 else if(direction == -1){
  99.                         string1.replace('A', '~');
  100.                         string2.replace('A', '~');
  101.                         for(char x = 'B'; x <= 'Z'; x++){
  102.                                 string1.replace(x, (char)(x - 1));
  103.                                 string2.replace(x, (char)(x - 1));
  104.                         }
  105.                         string1.replace('~', 'Z');
  106.                         string2.replace('~', 'Z');
  107.                         rotations -= 1;
  108.                 }
  109.                 if(rotations > 39) rotations = 0;
  110.                 if(rotations < 0) rotations = 39;
  111.         }
  112.  
  113.         /**
  114.          * Get the position of the rotor
  115.          * @return the current position of the rotor (0 to 39)
  116.          */
  117.         public int getOrientation(){
  118.                 return rotations;
  119.         }
  120.         /**
  121.          * Get the frequency at which the rotor turns
  122.          * @return the frequency at which the rotor turns
  123.          */
  124.         public int getFrequency(){
  125.                 return frequency;
  126.         }
  127. }
  128.  
  129. package beast;
  130.  
  131. import java.lang.reflect.MalformedParameterizedTypeException;
  132.  
  133. /**
  134.  * Represents digitially a stationary inverter in an enigma machine.
  135.  * An inverter has has two positions.
  136.  * @author Colter LeSage
  137.  */
  138. public class Inverter {
  139.        
  140.         /**
  141.          * A standard inverter ready to use right out of the box.
  142.          */
  143.         public static final Inverter STANDARD_INVERTER = new Inverter("QPWOE?IRUTYLA KSJDHFGZM.XNCBV1!029384756");
  144.        
  145.         private String key;
  146.         private int orientation;
  147.        
  148.         /**
  149.          * @param s a String containing all letters of the alphabet, the numbers from 0 to 9, a space, a period, an exclamation point, and a question mark
  150.          */
  151.         public Inverter(String s){
  152.                 key = s.toUpperCase();
  153.                 for(char x = 'A'; x < 'Z' + 1; x++){
  154.                         if(key.indexOf(x) < 0)
  155.                                 throw new MalformedParameterizedTypeException();
  156.                 }
  157.                 for(char x = '0'; x < '9' + 1; x++){
  158.                         if(key.indexOf(x) < 0)
  159.                                 throw new MalformedParameterizedTypeException();
  160.                 }
  161.                 if(key.indexOf('.') < 0)throw new MalformedParameterizedTypeException();
  162.                 if(key.indexOf(' ') < 0)throw new MalformedParameterizedTypeException();
  163.                 if(key.indexOf('?') < 0)throw new MalformedParameterizedTypeException();
  164.                 if(key.indexOf('!') < 0)throw new MalformedParameterizedTypeException();
  165.                
  166.                 orientation = 1;
  167.         }
  168.        
  169.         /**
  170.          * "flips" the inverter between it's two states; 1 and -1
  171.          */
  172.         public void flip(){
  173.                 orientation = -orientation;
  174.                 StringBuffer firstHalf = new StringBuffer(key.substring(0, 20));
  175.                 key = (firstHalf.reverse()).toString() + key.substring(20, 40);
  176.         }
  177.        
  178.         /**
  179.          * @return returns either 1 or -1 based on the inverter's position
  180.          */
  181.         public int getOrientation(){
  182.                 return orientation;
  183.         }
  184.        
  185.         /**
  186.          * @param c char to be sent through the inverter
  187.          * @return the char that the input char is paired with
  188.          */
  189.         public char chug(char c){
  190.                 return key.charAt(39 - key.indexOf(c));
  191.         }
  192. }