Вы находитесь на странице: 1из 10

T.Y.B.Sc.I.

Network Security
Practical No.1(A)

Aim: Program to implement Substitution cipher


A. Caesar Cipher

Source Code:
import java.io.*;
public class Caesar
{
int offset=3;
public String encrypt(String s) throws IOException
{
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
char t=s.charAt(i);
if((t>='A')&&(t<='Z'))
{
int t1=t-'A'+offset;
t1=t1%26;
sb.append((char)(t1+'A'));
}
else if((t>='a')&&(t<='z'))
{
int t1=t-'a'+offset;
t1=t1%26;
sb.append((char)(t1+'a'));
}
}
return sb.toString();
}
public String decrypt(String s) throws IOException
{
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
char t=s.charAt(i);
if((t>='A')&&(t<='Z'))
{

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

int t1=t-'A'-offset;
if(t1<0) t1=26+t1;
sb.append((char)(t1+'A'));
}
else if((t>='a')&&(t<='z'))
{
int t1=t-'a'-offset;
if(t1<0) t1=26+t1;
sb.append((char)(t1+'a'));
}}
return sb.toString();
}
public static void main(String[] args) throws IOException
{
BufferedReader b;
System.out.println("\n\t\t Caeser Encryption Technique");
System.out.print("Enter The String: ");
b=new BufferedReader(new InputStreamReader(System.in));
String oriStr=b.readLine();
Caesar c=new Caesar();
String encStr=c.encrypt(oriStr);
System.out.println("\n\t\t The Encrypted string is:"+encStr);
String descStr=c.decrypt(encStr);
System.out.println("\n\t\t The decrypt string is:"+descStr);
System.out.println("\n");
}
}

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

Output:

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security
Practical No.1(B)

Aim: Program to implement Substitution cipher


B. Modified Caesar Cipher

Source Code:
import java.io.*;
public class ModifiedCaesar
{
int offset=7;
public String encrypt(String s) throws IOException
{
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
char t=s.charAt(i);
if((t>='A')&&(t<='Z'))
{
int t1=t-'A'+offset;
t1=t1%26;
sb.append((char)(t1+'A'));
}
else if((t>='a')&&(t<='z'))
{
int t1=t-'a'+offset;
t1=t1%26;
sb.append((char)(t1+'a'));
}
}
return sb.toString();
}
public String decrypt(String s) throws IOException
{
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
char t=s.charAt(i);
if((t>='A')&&(t<='Z'))
{

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

int t1=t-'A'-offset;
if(t1<0) t1=26+t1;
sb.append((char)(t1+'A'));
}
else if((t>='a')&&(t<='z'))
{
int t1=t-'a'-offset;
if(t1<0) t1=26+t1;
sb.append((char)(t1+'a'));
}
}
return sb.toString();
}
public static void main(String[] args) throws IOException
{
BufferedReader b;
System.out.println("\n\t\tModifiedCaeser Encryption Technique");
System.out.print("Enter The String: ");
b=new BufferedReader(new InputStreamReader(System.in));
String oriStr=b.readLine();
ModifiedCaesar c=new ModifiedCaesar();
String encStr=c.encrypt(oriStr);
System.out.println("\n\t\t The Encrypted string is:"+encStr);
String descStr=c.decrypt(encStr);
System.out.println("\n\t\t The decrypt string is:"+descStr);
System.out.println("\n");
}
}

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

Output:

Practical No.1(C)
Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

Aim: Program to implement Substitution cipher


C. Vigenere Cipher ( Polyalphabetic Cipher)

Source Code:
import java.io.*;
public class VigenereCipher
{
public static void main(String args[])
{
String key="CIPHER";
String ori="thiscryptosystemisnotsecure";
String enc=encrypt(ori,key);
System.out.println("\n"+enc);
System.out.println("\n"+decrypt(enc,key));
}
static String encrypt(String text,final String key)
{
String res="";
text=text.toUpperCase();
for(int i=0,j=0;i<text.length();i++)
{
char c=text.charAt(i);
if(c<'A'||c>'Z')continue;
res +=(char)((c+key.charAt(j)-2*'A')%26+'A');
j=++j%key.length();
}
return res;
}
static String decrypt(String text,final String key)
{
String res="";
text=text.toUpperCase();
for(int i=0,j=0;i<text.length();i++)
{
char c=text.charAt(i);
if(c<'A'||c>'Z')continue;
res +=(char)((c-key.charAt(j)+26)%26+'A');
j=++j%key.length();
}
return res;
}
}

Output:
Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

Practical No.1(D)
Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

Aim: Program to implement Substitution cipher


D. Mono-Alphabetic Cipher

Source Code:
import java.io.*;
import java.util.Random;
public class Monoalphabetic
{
public int offset()
{
int set;
Random ran=new Random();
set=ran.nextInt(10);
System.out.print(" "+set);
return set;
}
public String encrypt(String s)throws IOException
{
StringBuffer sb =new StringBuffer();
for(int i=0;i<s.length();i++)
{
char t=s.charAt(i);
if((t>='A')&&(t<='Z'))
{
int t1=t-'A'+offset();
System.out.println(""+offset());
t1=t1%26;
sb.append((char)(t1+'A'));
}
else if((t>='a')&&(t<='z'))
{
int t1=t-'a'+offset();
t1=t1%26;
sb.append((char)(t1+'a'));
}
}
return sb.toString();
}
public static void main(String args[])throws IOException
{
BufferedReader b;
System.out.println("\n\t\t Monoalphabetic Encryption Encryption Technique");
System.out.print("\n Enter the string:");
b=new BufferedReader(new InputStreamReader(System.in));

Created By: Ankit Chaubey

Roll No: 24

T.Y.B.Sc.I.T

Network Security

String oriStr=b.readLine();
Monoalphabetic c=new Monoalphabetic();
String encStr=c.encrypt(oriStr);
System.out.println("\n The encrypted string is:"+encStr);
System.out.println("\n");
}
}

Output:

Created By: Ankit Chaubey

Roll No: 24

Вам также может понравиться