Aashijit
1/30/2016 - 7:05 PM

Noise Driven Encryption Algorithm Version-2 (Decryption)

Noise Driven Encryption Algorithm Version-2 (Decryption)

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("C:\\Users\\Expresions\\IdeaProjects\\Light_Version\\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("C:\\Users\\Expresions\\IdeaProjects\\Light_Version\\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;
    }

}
0100000
0011000
1011001
0100110
1010111
1001100
0000101
1011111
0010001
0111101
1100010
1110001
0100001
01010		
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;

/**
 * Created by Expresions on 12/25/2015.
 */
public class extract_binary_into_RAM_from_file
{
    int[][] array;
    int[] res;
    int cnt=0;

    public extract_binary_into_RAM_from_file()
    {
        //finding the size of the file in the constructor
        int len1,len2=0;
        try
        {
            FileReader fr=new FileReader("C:\\Users\\Expresions\\IdeaProjects\\decription_Light_Version\\src\\crypt.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("C:\\Users\\Expresions\\IdeaProjects\\decription_Light_Version\\src\\crypt.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==13)
                        ch=fput.read();
                    if(ch==10)
                        ch=fput.read();

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

}
package mEssage_into_Binary_File;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * Created by LENOVO on 18-10-2015.
 */
public class file_into_string
{
    File file;
    public String message=""; //made public so that all the classes can see it
    String tmp="";


    public file_into_string(File file)
    {
        // get the plain text from the file and create the string

        this.file=file;

        // this file contains the plain text

        //creating the string message
        //for(int i=0;i<6;i++)
          //  message=message+(char)2;
       // message=message+"a";
        //new write(message);
       insert();
    }


    private void insert()
    {
        try
        {
            FileReader fr=new FileReader(file);
            BufferedReader br=new BufferedReader(fr);
            String tmp=br.readLine();
            while(tmp!=null)
            {
                message=message+tmp;
                tmp=br.readLine();
            }

            br.close();
            message=message+"a";
            new write(message);

        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public String get_message()
    {
        return message;
    }
}
package mEssage_into_Binary_File;

/**
 * Created by LENOVO on 18-10-2015.
 */
public class tmp_checker
{
    public static void main(String[] args)
    {
      //  File file=new File("C:\\Users\\Expresions\\IdeaProjects\\Light_Version\\src\\plain.txt");
      //  new file_into_string(file);
        //new Get_into_Array.get_full_file_into_ram();
      //  Random random=new Random();
    }
}
package mEssage_into_Binary_File;


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

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

    public write(String message)
    {
        this.message=message;
        System.out.println(message);
        newline=8;
        System.out.println(newline);
        //store the newline position in a new file
        store(newline);
        count_position_of_file=0;  //initializing the file pointer counter to 0
        change_to_binary_and_write();
    }

    private void store(int pass)
    {
        try
        {
            FileWriter fw=new FileWriter("key2.txt");
            BufferedWriter bw=new BufferedWriter(fw);
            PrintWriter pw=new PrintWriter(bw);
            pw.print(pass);
            pw.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    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(message.charAt(0));
        write_to_file(bin,1);

        for (int i = 1; i < message.length(); i++)
        {
            bin=binary(message.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 12/25/2015.
 */
public class test
{
    public static void main(String[] args)
    {
        int[][] matrix;
        extract_binary_into_RAM_from_file e=new extract_binary_into_RAM_from_file();
        matrix=e.get_matrix();
        for(int i=0;i<matrix.length;i++)
        {
            for(int j=0;j<matrix[i].length;j++)
                System.out.print(matrix[i][j]+"\t");
            System.out.println();
        }
        new work_decrypt();
    }

}
package wave_matrix_creation;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * Created by Expresions on 12/24/2015.
 */
public class inner_worker_for_matrix_manipulation
{
    int[][] matrix;
    ArrayList<String> al;
    int[][] phase_matrix;
    ArrayList<String> ak;
    public inner_worker_for_matrix_manipulation(int[][] matrix,ArrayList<String> al,int[][] phase_matrix)
    {
        this.phase_matrix=phase_matrix;
        this.matrix=matrix;
        this.al=al;
        //System.out.println(matrix.length+"\t"+matrix[0].length);
        ak=remove_duplicates(al);
      //  for(int i=0;i<ak.size();i++)
       //     System.out.println(ak);
        System.out.print(".");
        make();
    }
    private ArrayList<String> remove_duplicates(ArrayList<String> al)
    {
        ArrayList<String> list= new ArrayList<>();
        HashSet<String> set=new HashSet<>();
        for(String item:al)
        {
            if(!set.contains(item))
            {
                list.add(item);
                set.add(item);
            }
        }
     return list;
    }

    private void make()
    {
        for(int i=0;i<ak.size();i++)
        {
         String[] str=ak.get(i).split(",");

         int x=Integer.parseInt(str[0]);
         int y=Integer.parseInt(str[1]);
         //   System.out.println(x+"\t"+y);
         binary_operation(Math.abs(x),Math.abs(y));
        }
    }

    private void binary_operation(int x,int y)
    {
        if( x<matrix[0].length &&  y<matrix.length)
        {
            if(matrix[x][y]>=0)
                operation(x,y);
        }
    }
    private void operation(int x,int y)
    {
        //matrix[x][y]=(matrix[x][y] + phase_matrix[x][y])%2;
        if(matrix[x][y]==phase_matrix[x][y])
        {
            matrix[x][y]=1;
        }
        else {
            matrix[x][y] = 0;
        }
    }

    public int[][] getMatrix()
    {
        return matrix;
    }

}
package wave_matrix_creation;

/**
 * Created by Expresions on 12/24/2015.
 */
public class matrix_creation
{
    int[][] matrix;
    public matrix_creation(int[][] matrix)
    {
        this.matrix=matrix;
        for(int i=0;i<matrix.length;i++)
            for(int j=0;j<matrix[i].length;j++)
                matrix[i][j]=0;
    }
    public int[][] getMatrix()
    {return matrix;}
}
package wave_matrix_creation;

import java.util.ArrayList;

/**
 * Created by Expresions on 12/24/2015.
 */
public class phase_matrix_creation
{
    int[][] phase_matrix;
    ArrayList<String> al;
    int element;
    public phase_matrix_creation(int[][] phase_matrix, ArrayList<String> al,int element)
    {
        this.element=element;
        this.phase_matrix=phase_matrix;
        this.al=al;
        //initialize();
        make_matrix();
    }

    public void initialize()
    {
        for(int i=0;i<phase_matrix.length;i++)
            for(int j=0;j<phase_matrix[i].length;j++)
                phase_matrix[i][j]=0;
    }
    private void make_matrix()
    {       //two consecutive points will be of different phases
        for(int i=0;i<al.size();i++)
        {
            String[] str=al.get(i).split(",");
            int x=Integer.parseInt(str[0]);
            int y=Integer.parseInt(str[1]);
            if(x>=0 && x<phase_matrix.length  && y>=0 && y<phase_matrix[0].length) {
                phase_matrix[Math.abs(x)][Math.abs(y)]=element;
            }
        }
    }
    public int[][] getPhase_matrix()
    {
        return phase_matrix;
    }

}
package wave_matrix_creation;

import java.util.ArrayList;

/**
 * Created by Expresions on 12/24/2015.
 */

public class wave_generation
{
    int[][] matrix;
    int centreX,centreY;
    int radius;
    int steplength;
    int stepadder;
  int sx,sy;
    public wave_generation(int[][] matrix,int centreX,int centreY,int radius,int steplength,int stepadder)
    {
     this.radius=radius;
     this.stepadder=stepadder;
     this.steplength=steplength;
     this.centreX=centreX;
     this.centreY=centreY;
     this.matrix=matrix;
        sx=matrix[0].length;
        sy=matrix.length;

     create();
    }

    private void create()
    {
        matrix_creation m=new matrix_creation(matrix);
        matrix=m.getMatrix();
        System.out.println("wave generation:-\t"+matrix.length+"\t"+matrix[0].length);
        ArrayList<String> aa=new ArrayList<>();
        aa=wave_generate(centreX,centreY,aa,1);
        int step=20; //this is to be chosen by the user
        for(int i=0;i<2;i++)
        {
            aa = crack(aa, step);
          //  for(int j=0;j<aa.size();j++)
            //    System.out.println(aa.get(j));
            step=step*20;
        }
    }
    private ArrayList<String> crack(ArrayList<String> al,int step)
    {
        ArrayList<String> al1=new ArrayList<>();
        for(int i=0;i<al.size();i++)
        {
            ArrayList<String> al2=new ArrayList<>();
            String[] as=al.get(i).split(",");
            int x=Integer.parseInt(as[0]);
            int y=Integer.parseInt(as[1]);
            wave_generate(x,y,al2,step);
            al1.addAll(al2);
        }
        return al1;
    }


    private  ArrayList<String> wave_generate(int h,int k,ArrayList<String> aa,int step)
    {
        //int step=10; //radius is to be chosen from the key supplied by the user
        int[][] phase_matrix=new int[sx][sy];
        for(int i=0;i<matrix.length;i++) {
            ArrayList<String> al=new ArrayList<>();
            wave(h, k, i, al, step);
            //step++;
            int ele=0;
            if(i%2==0)
                ele=1;

            phase_matrix_creation p=new phase_matrix_creation(phase_matrix,al,ele);
            phase_matrix=p.getPhase_matrix();
        /*   for(int l=0;l<phase_matrix.length;l++)
            {
                for(int j=0;j<phase_matrix[l].length;j++)
                    System.out.print(phase_matrix[l][j]+"\t");
                System.out.println();
            }
            System.out.println();*/
            aa.addAll(al);
        }

        inner_worker_for_matrix_manipulation i=new inner_worker_for_matrix_manipulation(matrix,aa,phase_matrix);
        matrix=i.getMatrix();
        return aa;
    }

    private void wave(int h,int k,int r,ArrayList<String> al,int step)
    {
        if(r==0) //not bothering if the radius is zero
        {
            al.add(h+","+k);
            return ;
        }
        int x,y,xend;
        xend=r/(int)Math.sqrt(2);
        x=0;
        while(x<=xend)
        {
            y=(int)Math.sqrt(r*r-x*x);
            al.add((x+h)+","+(y+k));
            al.add((y+h)+","+(x+k));
            al.add((-x+h)+","+(y+k));
            al.add((x+h)+","+(-y+k));
            al.add((-x+h)+","+(-y+k));
            al.add((-y+h)+","+(x+k));
            al.add((y+h)+","+(-x+k));
            al.add((-y+h)+","+(-x+k));
            x=x+step;
        }
    }
    public int[][] getMatrix()
    {
        return matrix;
    }


}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;

/**
 * Created by Expresions on 12/25/2015.
 */
public class work_decrypt
{
    String key;
    int[][] matrix;
    int sizex,sizey;
    int nox;
    String result;
    public work_decrypt()
    {
        result="";
        System.out.println("ENTER THE KEY GIVEN");
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            key = br.readLine();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("KEY ENETERED:-\t"+key);
        get_size();
        number_of_matrix_genertion();
        extract_binary_into_RAM_from_file e=new extract_binary_into_RAM_from_file();
        matrix=e.get_matrix();
        break_key_and_work();
        get_ascii();
    }
    private void number_of_matrix_genertion()
    {
        if(sizex>8)
        {
            nox=sizex/8+1;
        }
    }
    private void break_key_and_work()
    {
        int[][] wave=new int[7][7];
        int countx=0;
        for(int i=0;i<key.length()-1;i+=2)
        {
         int c1=(int)key.charAt(i)-48;
         int c2=(int)key.charAt(i+1)-48;
            wave_matrix_creation.wave_generation w=new wave_matrix_creation.wave_generation(wave,c1,c2,10,1,1);
            wave=w.getMatrix();
            work(wave,countx);
            countx+=7;
        }
    }
    private void get_size()
    {
        try {
            FileReader fr = new FileReader("C:\\Users\\Expresions\\IdeaProjects\\decription_Light_Version\\src\\crypt.txt");
            BufferedReader br = new BufferedReader(fr);
            String tmp = br.readLine();
            String tmp1 = tmp;
            while (tmp != null)
            {
                sizex++;
                tmp = br.readLine();
            }
            sizey = tmp1.length();
            System.out.println(sizex + "\t" + sizey);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void work(int[][] wave_matrix,int countx)
    {

            for(int i=countx+0;i<countx+7;i++)
            {
                for(int j=0;j<7;j++)
                {
                    if(i<matrix.length && j<matrix[0].length)
                    {
                        complement(matrix,i,j,wave_matrix,countx);
                    }
                }
            }
    }

    private void complement(int[][] matrix,int i,int j,int[][] wave_matrix,int countx)
    {
        //System.out.println(i+"\t"+countx);
        if(wave_matrix[i-countx][j]==1)
        {
            if(matrix[i][j]==1)
                matrix[i][j]=0;
            else if(matrix[i][j]==0)
                matrix[i][j]=1;
            else
            {}

        }
    }
    private void get_ascii()
    {
        int[] num=new int[8];
        int count=0;
        //System.out.println("ASCII VALUES :-\t");
        for(int i=0;i<matrix.length;i++)
        {
            for(int j=0;j<matrix[i].length;j++)
            {
                if(count==8)
                {
                    count=0;
                    j=j-1;
                    // System.out.println();
                    get_value(num);
                }
                else
                    num[count++] = matrix[i][j];
                //  System.out.print(num[count++]);
            }
        }
        print();

    }


    private void get_value(int[] x)
    {
        //for(int i=0;i<x.length;i++)
        //System.out.print(x[i]+"\t");
        //System.out.println();

        double sum=0.0;
        for(int i=x.length-1;i>=0;i--)
        {
            sum=sum+x[i]*Math.pow(2,(x.length-1)-i);
        }
        result=result+(int)sum+" ";
        // System.out.print((char)(int)sum+"\t");
    }
    private void print()
    {
        result=result.substring(0,result.length()-4);
        System.out.println();
        System.out.println("DECRYPTED ASCII VALUES:-\t");
        System.out.println(result);
    }



}