Monday, April 24, 2017

Rail Fence Cipher Java implementation



Source Code:


/* railfence cipher*/
package railfence;
import java.util.*;

public class RailFence {


public static void main(String arg[]){


System.out.println("Enter the number of rails:");
Scanner in=new Scanner (System.in);
int rails=in.nextInt();



System.out.println("Enter the plaintext for encryption");
Scanner inn=new Scanner (System.in);
String plaintext=inn.next();

encryption(plaintext,rails);


System.out.println("------------------Decryption process start----------");

System.out.println("Enter the number of rails:");
rails=in.nextInt();
System.out.println("Enter the ciphertext for decryption:");
String ciphertext=in.next();
decryption(ciphertext,rails);

}
public static void encryption(String str,int rails){

boolean checkdown=false;  //check whether it is moving downward or upward
int j=0;
int row=rails;                  // no of row is the no of rails entered by user
int col=str.length();             //column length is the size of string
char[][] a=new char[row][col];
//we create a matrix of a of row *col size

for(int i=0;i<col;i++){  //matrix visitin in rails order and putting the character of plaintext


if(j==0||j==row-1)
checkdown=!checkdown;
a[j][i]=str.charAt(i);
if(checkdown){


j++;
}
else
j--;
}

//visiting the matrix in usual order to get ciphertext
for(int i=0;i<row;i++){
for(int k=0;k<col;k++){
System.out.print(a[i][k]+"  ");
}
System.out.println();
}
String en="";

System.out.println("----------------------");
for(int i=0;i<row;i++){
for(int k=0;k<col;k++){
if(a[i][k]!=0)
en=en+a[i][k];

}

}
System.out.println(en);//printing the ciphertext


}



public static void decryption(String str,int rails){


boolean checkdown=false;
int j=0;
int row=rails;
int col=str.length();
char[][] a=new char[row][col];

//first of all mark the rails position by * in the matrix
for(int i=0;i<col;i++){
if(j==0||j==row-1)
checkdown=!checkdown;



a[j][i]='*';
if(checkdown)j++;
else j--;

}


//now enter the character of cipheetext in the matrix positon that have * symbol
int index=0;

for(int i=0;i<row;i++){
for(int k=0;k<col;k++){


if(a[i][k]=='*'&&index<str.length()){
a[i][k]=str.charAt(index++);




}

}



}

// visit each character in rails order as character are put in the encryption function
for(int i=0;i<row;i++){
for (int k=0;k<col;k++){
System.out.print(a[i][k]+ "\t");
}
System.out.println();
}


checkdown=false;
String s="";
j=0;

for(int i=0;i<col;i++){
if( j==0||j==row-1)
checkdown=!checkdown;


s+=a[j][i];


if(checkdown)j++;
else j--;

}



System.out.print(s);//print the plaintext that was decrypted by rail fence cipher







}
}


Saturday, April 8, 2017

PlayFair Cipher

JAVA IMPLEMENTATION OF PLAYFAIR CIPHER




package playfair;

import java.awt.Point;
import java.util.Scanner;

public class PlayFair {
public static char [][] a=new char[5][5];
public static String alpha="abcdefghijklmnopqrstuvwxyz";

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("Enter the key ");
Scanner in =new Scanner(System.in);
String key=in.nextLine();
setMatrix(key);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Enter the message to Encryption(plaintext):");
String plaintext=in.nextLine();
encryption(plaintext);
System.out.println("Enter the message to Decryption");
String ciphertext=in.nextLine();
decryption(ciphertext);
}
public static boolean isRepeat(char c){
for(int i=0;i<5;i++){
for (int j=0;j<5;j++){
if(a[i][j]==c||(a[i][j]=='i'&&c=='j')||(a[i][j]=='j'&&c=='i'))
return true;
}
}
return false;
}
public  static void setMatrix(String str){
int k=0; int p=0,q=0;

//setting the keyword in matrix
loop:{
for(int i=0;i<5;i++){
for( int j=0;j<5;){
char ch=str.charAt(k++);
if(!isRepeat(ch)){
a[i][j++]=ch;
}
if(k==str.length()){
p=i;
q=j;
break loop;
}
}
}
}//looping break
//setting remaining letter in matrix
k=0; 
loop:
{
for(int i=p;i<5;i++){
for(int j=q ;j<5;){
char ch=alpha.charAt(k++);
if(!isRepeat(ch)){
a[i][j++]=ch;
}
if(k==alpha.length())
break loop;
}
q=0;
}
}//loop breaking
}

public static void encryption(String str){
StringBuilder sb=new StringBuilder(str);
for(int i=0;i<sb.length();i+=2){
if(i==sb.length()-1)
sb.append(sb.length()%2==1?'x':"");
else if (sb.charAt(i)==sb.charAt(i+1))
sb.insert(i+1,"x");
}
encode(sb);
}
//function that encode the string
public static void encode(StringBuilder sb){

for (int i=0;i<sb.length();i+=2){
char key1=sb.charAt(i);
char key2=sb.charAt(i+1);
Point key1pos=position(key1);
Point key2pos=position(key2);
//testing if row1 is equal to row2
int row1=key1pos.x;
int row2=key2pos.x;

int col1=key1pos.y;
int col2=key2pos.y;
if(row1==row2){
col1=(key1pos.y+1)%5;
    col2=(key2pos.y+1)%5;
}
else if( col1==col2){
 

row1=(key1pos.x+1)%5;
row2=(key2pos.x+1)%5;
}
else{
int temp=col1;
col1=col2;
col2=temp;
}
sb.setCharAt(i,a[row1][col1]);
sb.setCharAt(i+1,a[row2][col2]);
 
 
 
}
System.out.println(sb);
}
public static void decryption(String str){
StringBuilder sb=new StringBuilder(str);
for (int i=0;i<sb.length();i+=2){
char key1=sb.charAt(i);
char key2=sb.charAt(i+1);
Point key1pos=position(key1);
Point key2pos=position(key2);
//testing if row1 is equal to row2
int row1=key1pos.x;
int row2=key2pos.x;

int col1=key1pos.y;
int col2=key2pos.y;
if(row1==row2){
col1=(key1pos.y-1);
    col2=(key2pos.y-1);
    if(col1==-1) col1=4;
   
    if(col2==-1) col2=4;
    
 
}
else if( col1==col2){
 

row1=(key1pos.x-1);
row2=(key2pos.x-1);
 if(row1==-1) row1=4;
 if(row2==-1) row2=4;
   
    
}
else{
int temp=col1;
col1=col2;
col2=temp;
}
sb.setCharAt(i,a[row1][col1]);
sb.setCharAt(i+1,a[row2][col2]);
 
 
 
}
System.out.println(sb);
}



// function to find the position of character in matrix
public static Point position(char c){
Point p=new Point();

for(int i=0;i<5;i++){
for (int j=0;j<5;j++){
if(a[i][j]==c||(a[i][j]=='i'&&c=='j')||(a[i][j]=='j'&&c=='i'))
{
p.x=i;
p.y=j;
return p;
}
}
}
return null;
}

}

Wednesday, April 5, 2017

Ceaser Cipher Java Implementation

Ceaser Cipher Java Implementation




The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.
In this way, a message that initially was quite readable, ends up in a form that can not be understood at a simple glance.
For example, here's the Caesar Cipher encryption of a message, using a right shift of 3.
Plaintext:  
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: 
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Encryption:  c=(m+k)mod 26
Decryption: m=(c-k+26)mod 26
source code:
package ceaser;


import java.util.Scanner;

public class Ceaser {

 
 
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  System.out.println("Enter the plaintext:");
  Scanner in =new Scanner(System.in);
  String str=in.nextLine();
  Ceaser.encryption(str);
  System.out.println("Enter the ciphertext:");
  str=in.nextLine();
  Ceaser.decryption(str);
  
  

 }
 

 
 public static void encryption(String str){
 String en="";  
  for(int i=0;i<str.length();i++){
   
     char ch = (char) (((int)str.charAt(i) +3 - 97) % 26 + 97);
   
     en +=ch;
   
   
  }
  System.out.println("The ciphertext is:"+en);
  
 }
 public static void decryption(String str){
  
 
  String en="";  
  for(int i=0;i<str.length();i++){
   
     char ch = (char) (((int)str.charAt(i) -3 +26 -97) % 26 + 97);
   
     en +=ch;
   
   
  }
  System.out.println("The ciphertext is:"+en);
 
 
 }
 

}


Output:
Enter the plaintext:
patancampus
The ciphertext is:sdwdqfdpsxv
Enter the ciphertext:
ghimireshankar
The ciphertext is:defjfobpexkhxo

Run your first java program

ØSet up/Install Java
1. Make sure the JDK is installed and that the jdk/bin directory is on the executable path.



2. Updating the PATH Environment Variable



3.Verify Java Installed Correctly

Windows + R = Open Run







Simple Program of Java
In this page, we will learn how to write the simple program of java. We can write a simple hello java program easily after installing the JDK.
To create a simple java program, you need to create a class that contains main method.
Let's understand the requirement first

Requirement for Hello Java Example

For executing any java program, you need to
        Install the JDK if you don't have installed it, download the JDK and install it.
        Set path of the jdk/bin directory.
        create the java program
        compile and run the java program


Creating hello java example


To compile:      javac HelloWorld.java

To execute:     java HelloWorld


Output: HELLO WORLD

Socket Programming in Java

Source Code: client Side: package learning; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event....