Wednesday, December 30, 2015

Polyalphabet Substitution Cipher with Period

This is poly lphabet substitution with period

Example:
Plaintext : IHATEYOU
Period : 3
          ALPHABET : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
          ----------------------------------------------------------------
          PERIOD 1 : B K L Y C P X F I Z J W D A H U R N G S T V Q M O E
          PERIOD 2 : Y U N S I D V T H L C K Q B P R Z M X O J G A W E F
          PERIOD 3 : I N T Q V E W F M G J B S Y U P Z H X C A L O K R D

Those period alphabet is can be done randomly without duplicate.

Let's start
Encryption:
For the first letter of plaintext is for period 1, second letter is for period 2 and third letter for period 3, then start over fourth letter for period 1 and so on.

For example:
 
          I letter of plain text, opposite of I at period 1 is I
          H letter of plain text, opposite of H at period 2 is T
          A letter of plain text, opposite of A at period 3 is I
          .
          .
          .

          Result
          I = I
          H = T
          A = I
          T = S
          E = I
          Y = R
          O = H
          U = J

Cipher text : ITISIRHJ

Lets do decryption
Decryption:
For the encryption part, the substitution from alphabet to period.
For the decryption part, we do reverse from period to alphabet.

As we know, the first of ciphertext is from period 1 and so on. Then, by doing reverse,

          Cipher text : ITISIRHJ

          I letter of plain text, opposite of I at alphabet from period 1 is I
          T letter of plain text, opposite of T at alphabet from period 2 is H
          I letter of plain text, opposite of I at alphabet from period 3 is A
          .
          .
          .
          Result
          I = I
          T = H
          I = A
          S = T
          I = E
          R = Y
          H = O
          J = U
 

Plain text : I HATE YOU


Change Snippet Background Color
          
            
/*
   Polyalphabet Subtitution
   Create by Hafiq
*/

import java.util.*;

public class PolyAlpahbetSub{
   
   private static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   private static ArrayList<String> list = new ArrayList<String>(); 
   
   public static void main(String[] args){
      
      String text = "IHATEYOU";
      int period = 3;
      Period(period);
      
      System.out.println("CT:   "+alpha);
      for(int x=0;x<list.size();x++){
         System.out.println("PT"+x+":  "+list.get(x));
      }
            
      String enc = PASEncryption(text,period);
      System.out.println("\nPlaintext : "+text);
      System.out.println("Encrypted : "+enc);
      System.out.println("Decrypted : "+PASDecryption(enc,period));
   }
   
   public static void Period(int period){
      Random r = new Random();
      
      for(int x=0;x<period;x++){
         String key = "";
         for (int i = 0; i < 26;) {
             char c = (char) (r.nextInt(26) + 'A');
             if(!key.toString().contains(""+c)){
                key = key + c;
                i++;
             }
         }  
         list.add(key);    
      }
   }
   
   public static String PASEncryption(String text,int period){
      int len = text.length();
      
      String sb = "";
      
      int p = 0;
      for(int x=0;x<len;x++){
         int get = alpha.indexOf(""+text.charAt(x));
        
         if (p == period)
            p = 0;
         
         String CT = list.get(p);
         
         sb = sb + CT.charAt(get);
            
         p++;
      }
      
      return sb;
   }
   
   public static String PASDecryption(String text,int period){
      int len = text.length();
      
      String sb = "";
      
      int p = 0;
      for(int x=0;x<len;x++){
         if (p == period)
            p = 0;
         
         int get = list.get(p).indexOf(""+text.charAt(x));
         
         sb = sb + alpha.charAt(get);
            
         p++;
      }
      
      return sb;
   }

}
            
          
        

No comments:

Post a Comment