1.program code for character stuffing and destuffing in java
import java.io.*;
import java.util.*;
import java.lang.*;
class Charstuff
{
public static void main(String args[])
{
Scanner k =new Scanner (System.in);
System.out.println("enter the string\t");
String s=k.nextLine();
String str1;
String str2="";
int i,m,j;
m=s.length();
System.out.println("original data "+s);
str1="dlestx";
for(i=0;i<=m-1;i++)
{
if((s.charAt(i)=='d')&&(s.charAt(i+1)=='l')&&(s.charAt(i+2)=='e'))
{
str1=str1+"dle";
}
str1=str1+s.substring(i,i+1);
}
str1=str1+"dleetx";
int p=str1.length();
System.out.println("transmitted data "+str1);
for(i=6;i<p-6;i++)
{
if((str1.charAt(i)=='d')&&(str1.charAt(i+1)=='l')&&(str1.charAt(i+2)=='e')&&(str1.charAt(i+3)=='d')&&(str1.charAt(i+4)=='l')&&(str1.charAt(i+5)=='e'))
{
i=i+3;
}
str2=str2+str1.substring(i,i+1);
}
System.out.println("received data is "+str2);
}
}
import java.util.*;
import java.lang.*;
class Charstuff
{
public static void main(String args[])
{
Scanner k =new Scanner (System.in);
System.out.println("enter the string\t");
String s=k.nextLine();
String str1;
String str2="";
int i,m,j;
m=s.length();
System.out.println("original data "+s);
str1="dlestx";
for(i=0;i<=m-1;i++)
{
if((s.charAt(i)=='d')&&(s.charAt(i+1)=='l')&&(s.charAt(i+2)=='e'))
{
str1=str1+"dle";
}
str1=str1+s.substring(i,i+1);
}
str1=str1+"dleetx";
int p=str1.length();
System.out.println("transmitted data "+str1);
for(i=6;i<p-6;i++)
{
if((str1.charAt(i)=='d')&&(str1.charAt(i+1)=='l')&&(str1.charAt(i+2)=='e')&&(str1.charAt(i+3)=='d')&&(str1.charAt(i+4)=='l')&&(str1.charAt(i+5)=='e'))
{
i=i+3;
}
str2=str2+str1.substring(i,i+1);
}
System.out.println("received data is "+str2);
}
}
BITSTUFFING
import java.io.*;
import java.*;
class Bitstuff
{
public
static void main(String args[])throws Exception
{
int
a[]=new int[70];
int
b[]=new int[70];
int
ca[]=new int[70];
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
how many bits u want to enter");
int
n,i,j=0,k,c=0;
n=Integer.parseInt(br.readLine());
System.out.println("Enter
"+n+" bits 0,1");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
if(a[i]!=0&&a[i]!=1)
{
System.out.println("Invalid
input,try again");
i=-1;
continue;
}
}
for(j=0;j<=7;j++)
{
if(j==0||j==7)
b[j]=0;
else
b[j]=1;
}
for(i=0;i<n;i++)
{
if(a[i]==1)
c++;
if(a[i]==0&&c<=4)
c=0;
if(c==5)
{
b[j++]=a[i];
b[j++]=0;
c=0;
}
else
b[j++]=a[i];
}
for(int
m=0;m<=7;m++)
{
if(m==0||m==7)
b[j++]=0;
else
b[j++]=1;
}
System.out.println("Stuffed
data is: ");
for(i=0;i<j;i++)
System.out.print(b[i]);
c=k=0;
j=j-8;
for(i=8;i<j;i++)
{
if(b[i]==1)
c++;
if(b[i]==0)
if(c==5)
{
i++;
ca[k++]=b[i];
c=1;
continue;
}
else
c=0;
ca[k++]=b[i];
}
System.out.println("\nAfter
Destuffing");
for(i=0;i<k;i++)
System.out.print(ca[i]);
}
}
Output:
C:\>javac Bitstuff.java
C:\>java Bitstuff
Enter how many bits you want to enter
10
Enter 10 bits 0,1
0
1
1
1
1
1
1
1
1
0
Stuffed data is
011111100111110111001111110
After Destuffing
0111111110
Character stuffing
import java.io.*;
public class CharStuff
{
public
static void main(String args[])throws Exception
{
String
str=" ";
StringBuffer
stuff;
StringBuffer
destuff;
System.out.println("Enter
a string:- ");
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
int
i;
stuff=new
StringBuffer(str);
int
len=stuff.length();
for(i=0;i<len;i++)
{
if(str.charAt(i)=='d'
&& str.charAt(i+1)=='l'&&str.charAt(i+2)=='e')
stuff.insert(i+3,"dle");
}
stuff.insert(0,"dlestx");
stuff.append("dleetx");
System.out.println("stuffed
data: "+stuff);
destuff=new
StringBuffer();
destuff.append(stuff.delete(0,7));
destuff.reverse();
destuff.delete(0,7);
destuff.reverse();
for(i=0;i<destuff.length();i++)
{
if(destuff.charAt(i)=='d'&&destuff.charAt(i+1)=='l'&&destuff.charAt(i+2)=='e')
{
if(destuff.charAt(i+3)=='d'&&destuff.charAt(i+4)=='l'&&destuff.charAt(i+5)=='e')
destuff.delete(i+3,i+6);
}
}
System.out.println("Destuffed data:
"+destuff);
}
}
Output:
C:\>javac CharStuff.java
C:\>java CharStuff
Enter a string:-
abcdlefgh
stuffed data: dle stxabcdledlefghdle etx
Destuffed data: abcdlefgh
DIJKSTRA’S ALGORITHM
import java.lang.*;
import java.io.*;
import java.util.*;
class Main
{
public
static void main(String args[])
{
BufferedReader b=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter how many vertices:");
int n;
n=Integer.parseInt(b.readLine());
int a[][]=new int[n][n];
int set[]=new int[n];
int path[]=new int[n];
int len[]=new int[n];
int j,k,s,f,i,d;
System.out.println("Enter
"+n*n+"weights for the graph");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=Integer.parseInt(b.readLine());
System.out.println("ENter the
source vertex");
s=Integer.parseInt(b.readLine());
s=s-1;
for(i=0;i<n;i++)
set[i]=0;
set[s]=1;
for(i=0;i<n;i++)
{
if(a[s][i]==0)
{
len[i]=1234;
path[i]=-1;
}
else
{
len[i]=a[s][i];
path[i]=s;
}
}
len[s]=0;
f=1;
while(f==1)
{
j=srch(len,set,n);
set[j]=1;
for(i=0;i<n;i++)
{
if(set[i]==1)
continue;
else
if(a[i][j]!=0)
if(len[j]+a[i][j]<len[i])
{
len[i]=len[j]+a[i][j];
path[i]=j;
}
}
f=0;
for(k=0;k<n;k++)
if(set[k]==0)
{
f=1;
break;
}
}
System.out.println("ENter the
destination vertex");
d=Integer.parseInt(b.readLine());
System.out.println("shortest
path from the vertex "+s+" to the vertex "+d+" is ");
System.out.print("v"+d+"<-");
d=d-1;
for(i=d;i!=s;)
{
System.out.print("v"+(path[i]+1)+"<-");
i=path[i];
}
}
static int srch(int l[],int
s[],int n)
{
int
temp[]=new int[n];
int
i,j=0,min;
for(i=0;i<n;i++)
if(s[i]==0)
temp[j++]=l[i];
min=temp[0];
for(i=1;i<j;i++)
if(min>temp[i])
min=temp[i];
for(i=0;i<n;i++)
if(s[i]==0&&min==l[i])
break;
return i;
}
}
Output:
C:\ java main
Enter how many vertices : 5
Enter 25 weights for the graph
0
1 0
2
1
3
3
0
0
0
0
2
2
0
0
0
0
1
1
1
2
3
0
0
1
0
0
2
1
0
0
0
1
Enter the source vertex: 1
Enter the destination vertex: 5
Shortest path from the vertex 0 to the
vertex 5 is
V5<-v2<-v1<-
//PROGRAM TO ILLUSTRATE LINK STATE
ROUTING ALGORITHM
import java.io.*;
class Weightedgraph
{
int
size;
int
adjmat[][];
Weightedgraph(int
a[][],int n)
{
size=n;
adjmat=new
int [size][size];
for(int
i=0;i<size;i++)
for(int
j=0;j<size;j++)
adjmat[i][j]=a[i][j];
}
public
void shortestpath()
{
int
i;
int
min;
int
v,w;
boolean
found[]=new boolean[size];
int
d[]=new int[size];
found[0]=true;
d[0]=0;
for(v=1;v<size;v++)
{
found[v]=false;
d[v]=adjmat[0][v];
}
for(i=1;i<size;i++)
{
min=99999;
for(w=1;w<size;w++)
{
if(!found[w]&&d[w]<min)
{
v=w;
min=d[w];
}
}
found[v]=true;
for(w=1;w<size;w++)
if(!found[w])
if(min+adjmat[v][w]<d[w])
d[w]=min+adjmat[v][w];
}
System.out.println(“Using
linkstate routing”);
System.out.println("Shortest
path from vertex 0 to other vertices ");
for(i=1;i<size;i++)
System.out.println("0->"+i+":"+d[i]);
}
}
class Link
{
public
static void main(String args[])throws Exception
{
final int INF=99999;
int
a[][]={
{INF,2,3,6,INF},
{2,INF,4,1,1},
{3,4,INF,INF,2},
{INF,1,INF,INF,1},
{INF,1,2,1,INF},
};
Weightedgraph
wg=new Weightedgraph(a,5);
wg.shortestpath();
}
}
/*
OUTPUT:-
D:\>javac Link.java
D:\>java Link
Shortest path from vertex 0 to other
vertices
0->1:2
0->2:3
0->3:3
0->4:3
*/
/* Distance
Vector Routing */
import java.io.*;
class DVR
{
public
static void main(String args[])throws Exception
{
int
N=6;
int
i,j,k,R;
int
array[][][]=new int[6][6][3];
int
temp[][]=new int[6][3];
int
INF=99999;
DataInputStream
dis=new DataInputStream(System.in);
int
graph[][]={
{0,1,4,INF,INF,3},
{1,0,INF,2,4,INF},
{4,INF,0,INF,2,INF},
{INF,2,INF,0,INF,3},
{INF,4,2,INF,0,2},
{3,INF,INF,3,2,0}
};
for(R=0;R<N;R++)
for(i=0;i<N;i++)
{
array[R][i][0]=i+1;
array[R][i][1]=graph[R][i];
}
for(R=0;R<N;R++)
{
for(int
tempval=0;tempval<N;tempval++)
{
temp[tempval][0]=array[R][tempval][0];
if(array[R][tempval][1]==INF)
temp[tempval][1]=0;
else
temp[tempval][1]=array[R][tempval][1];
temp[tempval][2]=0;
}
for(i=0;i<N;i++)
{
if(array[R][i][1]!=INF
&& array[R][i][1]!=0)
{
int
nextvalue=array[R][i][1];
for(k=0;k<N;k++)
{
if(array[R][k][1]==INF)
{
if(array[i][k][1]!=INF
&& array[i][k][1]!=0)
{
if(temp[k][1]==0)
{
temp[k][1]=array[i][k][1]+nextvalue;
temp[k][2]=i+1;
}
else
if(temp[k][1]>0)
{
int
maxvalue=temp[k][1];
if(maxvalue>array[i][k][1]+nextvalue)
{
temp[k][1]=array[i][k][1]+nextvalue;
temp[k][2]=i+1;
}
}
}
}
}
}
else
continue;
}
System.out.println("\n----------------------");
System.out.println("Final routing
table for node:"+(R+1));
System.out.println("\n
-----------------------");
System.out.println("To cost next
:");
System.out.println("\n-------------------------");
for(int tempval=0;tempval<N;tempval++)
{
System.out.println("
"+temp[tempval][0]+" "+temp[tempval][1]+"
"+temp[tempval][2]);
array[R][tempval][0]=temp[tempval][0];
array[R][tempval][1]=temp[tempval][1];
array[R][tempval][2]=temp[tempval][2];
}
}
}
}
/*
OUTPUT :
D:\sre>javac DVR.java
D:\sre>java DVR
----------------------
Final routing table for node:1
-----------------------
To cost next :
-------------------------
1 0
0
2 1
0
3 4
0
4 3
2
5 5
2
6 3
0
----------------------
Final routing table for node:2
-----------------------
To
cost next :
-------------------------
1
1 0
2
0 0
3
5 1
4
2 0
5
4 0
6
4 1
----------------------
Final routing table for node:3
-----------------------
To cost next :
-------------------------
1 4
0
2 5 1
3 0 0
4 7 1
5 2 0
6 4 5
----------------------
Final routing table for node:4
-----------------------
To cost next :
-------------------------
1
3 2
2
2 0
3
7 2
4
0 0
5
5 6
6
3 0
----------------------
Final routing table for node:5
-----------------------
To cost next :
-------------------------
1 5
2
2 4
0
3 2
0
4 5
6
5 0
0
6 2
0
----------------------
Final routing table for node:6
-----------------------
To
cost next :
-------------------------
1
3 0
2 4 1
3
4 5
4 3 0
5 2 0
6 0 0
*/
/* A) Substitution */
import java.io.*;
class Sub
{
public static
void main(String args[])throws IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n
Enter a line to encode: ");
String
str=br.readLine();
StringBuffer
str1=new StringBuffer();
System.out.println("\n
Before encoding:"+str);
for(int
i=0;i<str.length();i++)
{
char
ch=str.charAt(i);
if(Character.isWhitespace(ch))
str1.append("
");
else
{
int
j=(Character.valueOf(ch)+3);
if(j<(int)(Character.valueOf('z')))
str1.append((char)(j));
else
str1.append((char)(j-26));
}
}
System.out.println("\n
After coding: " +str1);
}
}
/* OUTPUT
D:\sre>notepad Sub.java
D:\sre>javac Sub.java
D:\sre>java Sub
Enter a line to encode:
Jbict
Before encoding:Jbict
After coding: Melfwf */
/* B) Transposition */
import java.io.*;
public class Tra
{
public static
void main(String args[])throws IOException
{
BufferedReader
b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n
Enter a line to encode: ");
String
str=b.readLine();
StringBuffer
str1=new StringBuffer();
System.out.println("\n
Enter number of keys :");
int
n=Integer.parseInt(b.readLine());
for(int
i=0;i<str.length();i+=2)
str1.append(str.charAt(i));
for(int
i=1;i<str.length();i+=2)
str1.append(str.charAt(i));
StringBuffer
[]str2=new StringBuffer[n+2];
for(int
i=1;i<=n;i++)
str2[i]=new
StringBuffer();
for(int
i=1;i<=n;i++)
{
System.out.println("\n
Enter key value : " +i+ ":");
int
key=Integer.parseInt(b.readLine());
for(int
j=i-1;j<str.length();j+=n)
str2[key].append(str1.charAt(j));
}
for(int
i=1;i<=n;i++)
System.out.println(str2[i]);
}
}
ENRYPTION AND DECRYPTION USING ONE TIMEPAD
import java.io.*;
import java.math.*;
import java.util.*;
class Onetimepad
{
public
static void main(String args[])throws Exception
{
BufferedReader
dis=new BufferedReader(new InputStreamReader(System.in));
Random
randomgenerator=new Random();
String
temp=" ";
System.out.print("Enter
PLAIN TEXT: \n");
temp=dis.readLine();
temp=temp.toUpperCase();
int
length=temp.length();
char
plaintext[]=new char[length];
char
key[]=new char[length];
char
ciphertext[]=new char[length];
char
deciphertext[]=new char[length];
for(int
i=0;i<length;i++)
{
plaintext[i]=temp.charAt(i);
key[i]=(char)(65+randomgenerator.nextInt(26));
ciphertext[i]=(char)(((int)plaintext[i])^((int)key[i]));
deciphertext[i]=(char)(((int)ciphertext[i])^((int)key[i]));
}
System.out.println("PLAIN
TEXT : ");
for(int
i=0;i<length;i++)
System.out.print("
"+plaintext[i]);
System.out.println("\n\nKEY:
\n");
for(int
i=0;i<length;i++)
System.out.print("
"+key[i]);
System.out.println("\n\nCIPHER
TEXT : \n");
for(int
i=0;i<length;i++)
System.out.print("
"+ciphertext[i]);
System.out.println("\n\nDECIPHER
TEXT : \n");
for(int
i=0;i<length;i++)
System.out.print("
"+deciphertext[i]);
}
}
Output:
C:\>javac Onetimepad.java
C:\>java Onetimepad
Enter PLAIN TEXT: character stuffing
PLAIN TEXT:
C H A R A C T E R S T U F F I N G
KEY:
J R Q L C T P P I G M S D G Y Z M J
CIPHER TEXT:
→ ► ▲ ☻ ↨ ♦ § ← g ▲ ◄ ☺ ▼ ‼ ♥
DECIPHER TEXT:
C H A R A C T E R S T U F F I N G
//RSA
import java.io.*;
import static java.lang.Math.*;
class rsa
{
public static void main(String[]args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter two prime numbers");
int p=Integer.parseInt(br.readLine());
int
q=Integer.parseInt(br.readLine());
int
n=(int)(p*q);
int
d;
System.out.println("enter
e value");
int
e=Integer.parseInt(br.readLine());
for(int
i=2;;i++)
{
if((e*i) % ((p-1)*(q-1))==1)
{
d=i;
break;
}
}
int
ptext=4;
int
c=(int)((long)(pow(ptext,e))%(n));
System.out.println("Cipher
text is" +c);
int
pt=(int)((long)(pow(c,d))%(n));
System.out.println("plain
text:"+pt);
}
}
/*OUTPUT:
D:\sre>java rsa
enter two prime numbers
5
7
enter e value
11
Cipher text is9
plain text:4
*/
//ROUTING USING FLOODING
import java.io.*;
class weightedgraph
{
int
size;
int
adjmat[][];
int
b[];
weightedgraph(int a[][],int n)
{
size=n;
b=new int[30];
adjmat=new
int[size][size];
for(int
i=0;i<size;i++)
for(int
j=0;j<size;j++)
adjmat[i][j]=a[i][j];
b[0]=123;
}
public void shortestpath()
{
int i,s;
int min;
int v,w;
boolean
found[]=new boolean[size];
int d[]=new
int[size];
found[0]=true;
d[0]=0;
for(v=1;v<size;v++)
{
found[v]=false;
d[v]=adjmat[0][v];
}
for(i=1;i<size;i++)
{
min=99999;
for(w=1;w<size;w++)
{
if(!found[w]&&d[w]<min)
{
v=w;
min=d[w];
}
}
found[v]=true;
b[v]=123;
for(w=1;w<size;w++)
if(!found[w])
if(min+adjmat[v][w]<d[w])
d[w]=min+adjmat[v][w];
}
System.out.println("Shortest
path from vertex 0 to other vertices: ");
for(i=1;i<size;i++)
System.out.println("0->"+i+":"+d[i]);
for(i=1;i<size;i++)
System.out.println("message at
node"+i+"is:"+b[i]);
}
}
class flooding
{
public static void main(String
args[])throws Exception
{
final int
INF=99999;
int a[][]={
{INF,2,3,6,INF},
{2,INF,4,1,1},
{3,4,INF,INF,2},
{INF,1,INF,INF,1},
{INF,1,2,1,INF},
};
weightedgraph
wg=new weightedgraph(a,5);
wg.shortestpath();
}
}
/*OUTPUT:
D:\sre>javac flooding.java
D:\sre>java flooding
Shortest path from vertex 0 to other
vertices:
0->1:2
0->2:3
0->3:3
0->4:3
message at node1is:123
message at node2is:123
message at node3is:123
message at node4is:123
*/
/* OUTPUT
D:\sre>javac Tra.java
D:\sre>java Tra
Enter a line to encode:
WELCOMETOJBICT
Enter number of keys :
3
Enter key value : 1:
3
Enter key value : 2:
2
Enter key value : 3:
1
OBCJ
LOETT
WECMI
*/