title: Simple encryption algorithm for homework. not getting decryption working properly
body: <p>This is a homework question that I can't get my head around at all</p>
  
  <p>Its a very simple encryption algorithm. You start with a string of characters as your alphabet:</p>
  
  <p><code>ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!, .</code></p>
  
  <p>Then ask the user to enter there own string that will act as a <code>map</code> such as:</p>
  
  <p><code>0987654321! .,POIUYTREWQASDFGHJKLMNBVCXZ</code></p>
  
  <p>Then the program uses this to make a <code>map</code> and allows you to enter text that gets encrypted.</p>
  
  <p>For example <code>MY NAME IS JOSEPH</code> would be encrypted as <code>.AX,0.6X2YX1PY6O3</code></p>
  
  <p>This is all very easy, however he said that its a one to one mapping and thus implied that if I enter <code>.AX,0.6X2YX1PY6O3</code> back into the program I will get out <code>MY NAME IS JOSEPH</code></p>
  
  <p>This doesn't happen, because <code>.AX,0.6X2YX1PY6O3</code> becomes <code>Z0QCDZQGAQFOALDH</code></p>
  
  <p>The mapping only works to decrypt when you go backwards but the question implies that the program just loops and runs the one algorithm every time. </p>
  
  <p>Even if some could say that it is possible I would be happy, I have pages and pages of paper filled up with possible workings, but I came up with nothing, the only solution to run the algorithm backwards back I don't think we are allowed to do that.</p>
  
  <p>Any ideas?</p>
  
  <p>Edit:</p>
  
  <p>Unfortunately I can't get this to work (Using the orbit computation idea) What am I doing wrong?</p>
  
  <pre><code>//import scanner class
  import java.util.Scanner;
  
  public class Encryption {
  
      static Scanner inputString = new Scanner(System.in);
      //define alphabet
      private static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!, .";
      private static String map;
      private static int[] encryptionMap = new int[40];//mapping int array
      private static boolean exit = false;
      private static boolean valid = true;
  
      public static void main(String[] args) {
  
          String encrypt, userInput;
          userInput = new String();
  
          System.out.println("This program takes a large reordered string");
          System.out.println("and uses it to encrypt your data");
          System.out.println("Please enter a mapping string of 40 length and the same characters as below but in different order:");
          System.out.println(alpha);
  
          //getMap();//don't get user input for map, for testing!
          map=".ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!, ";//forced input for testing only!
          do{
              if (valid == true){
                  System.out.println("Enter Q to quit, otherwise enter a string:");
                  userInput = getInput();
                  if (userInput.charAt(0) != 'Q' ){//&amp;&amp; userInput.length()&lt;2){
  
                      encrypt = encrypt(userInput);
                      for (int x=0; x&lt;39; x++){//here I am trying to get the orbit computation going
                          encrypt = encrypt(encrypt);
                      }
                      System.out.println("You entered: "+userInput);
                      System.out.println("Encrypted Version: "+encrypt);
                  }else if (userInput.charAt(0) == 'Q'){//&amp;&amp; userInput.length()&lt;2){
                      exit = true;
                  }
  
              }
              else if (valid == false){
                  System.out.println("Error, your string for mapping is incorrect");
                  valid = true;//reset condition to repeat
              }
          }while(exit == false);
          System.out.println("Good bye");
      }
  
      static String encrypt(String userInput){
          //use mapping array to encypt data
          String encrypt;
          StringBuffer tmp = new StringBuffer();
          char current;
          int alphaPosition;
          int temp;
  
  
          //run through the user string
          for (int x=0; x&lt;userInput.length(); x++){
              //get character
              current = userInput.charAt(x);
  
  
              //get location of current character in alphabet
              alphaPosition = alpha.indexOf(current);
              //encryptionMap.charAt(alphaPosition)
  
              tmp.append(map.charAt(alphaPosition));
  
          }
          encrypt = tmp.toString();
          return(encrypt);
      }
  
      static void getMap(){
          //get a mapping string and validate from the user
          map = getInput();
          //validate code
          if (map.length() != 40){
              valid = false;
          }
          else{
              for (int x=0; x&lt;40; x++){
                  if (map.indexOf(alpha.charAt(x)) == -1){
                      valid = false;
                  }
              }
          }
          if (valid == true){
              for (int x=0; x&lt;40; x++){
                  int a = (int)(alpha.charAt(x));
                  int y = (int)( map.charAt(x));
                  //create encryption map
                  encryptionMap[x]=(a-y);
              }
          }
      }
  
      static String getInput(){
          //get input(this repeats)
          String input = inputString.nextLine();
          input = input.toUpperCase();
          if ("QUIT".equals(input) || "END".equals(input) || "NO".equals(input) || "N".equals(input)){
              StringBuffer tmp = new StringBuffer();
              tmp.append('Q');
              input = tmp.toString();
          }
          return(input);
      }
  
  
  
  
  }
  </code></pre>