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

Saturday, March 25, 2017

Graph coloring problem java implementation



Problem:

The problem is, given m colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are colored using same color.

the given graphs:




java program


package graphcolor;


import java.util.ArrayList;

public class Graph {
class Node {
public int vertex;
public  int vcolor;
public ArrayList<Node> pointer =new ArrayList<Node>();
public Node( int v){
this.vertex=v;
}
}



Node[] n=new Node[7];



public Graph(){
for(int i=0;i<n.length;i++){
n[i]=new Node(i+1);
}
this.addEdge(n[0],n[1]);
this.addEdge(n[0],n[2]);
this.addEdge(n[1],n[3]);
this.addEdge(n[2],n[3]);
this.addEdge(n[2],n[4]);
this.addEdge(n[2],n[5]);
this.addEdge(n[3],n[4]);
this.addEdge(n[4],n[5]);
this.addEdge(n[6],n[6]);

}

public void addEdge(Node A, Node B){
A.pointer.add(B);
B.pointer.add(A);

}

public void colorGraph(){
for (int i=0;i<n.length;i++){
//method to finding the unused color
n[i].vcolor=this.findunusedcolor(n[i]);
}
}

public int findunusedcolor(Node n){
int unusedcolor = 0;
int usedcolor[]=new int[10];
for(int j=0;j<n.pointer.size();j++){
int c=n.pointer.get(j).vcolor;
usedcolor[j]=c;

}
int [] color=new int[3];
for(int i=0;i<color.length;i++){
color[i]=i;
}
boolean check;
for(int i=0;i<color.length;i++){
check=true;
for(int j=0;j<usedcolor.length;j++){
if(color[i]==usedcolor[j]){
check=false;
}
}
if(check){
unusedcolor=color[i];
}
}
return unusedcolor;
}

public void showGraph(){
for (int i=0;i<n.length;i++){
System.out.println("node"+ n[i].vertex+"   color"+n[i].vcolor );
}
}

}
***********************************************
package graphcolor;

public class GraphTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
Graph g=new Graph();
g.colorGraph();
System.out.println("The Problem's solution is:");
g.showGraph();

}

}


Output:

****************************
The Problem's solution is:
node1   color2
node2   color1
node3   color1
node4   color2
node5   color0
node6   color2
node7   color2

Friday, March 24, 2017

Simple library management database

The MySQL link is here for complete database click here


The libtary management system ddfhksdahfksa <>



Java implementation of 4 Queen Problem by setting the one default queen initially

Problem: 
             The 4-Queens Problem consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal.



// queen test class

package fourqueen;

public class QueenTest {

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

Queen q=new Queen();
q.setQueen();
System.out.println("The Output is:");
q.showQueen();

}

}




//queen class

package fourqueen;

public class Queen {
final public int n=4;
public int a[][]=new int [n][n];

//constructor for queen
public Queen(){

for (int i=0;i<n;i++){
for  (int j=0;j<n;j++){
a[i][j]=0;
}
}
a[0][2]=1;//setting the initial queen



}

//function to check  the position is right for queen or not 
public boolean canPlaceQueen(int row,int col){
int i,j;

//checking left elements
for(i=0;i<col;i++){
if(a[row][i]==1){

return false;
}


}

//checking right elements
for( i=col;i<n;i++){

if(a[row][i]==1){

return false;
}



}


//checking upper element
for ( i=0;i<row;i++){

if(a[i][col]==1){

return false;
}

}
//checking lower element

for ( i=row;i<n;i++){
if(a[i][col]==1){
return false;
}
}
//checking left upper diagonal element 
for( i=row, j=col;i>=0&&j>=0;i--,j--){
if(a[i][j]==1){
return false;

}
}
//checking right upper diagonal element
for( i=row, j=col;i>=0&&j<n;i--,j++){
if(a[i][j]==1){
return false;

}

}

//checking left lower diagonal element
for( i=row, j=col;i<n&&j>=0;i++,j--){
if(a[i][j]==1){
return false;

}

}

//checking right lower diagonal element
for( i=row, j=col;i<n&&j<n;i++,j++){
if(a[i][j]==1){
return false;
}

}




return true;



}


public void setQueen(){

int i,j;


//checking from second row as first queen is place in first row
for(i=1;i<n;i++){

for(j=0;j<n;j++){


if(canPlaceQueen(i,j)){

a[i][j]=1;
}

}
}

}
//function to print queen
public void showQueen(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(a[i][j]==1){
System.out.print("Q  ");
}
else
System.out.print(a[i][j]+"  ");

}
System.out.println("");
}


}










}

Output:

The Output is:
0  0  Q  0  
Q  0  0  0  
0  0  0  Q  
0  Q  0  0  






Socket Programming in Java

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