postheadericon Mono-alphabetic Substitution Cipher example using Java

Write a programs to simulate encryption and decryption technique using Mono-alphabetic Substitution Cipher, algorithm development and Communication between client and server will be done using Java server socket programming.

MonoAlphabeticSubstitutionCipher.java

package bsr;
import java.util.*;
import java.net.*;
import java.io.*;
/*A code from www.bipinrupadiya.com*/
public class MonoAlphabeticSubstitutionCipher
{
public char p[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public char ch[] = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
public String doEncryption(String s)
{



char c[]=new char[(s.length())];
for(int i=0;i<s.length();i++)
{
for(int j=0;j<26;j++)
{
if(p[j]==s.charAt(i))
{
c[i]=ch[j];
break;
}
}
System.out.println(s.charAt(i) +" "+c[i]);
}
System.out.println("Cipher Text : "+c);
return(new String(c));
}
public String doDecryption(String s)
{
System.out.println(s +" Len Of s :"+s.length());
char p1[]=new char[(s.length())];
for(int i=0;i<s.length();i++)
{
for(int j=0;j<26;j++)
{
if(ch[j]==s.charAt(i))
{
p1[i]=p[j];
break;
}
}
}
System.out.println("Plain Text : "+new String(p1));
return(new String(p1));
}
}

MySocket.java

package bsr;
/*A code from www.bipinrupadiya.com*/
import java.util.*;
import java.net.*;
import java.io.*;
public class MySocket
{
String serverName;
int port;
String frame;
public MySocket()
{
this.serverName="localhost";
this.port=6061;
this.frame="BipinRupadiya";
}
public MySocket(int port)
{
this.port=port;
}
public MySocket(String serverName,int port,String frame)
{
this.serverName=serverName;
this.port=port;
this.frame=frame;
}

public void sendFrame(String frame) throws Exception
{
ServerSocket ss=new ServerSocket(port);
Socket s=ss.accept();

byte bFrame[]=frame.getBytes();
OutputStream os=s.getOutputStream();
os.write(bFrame);
ss.close();
s.close();

    }
public String receiveFrame() throws Exception
{
Socket s=new Socket(serverName,port);
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
s.close();
return(str);
    }

}

Sender.java

import bsr.*;
/*A code from www.bipinrupadiya.com*/
public class sender
{
public static void main(String args[])throws Exception
{

String myString="BipinRupadiya";
/*
// read data from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter String(only alphabets allowed) :");
myString=sc.next();

*/

MonoAlphabeticSubstitutionCipher obj = new MonoAlphabeticSubstitutionCipher();

String encryptedString=obj.doEncryption(myString);
System.out.println("\nEncryted String : "+encryptedString);
MySocket m = new MySocket();
m.sendFrame(encryptedString);
}

}

Receiver.java


import bsr.*;
public class receiver
{
public static void main(String args[])throws Exception
{

MonoAlphabeticSubstitutionCipher obj = new MonoAlphabeticSubstitutionCipher();

MySocket m = new MySocket();
String encryptedString=m.receiveFrame();
String decryptedString=obj.doDecryption(encryptedString);
System.out.println("\nDecryted String : "+decryptedString);
}
}



To execute this code...


  • compile bsr package
  • compile sender.java
  • compile receiver.java
  • open two command prompt
  • in 1st command prompt execute sender.java
  • in 2nd command prompt execute receiver.java

1 comments:

Unknown said...

please provide bsr package code

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.