Aashijit
1/30/2016 - 6:50 PM

Key Generator for Noise Driven Encryption Algorithm (Version - 3)

Key Generator for Noise Driven Encryption Algorithm (Version - 3)

import mEssage_into_Binary_File.*;
import Get_into_Array.*;
/**
 * Created by Expresions on 1/30/2016.
 */
public class Generator implements  Listen_for_random_numbers
{  
    int[] random_positions;
    private String password;
    int[][] binary;

    public Generator()
    {
    }

    public void take_input()
    {
        take_input t=new take_input();
        this.password=t.get_password();
        new write(password,7);  //newline character to be inserted
        binary=new get_full_file_into_ram().get();
    }
    
    public void start_random_generation()
    {
      generation_of_random g=new generation_of_random(40,800);  //Number_of_terms and start_term
      g.start();
    }

    @Override
    public void listen(int[] numbers)
    {
      System.out.println("Received The Numbers");
      random_positions=numbers;
    }
}
package Get_into_Array;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;

/**
 * Created by LENOVO on 25-10-2015.
 */
public class get_full_file_into_ram
{
    int[][] array;

    public get_full_file_into_ram()
    {
        //finding the size of the file in the constructor
        int len1,len2=0;
        try
        {
            FileReader fr=new FileReader("binary.txt");
            BufferedReader br=new BufferedReader(fr);
            String tmp=br.readLine();
            len1=tmp.length();

            while(tmp!=null)
            {
                len2++;
                tmp=br.readLine();
            }
            br.close();
            //creating the ram space

            array=new int[len2][len1];
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        initialize_array();
        get_text();
        //print();
    }

    private void initialize_array()
    {
        for(int i=0;i<array.length;i++)
            for(int j=0;j<array[i].length;j++)
            array[i][j]=-1;
    }

    private void get_text()
    {
        //getting the full text in R A M

        try
        {
            FileInputStream fput=new FileInputStream("binary.txt");
            int ch=fput.read();
           for(int i=0;i<array.length && ch!=-1;i++)
           {
               for(int j=0;j<array[i].length && ch!=-1;j++)
               {
                   if(ch==10)
                       ch=fput.read();

                   array[i][j]=ch-48;
                   ch=fput.read();
               }
           }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }


    public int[][] get()
    {
        //System.out.println("RAM VIEW :-\t");
        /*for(int i=0;i<array.length;i++)
        {
            for (int j=0;j<array[i].length;j++)
                System.out.print(array[i][j]+"\t");
            System.out.println();
        }*/
        return array;
    }

}
/**
 * Created by Expresions on 1/30/2016.
 */
public interface Listen_for_random_numbers
{
    public void listen(int[] numbers);
}
import java.math.BigDecimal;

/**
 * Created by Expresions on 1/30/2016.
 */
public class generation_of_random extends Thread
{
    private BigDecimal pi;
    int number_of_terms;
    int start_term;
    public generation_of_random(int number_of_terms,int start_term)
    {
        this.number_of_terms=number_of_terms;
        this.start_term=start_term;
        pi=new BigDecimal(0.0);
    }

    @Override
    public void run()
    {
        int n_o_t=30;
        BigDecimal steen=new BigDecimal(16.00);
        BigDecimal eight=new BigDecimal(8.00);
        BigDecimal four=new BigDecimal(4.00);
        BigDecimal one=new BigDecimal(1.00);
        BigDecimal five=new BigDecimal(5.00);
        BigDecimal six=new BigDecimal(6.00);
        BigDecimal two=new BigDecimal(2.00);
        for (int i=0;i<800;i++)
        {
            BigDecimal ci=new BigDecimal(i);
            BigDecimal tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8,t1;
            tmp1=steen.pow(i);
            tmp2=one.divide(tmp1,n_o_t,BigDecimal.ROUND_DOWN);
            t1=eight.multiply(ci);
            tmp3=four.divide(t1.add(one),n_o_t,BigDecimal.ROUND_DOWN);
            tmp4=two.divide(t1.add(four),n_o_t,BigDecimal.ROUND_DOWN);
            tmp5=one.divide(t1.add(five),n_o_t,BigDecimal.ROUND_DOWN);
            tmp6=one.divide(t1.add(six),n_o_t,BigDecimal.ROUND_DOWN);
            tmp7=tmp3.subtract(tmp4);
            tmp7=tmp7.subtract(tmp5);
            tmp7=tmp7.subtract(tmp6);
            tmp8=tmp7.multiply(tmp2);
            pi=pi.add(tmp8);
        }
        String str="";
        String pii=pi.toString();
        pii=pii.substring(2,pii.length()-1);
        int[] counts=generate_prime(); //to generate the prime positions
        int[] random_digits=extract(counts,pii);
        new Generator().listen(random_digits);
    }
    private int[] generate_prime()
    {
        int[] gen=new int[number_of_terms];
        int count=0;
        for(int i=start_term;count<number_of_terms;i++)
        {
         int n=i;
            int flag=0;
         for(int j=1;j<=(n/2);j++)
         {
             if(n%j==0)
                 flag++;
         }
            if(flag==1)
                gen[count++]=n;
        }
        return gen;
    }

    private int[] extract(int[] numbers,String characters)
    {
        int[] extracted=new int[numbers.length];
        int j;

        for(int i=0;i<numbers.length;i++)
        {
            j=0;
          while(j<numbers[i])
              j++;
         if(j>=characters.length())
         {
             while(j>=characters.length())
             j = j - characters.length();
         }
           extracted[i]=Integer.parseInt(characters.charAt(j)+"");
        }
        return extracted;
    }
}
package mEssage_into_Binary_File;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;

public class write
{
   String password;
    int count_position_of_file;
    int newline=0;  //number of bytes after which we shall insert a newline character

    public write(String message,int newline)
    {
        this.password=message;
        System.out.println(message);
        this.newline=newline;
        count_position_of_file=0;  //initializing the file pointer counter to 0
        change_to_binary_and_write();
    }

    private void change_to_binary_and_write()  //changing o binary values and writing in a .txt file.
    {

        int[] bin; //array to store the binary combination of each ASCII character.

        bin=binary(password.charAt(0));
        write_to_file(bin,1);

        for (int i = 1; i < password.length(); i++)
        {
            bin=binary(password.charAt(i));
            write_to_file(bin,2);
        }
    }

    private void write_to_file(int[] bin,int check)  //checks if it is the first time that the function is called..if yes
    //it creates the file binary.txt anew..
    {
        // method to write each binary combination to a file
        try
        {
            FileWriter fw;
            if(check==1)
                fw=new FileWriter("binary.txt");
            else
                fw=new FileWriter("binary.txt",true);
            BufferedWriter bw=new BufferedWriter(fw);
            PrintWriter pw=new PrintWriter(bw);
            if(count_position_of_file==newline)
            {
                pw.print("\n");
                count_position_of_file=0;
            }
            for(int i=0;i<bin.length;i++)
            {
                if(count_position_of_file==newline)
                {
                    pw.print("\n");
                    count_position_of_file=0;
                }
                pw.print(bin[i]);
                count_position_of_file++;
            }
            pw.close();
        }catch (Exception e)
        {
            e.printStackTrace();   //If file cannot be opened
        }
    }

    //--> method to change each character to binary array...

    private int[] binary(char ascii)
    {
        int[] bin=new int[8];
        int num=(int)ascii;
        int count=0;
        while(num>0)
        {
            bin[count++]=num%2;
            num=num/2;
        }
        if(count<8)
        {
            while(count<8)
            {
                bin[count++]=0;
            }
        }
        for(int i=0;i<4;i++)
        {
            int tmp=bin[i];
            bin[i]=bin[7-i];
            bin[7-i]=tmp;
        }
        return bin;
    }

}
/**
 * Created by Expresions on 1/30/2016.
 */
public class main
{
    public static void main(String[] args)
    {
        Generator g=new Generator();
        g.start_random_generation();
        g.take_input();
    }
}
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by Expresions on 1/30/2016.
 */
public class take_input
{
    String pass;
    public take_input()
    {
        ask_for_input();
    }

    private void ask_for_input() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("\nEnter The Password:-");
            pass = br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String get_password()
    {
        return pass;
    }
}