Professional Documents
Culture Documents
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *src,*dest;
FILE *openfile(FILE *fptr,char *name, char *mode)
{
fptr=fopen(name,mode);
if(fptr==NULL)
{
printf("Unable to open a file \n"); exit(1);
}
return fptr;
}
void substitutencrypt()
{
char str[50],str1[50],ch;
printf("\n Enter the input filename to encrypt\n"); scanf("%s",str);
src=openfile(src,str,"r");
printf("Enter the output filename to decrypt\n");
scanf("%s",str1);
dest=openfile(dest,str1,"w");
ch=fgetc(src);
while(ch!=EOF)
{
fputc(ch+5,dest);
printf("%c",ch+5);
ch=fgetc(src);
}
fclose(dest);
return;
}
void substitutdecrypt()
{
char str[50],str1[50],ch;
printf("\n Enter the filename to decrypt \n");
scanf("%s",str);
src=openfile(src,str,"r");
printf("\n Enter the filename to encrypt\n");
scanf("%s",str1);
dest=openfile(dest,str1,"w");
ch=fgetc(src);
while(ch!=EOF)
{
fputc(ch-5,dest); ch=fgetc(src);
}
fclose(dest);
return;
}
void transpose()
{
int i=0,j=0,k=0,count;
char ch,str[50],str1[50],msg[50],dmsg[50];
printf(" enter the filename to encrypt \n");
scanf("%s",str);
src=openfile(src,str,"r");
printf("enter the filename to transpose\n");
scanf("%s",str1);
dest=openfile(dest,str1,"w");
ch=fgetc(src);
while(ch!=EOF)
{
msg[i++]=ch;
ch=fgetc(src);
}
msg[i]='\0';
count=strlen(msg);
if(count%2==0)
j=count/2;
else
j=count/2+1;
i=0;
while(i<count)
{
dmsg[k++]=msg[i++];
if(msg[i]=='\0')
dmsg[j++]='\0';
else
dmsg[j++]=msg[i++];
}
dmsg[i]='\0';
printf("\nThe cipher text is\n"); printf("%s",dmsg);
fwrite(dmsg,strlen(dmsg),1,dest); fclose(dest);
}
void transposedecrypt()
{
int i=0,j,k=-1,l,count;
char ch,str[50],str1[50],msg[50],dmsg[50];
printf("\nEnter the filename to decrypt\n");
scanf("%s",str);
src=openfile(src,str,"r");
printf("\nEnter the filename for plaintext\n");
scanf("%s",str1);
dest=openfile(dest,str1,"w");
ch=fgetc(src);
while(ch!=EOF)
{
dmsg[i++]=ch;
ch=fgetc(src);
}
dmsg[i]='\0';
i=0;
count=strlen(dmsg);
if(count%2==0)
j=count/2;
else
j=count/2+1;
l=j;
while(i<l && j<count)
{
msg[++k]=dmsg[i];
++i;
msg[++k]=dmsg[j];
++j;
}
if(i<l)
msg[++k]=dmsg[i];
k++;
msg[k]='\0';
printf("Decrypted message\n");
printf("%s",msg);
fwrite(msg,strlen(msg),1,dest); fclose(dest);
}
void main()
{
int choice; char str[50]; do
{
printf("\n Enter your choice \n");
printf("1.substitute encryption \n 2.substitute Decryption \n 3.Transpose\n 4. transposedecr\n
5.Exit\n \n");
scanf("%d",&choice);
switch(choice)
{
case 1: substitutencrypt(); break;
case 2: substitutdecrypt(); break;
case 3: transpose(); break;
case 4: transposedecrypt(); break;
default: exit(0);
}
}while(choice>0);
}
gcc transpose.c
./a.out
Enter your choice 1.substitute encryption 2.substitute Decryption 3.Transpose
4. transposedecr 5.Exit
1
Enter the input filename to encrypt f1.c
Enter the output filename to decrypt f2.c
mjqqt
Enter your choice 1.substitute encryption 2.substitute Decryption 3.Transpose
4.transposedecr
5.Exit
3
enter the filename to encrypt f1.c
enter the filename to transpose f3.c
The cipher text is hloel
Program 3 Apply the RSA algorithm on a text file to produce cipher text file.
import java.math.BigInteger ;
import java.util.Random ;
import java.io.* ;
/* RSA.java*/
public class RSA
{
/**
* Bit length of each prime number.
*/
int primeSize ;
/**
* Two distinct large prime numbers p and q.
*/
BigInteger p, q ;
/**
* Modulus N.
*/
BigInteger N ;
/**
*r=(p-1)*(q-1)
*/
BigInteger r ;
/**
* Public exponent E and Private exponent D
*/
BigInteger E, D ;
public RSA(){
}
/**
* Constructor.
*
* @param
primeSize
*/
public RSA( int primeSize )
{
this.primeSize = primeSize ;
* @param
message
String containing the plaintext message to be
encrypted.
* @return
The ciphertext as a BigInteger array.
*/
public BigInteger[] encrypt( String message )
{
int i ;
byte[] temp = new byte[1] ;
byte[] digits = message.getBytes() ;
BigInteger[] bigdigits = new BigInteger[digits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
{
temp[0] = digits[i] ;
bigdigits[i] = new BigInteger( temp ) ;
}
BigInteger[] encrypted = new BigInteger[bigdigits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
encrypted[i] = bigdigits[i].modPow( E, N ) ;
return( encrypted ) ;
}
/**
* Get prime number p.
*
* @return
Prime number p.
*/
public BigInteger getp()
{
return( p ) ;
}
/**
* Get prime number q.
*
* @return
Prime number q.
*/
public BigInteger getq()
{
return( q ) ;
}
/**
* Get r.
*
* @return
r.
*/
public BigInteger getr()
{
return( r ) ;
}
/**
* Get modulus N.
*
* @return
Modulus N.
*/
public BigInteger getN()
{
return( N ) ;
}
/**
* Get Public exponent E.
*
* @return
Public exponent E.
*/
public BigInteger getE()
{
return( E ) ;
}
/**
* Get Private exponent D.
*
* @return
Private exponent D.
*/
public BigInteger getD()
{
return( D ) ;
}
import java.math.*;
import java.io.*;
import java.io.FileReader;
import java.io.IOException;
public class RSAMain{
public static void main(String args[]){
String nhash;
BigInteger[] ciphertext = null;
BigInteger n = null;
BigInteger d = null;
StringBuffer input = new StringBuffer();
// Read From File
try {
BufferedReader br = new BufferedReader(new FileReader("rsa-input.txt"));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
input.append(sCurrentLine);
}
} catch (IOException e)
{
e.printStackTrace();
}
System.out.println(" Input text is :"+input);
// Perform RSA Encryption
RSA rsa = new RSA( 32 ) ;
n=rsa.getN(); // Modulus value
d=rsa.getD(); // Exponent value
ciphertext = rsa.encrypt(input.toString()) ; // perform RSA Encryption
// Convert binary cipherText to string format
StringBuffer bf = new StringBuffer();
for( int i = 0 ; i < ciphertext.length ; i++ )
{
bf.append( ciphertext[i].toString( 16 ).toUpperCase() ) ;
}
// convert string buffer to string
String message=bf.toString();
System.out.println("Encrypted text written to rsa-output.txt file");
/* Writing into File */
try {
BufferedWriter out = new BufferedWriter(new FileWriter("rsa-output.txt"));
out.flush();
out.write(message); // Write the string into file
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Program 4. Develop a mechanism to setup a security channel using Diffie-Hellman Key Exchange
between client and server
#include<stdio.h>
#include<math.h>
void main()
{
long int q,alpha,Xa,Xb,Ya,Yb,Ka,Kb;
printf("Enter a prime number q:\n");
scanf("%ld",&q);
printf("Enter the value of alpha:\n");
scanf("%ld",&alpha);
printf("User A: Enter a prime number Xa which is less than q:\n");
scanf("%ld",&Xa);
printf("User B: Enter a prime number Xb which is less than q:\n");
scanf("%ld",&Xb);
Ya=((long int)pow(alpha,Xa))%q;
printf("User A sends Ya=%ld to User B\n",Ya);
Yb=((long int)pow(alpha,Xb))%q;
printf("User B sends Yb=%ld to User A\n",Yb);
Ka=((long int)pow(Yb,Xa))%q;
printf("User A calculates secret key Ka=%ld \n",Ka);
Kb=((long int)pow(Ya,Xb))%q;
printf("User B calculates secret key Kb=%ld \n",Kb);
if(Ka==Kb)
printf("The keys exchanged by user A and user B are same");
else
printf("The keys exchanged by user A and user B are not same");
}
Output:
Enter a prime number q: 7
Enter the value of alpha: 3
User A: Enter a prime number Xa which is less than q:5
Program 7. Using any simulation tool: demonstrate packet filtering firewalls, create the ACL,
create VLAN [Subnetting].
Before applying any access list ping and make sure two end devices are
communicating with each other.
Router1(config)#interface serial 2/0 //Interface is a keyword used to get into any interface
followed by the type and number of the interface.
Router1(config-if)#clock rate 64000 // need to assign a clock rate for router to router
communication. It is assigned on the DCE side of the cable. Just point your mouse pointer over
the cable if a clock appears then its DCE
Router1(config-if)#no shutdown //*** By default all the ports are in shutdown/Inactive
state. No shutdown is used to activate
Router1(config-if)#exit // to
Router1(config)#interface fastEthernet 2/0
Router1(config-if)#ip address 10.0.0.1 255.0.0.0
Router1(config-if)#exit
Router1(config)#ip route 30.0.0.0 0.0.0.0 20.0.0.2 // known as static routing
Router1(config)#access-list 25 permit 30.0.0.20 0.0.0.0 // creating a access list
Explaination same as above
Router2>enable
Router2#configure terminal
Router2(config)#interface serial 2/0
Router2(config-if)#ip address 20.0.0.2 255.0.0.0
Router2(config-if)#no shutdown
Router2(config-if)#exit
Router2(config)#interface fastEthernet 2/0
Router2(config-if)#ip address 30.0.0.1 255.0.0.0
Router2(config-if)#exit
Router2(config)#ip route 10.0.0.0 0.0.0.0 20.0.0.1
VLAN
Switch>enable
Switch#configure terminal
Switch(config)#interface fastEthernet 4/0
Switch(config-if)#switchport access vlan 2
Switch(config-if).#exit
Switch(config)#
// Go into each interface and assign vlans.