Sunday, March 16, 2014

Character Frequency Analysis in Java | Source Code | Simple Cryptanalysis

Written by Pranshu Bajpai |  | LinkedIn

Source Code


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Pranshu
 */

public class DICAss16_CharFrequency {

    
    public static void main(String[] args) throws FileNotFoundException, IOException {
    
        File f = new File("test.txt");
        
        Scanner input = new Scanner(System.in);
        BufferedWriter w = new BufferedWriter(new FileWriter(f));
                    
        System.out.println("Enter text in book");
        
        w.write(input.nextLine());
        
        w.flush();
        w.close();
        
        BufferedReader r = new BufferedReader(new FileReader(f));
        
        String str;
        String alphabets = "abcdefghijklmnopqrstuvwxyz";
        String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String char1 = "";
        String char2 = "";
        String char3 = "";
        String char4 = "";
        
        str = r.readLine();
        System.out.println(str);
        
        char ch;
        char ch1;
        char ch2;
        char ch3 = 0;
        float[] arr = new float[26];
        int k;
        float len;
        
        // for single character frequency...
        for(int i=0; i < alphabets.length(); i++) {
            ch = alphabets.charAt(i);
            ch1 = alpha.charAt(i);
            k = 0;
            for(int j=0; j < str.length(); j++) {
                if((ch == str.charAt(j))||(ch1 == str.charAt(j))) {
                    k++;
                }
            }
        
            System.out.println("frequency of " + alphabets.charAt(i) + "= " + k);
            len = str.length();
            arr[i] = k / len;
           
        }
        
        System.out.println("________________________________________________");
        
        for(int i = 0; i < alphabets.length(); i++) {
            System.out.println("Probability of " + alphabets.charAt(i) + " = " + arr[i]);
        }
        
        // for  character pair frequency...
        System.out.println("________________________________________________");
        
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
            
                 k = 0;
                for(int l = 0; l < str.length() - 1; l++) {
                    if((ch == str.charAt(l) && ch1 == str.charAt(l+1))) {
                        k++;
                    }
                        
                }
            
                System.out.println("Frequency for " + char1 + char2 + "= "+k);
            }
        }
        
        System.out.println("________________________________________________");
        
        //for character triplet frequency...
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
       
                for(int x = 0; x < 26; x++) {
                    ch2 = (char) alphabets.charAt(x);
                    char3 = "";
                    char3 = char3 + ch2;
                    k = 0;
            
                    for(int l = 0; l < str.length() - 2; l++) {
                        if((ch == str.charAt(l) && ch1 == str.charAt(l+1) 
                                && ch2 == str.charAt(l+2))) {
                            k++;
                        }
                        
                    }
                    
                    System.out.println("for " + char1 + char2 + char3 + "= "+k);                
                }
            }
        }
        
        System.out.println("________________________________________________");
        
        // for character quadruplet frequency...
        for(int i = 0; i < 26; i++) {
            ch = (char) alphabets.charAt(i);
            char1 = "";
            char1 = char1 + ch;
        
            for(int j = 0; j < 26; j++) {
                ch1 = (char) alphabets.charAt(j);
                char2 = "";
                char2 = char2 + ch1;
               
                for(int x = 0; x < 26; x++) {
                    ch2 = (char) alphabets.charAt(x);
                    char3 = "";
                    char3 = char3 + ch2;
               
                    for(int y = 0; y < 26; y++) {
                        ch3 = (char) alphabets.charAt(y);
                        char4 = "";
                        char4 = char4 + ch3;  
                    }
                    
                    k = 0;
                    for(int l = 0; l < str.length() - 3; l++) {
                        if((ch == str.charAt(l) && 
                            ch1 == str.charAt(l+1) && 
                            ch2 == str.charAt(l+2) && 
                            ch3 == str.charAt(l+3))) {
                                k++;
                        }
                    }
                    System.out.println("for " + char1 + char2 + char3 + char4 + "= "+k);                
                }
            }
        }
    }
}

No comments:

Post a Comment