Plaint text = "FOLLOWDIRECTION"
Autokey = P
This Autokey is polyalphabet Substitution cipher. In this cipher, the key is a stream of subkeys which is each subkey is used to encrypt the corresponding character in the plaintext.
For example
Plaintext --> F O L L O W D I R E C T I O N Key --> P F O L L O W D I R E C T I O
As shown, the key is add the first of subkeys.
Lets Encrypt
              F O L L O W D I R E C T I O N
              P F O L L O W D I R E C T I O
              -----------------------------
          (+) U T Z W Z K Z L Z V G V B W B
              -----------------------------
  
 * can use vigenere table to calculate
Lets Decrypt
Put the key first
              P
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
              
Do subtraction cipher - key. ex: U - P = F . Add F plaintext letter to the end of the keystream.
              F
              P F
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
Do subtraction cipher - key. ex: T - F = O . Again add O plaintext letter to the end of the keystream.
              F O
              P F O
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
 
              F O L
              P F O L
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
              F O L L
              P F O L L
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
continue subtraction until
              F O L L O W D I R E C T I O N
              P F O L L O W D I R E C T I O
              -----------------------------
              U T Z W Z K Z L Z V G V B W B
              -----------------------------
DONE!
Change Snippet Background Color
/*
   Autokey encryption and decryption
*/
import java.util.*;
import java.lang.*;
import java.math.*;
public class Autokey{
   private static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   
   public static void main(String[] args){
      String text = "FOLLOWDIRECTION";
      String key = "P";  //15 - P
      
      if(key.matches("[-+]?\\d*\\.?\\d+"))
         key = ""+alpha.charAt(Integer.parseInt(key));
         
      String enc = AutoEncryption(text,key);
      System.out.println("Plaintext : "+text);
      System.out.println("Encrypted : "+enc);
      System.out.println("Decrypted : "+AutoDecryption(enc,key));
   }
   
   public static String AutoEncryption(String text,String key){
      int len = text.length();
      
      String subkey = key + text;
      subkey = subkey.substring(0,subkey.length()-key.length());
      
      String sb = "";
      for(int x=0;x<len;x++){
         int get1 = alpha.indexOf(text.charAt(x));
         int get2 = alpha.indexOf(subkey.charAt(x));
         
         int total = (get1 + get2)%26;
         
         sb += alpha.charAt(total);
      }
     
      return sb;   
   }
   
   public static String AutoDecryption(String text,String key){
      int len = text.length();
      
      String current = key;
      String sb ="";
      
      for(int x=0;x<len;x++){
         int get1 = alpha.indexOf(text.charAt(x));
         int get2 = alpha.indexOf(current.charAt(x));
         
         int total = (get1 - get2)%26;
         total = (total<0)? total + 26 : total;
         sb += alpha.charAt(total);
         
         current += alpha.charAt(total);
      }
      return sb;
   }
}