lab programs for information security lab

22

Upload: chanchalakshi

Post on 15-Feb-2016

37 views

Category:

Documents


9 download

DESCRIPTION

This has all the programs which is required to run all the programs of for INS Lab

TRANSCRIPT

Page 1: Lab Programs for Information security lab
Page 2: Lab Programs for Information security lab

1. Consider a file with composite data, substitute the content and transpose the ciphers.

#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");

Page 3: Lab Programs for Information security lab

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;

elsej=count/2+1;

i=0;while(i<count){

dmsg[k++]=msg[i++];if(msg[i]=='\0')

dmsg[j++]='\0';else

dmsg[j++]=msg[i++];}

Page 4: Lab Programs for Information security lab

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';

Page 5: Lab Programs for Information security lab

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.Transpose4. transposedecr 5.Exit1

Enter the input filename to encrypt f1.cEnter the output filename to decrypt f2.cmjqqtEnter your choice 1.substitute encryption 2.substitute Decryption 3.Transpose 4.transposedecr5.Exit3enter the filename to encrypt f1.center the filename to transpose f3.cThe cipher text is hloel

Page 6: Lab Programs for Information security lab

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 Bit length of each prime number. */public RSA( int primeSize ){

this.primeSize = primeSize ;

// Generate two distinct large prime numbers p and q.generatePrimeNumbers() ;

Page 7: Lab Programs for Information security lab

// Generate Public and Private Keys.generatePublicPrivateKeys() ;

/** * Generate two distinct large prime numbers p and q. */public void generatePrimeNumbers(){

p = new BigInteger( primeSize, 10, new Random()) ;

do{

q = new BigInteger( primeSize, 10, new Random()) ;}while( q.compareTo( p ) == 0 ) ;

}

/** * Generate Public and Private Keys. */public void generatePublicPrivateKeys(){

// N = p * qN = p.multiply( q ) ;

// r = ( p - 1 ) * ( q - 1 )r = p.subtract( BigInteger.valueOf( 1 ) ) ;r = r.multiply( q.subtract( BigInteger.valueOf( 1 ) ) ) ;

// Choose E, coprime to and less than rdo{

E = new BigInteger( 2 * primeSize, new Random() ) ;}while( ( E.compareTo( r ) != -1 ) || ( E.gcd( r ).compareTo( BigInteger.valueOf( 1 ) ) !=

0 ) ) ;

// Compute D, the inverse of E mod rD = E.modInverse( r ) ;

}

/** * Encrypts the plaintext (Using Public Key). *

Page 8: Lab Programs for Information security lab

* @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 ) ;}

Page 9: Lab Programs for Information security lab

/** * 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 ) ;}

}

Page 10: Lab Programs for Information security lab

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 Filetry {

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 EncryptionRSA rsa = new RSA( 32 ) ;n=rsa.getN(); // Modulus valued=rsa.getD(); // Exponent valueciphertext = 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() ) ;}

Page 11: Lab Programs for Information security lab

// 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(); }

}}

Page 12: Lab Programs for Information security lab

Program 4. Develop a mechanism to setup a security channel using Diffie-Hellman Key Exchangebetween 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

Page 13: Lab Programs for Information security lab

User A: Enter a prime number Xa which is less than q:5

User B: Enter a prime number Xb which is less than q:3

User A sends Ya=5 to User B

User B sends Yb=6 to User A

User A calculates secret key Ka=6

User B calculates secret key Kb=6

The keys exchanged by user A and user B are same

Page 14: Lab Programs for Information security lab

Program 7. Using any simulation tool: demonstrate packet filtering firewalls, create the ACL, create VLAN [Subnetting].

Select any component.

Page 15: Lab Programs for Information security lab

Click on CLI to start configuration.

Page 16: Lab Programs for Information security lab

Assigning IP address and command prompt(Used to ping)

Page 17: Lab Programs for Information security lab

Before applying any access list ping and make sure two end devices are communicating with each other.

Router1>enable // There are 0-15 different levels of enable. Router1#configure terminal // command used to get into configuration modeRouter1(config)#interface serial 2/0 //Interface is a keyword used to get into any interface followed by the type and number of the interface.

Page 18: Lab Programs for Information security lab

Router1(config-if)#ip address 20.0.0.1 255.0.0.0 // used to assign IP address to any device port.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 DCERouter1(config-if)#no shutdown //*** By default all the ports are in shutdown/Inactive state. No shutdown is used to activateRouter1(config-if)#exit // to Router1(config)#interface fastEthernet 2/0Router1(config-if)#ip address 10.0.0.1 255.0.0.0 Router1(config-if)#exitRouter1(config)#ip route 30.0.0.0 0.0.0.0 20.0.0.2 // known as static routingRouter1(config)#access-list 25 permit 30.0.0.20 0.0.0.0 // creating a access list

Explaination same as above

Router2>enableRouter2#configure terminalRouter2(config)#interface serial 2/0Router2(config-if)#ip address 20.0.0.2 255.0.0.0Router2(config-if)#no shutdownRouter2(config-if)#exitRouter2(config)#interface fastEthernet 2/0Router2(config-if)#ip address 30.0.0.1 255.0.0.0Router2(config-if)#exitRouter2(config)#ip route 10.0.0.0 0.0.0.0 20.0.0.1

VLAN

Page 19: Lab Programs for Information security lab

Switch>enable Switch#configure terminalSwitch(config)#interface fastEthernet 4/0Switch(config-if)#switchport access vlan 2 Switch(config-if).#exitSwitch(config)# // Go into each interface and assign vlans.