Encrypting Passwords

Data files are generally available to too many people. That means a programmer or technician could use a hex editor to open the file and look up someone's password. Then they could take money out or access some other protected information.

To protect passwords from hackers and other dishonest people, passwords are normally store in encrypted form. This means nobody can actually retrieve the password by reading the file. For example, here is an encrypted password: NCJMH . Can you guess what the real password is?

Maybe the java code for the encryption algorithm will help:

public class Crypto
{  public static void main(String[] args)
   {
      System.out.println(encrypt("testing"));
   }
   
   public static String encrypt(String s)
   {
      String answer = "";
      for (int x = 0; x < s.length(); x++)
      {
         char c = s.charAt(x);
         int code = (int)(c + x + 1);
         char nc = (char)code;
         answer = answer + nc;
      }
      return answer;
   }
}

If you only have the encrypt algorithm, then the only way to guess the password is to GUESS the password, encrypt it, and then check whether it is correct.  

(1) Read the algorithm and see if you can guess what it will do to "testing".

(2) Run the program and check whether you were correct.  If not, think through the algorithm again until you understand the result.

(3) Write the corresponding decrypt algorithm.  Check that it works correctly.

Here are some other possibilities for encryption algorithms.  Choose one that you understand and write the encrypt and decrypt methods.  Check that they work correctly.

(A)

(B)

(C)