C&NS Lab Manual 1-11
C&NS Lab Manual 1-11
REFERENCE BOOKS
Cryptography and Network Security: C K Shyamala, N Harin i, Dr T R Padmanabhan, Wiley
India, 1” Edition.
1. Cryptography and Network Security: C K Shyamala, N Harin i, Dr T
R Padmanabhan, Wiley India, 1” Edition.Data structures and Algorithms in java,
3rd edition, A. Drozdek, Cengage Learning.
2. Cryptography and Network Security : Forouzan Mukhopadhyay, MC Graw Hill,
2″” Edition..
3. Information Security, Principles and Practice: Mark Stamp, Wiley India. Principles
of Computer Security: WM.Arthur Conklin, Greg White, TMH..
Introduction to Network Security: Neal Krawetz, CENGAGE Learning.
Experiment – 1
AIM: Write a C program that contains a string (char pointer) with a value ‘Hello world’. The
program should XOR each character in this string with 0 and displays the result.
PROGRAM:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char str[]="Hello World";
char str1[11];
int i,len;
clrscr();
len=strlen(str);
printf("\ngiven string\n");
for(i=0;i<len;i++)
{
//str1[i]=str[i]^0;
printf("%c",str[i]);
}
printf("\ncipher text is \n");
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n");
getch();
}
OUTPUT:
Experiment – 2
AIM: Write a C program that contains a string (char pointer) with a value ‘Hello world’.
The program should AND or and XOR each character in this string with 127 and display the
result.
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
void main()
{
char str[]="hello";
char str1[11],str3[11];
char str2[11]="hello";
//clrscr();
int i,len;
clrscr();
len = strlen(str);
printf("\nactual data is");
for(i=0;i<len;i++)
{
printf("%c ",str[i]);
}
printf("\naftrer AND with 127: ");
for(i=0;i<len;i++)
{
str1[i] = str[i]&127;
printf("%c ",str1[i]);
}
printf("\nactual data is: ");
for(i=0;i<len;i++)
{
//str1[i] = str[i]&127;
printf("%c ",str2[i]);
}
printf("\n");
OUTPUT:
Experiment – 3
AIM: Write a Java program to perform encryption and decryption using the following
algorithms
a. Ceaser cipher b. Substitution cipher c. Hill Cipher
PROGRAM: CEASER CIPHER
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char plain[10], cipher[10];
int key,i,length;
int result;
clrscr();
printf("\n Enter the plain text:");
scanf("%s", plain);
printf("\n Enter the key value:");
scanf("%d", &key);
printf("\n \n \t PLAIN TEXt: %s",plain);
printf("\n \n \t ENCRYPTED TEXT: ");
for(i = 0, length = strlen(plain); i < length; i++)
{
cipher[i]=plain[i] + key;
if (isupper(plain[i]) && (cipher[i] > 'Z'))
cipher[i] = cipher[i] - 26;
if (islower(plain[i]) && (cipher[i] > 'z'))
cipher[i] = cipher[i] - 26;
printf("%c", cipher[i]); }
printf("\n \n \t AFTER DECRYPTION : ");
for(i=0;i<length;i++)
{
plain[i]=cipher[i]-key;
if(isupper(cipher[i])&&(plain[i]<'A'))
plain[i]=plain[i]+26;
if(islower(cipher[i])&&(plain[i]<'a'))
plain[i]=plain[i]+26;
printf("%c",plain[i]);
}
getch();
}
OUTPUT:
PROGRAM:SUBSTITUTION CIPHER
PROGRAM:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(){
char key[2][2],txt[128],ctxt[128],dtxt[128];
int t,i,j,z;
clrscr();
printf("Enter plain text: ");
scanf("%s",txt);
fflush(stdin);
printf("Enter 4 letter key: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++){
scanf("%c",&key[i][j]);
key[i][j]-='a'; //get in 0 to 25 range
}
//ENCRYPTION
for(i=0;i<=(strlen(txt)/2)+1;){
ctxt[i++] =((key[0][0]*(txt[i]-'a')+key[0][1]*(txt[i+1]-'a'))%26)+'a';
ctxt[i++] =((key[1][0]*(txt[i-1]-'a')+key[1][1]*(txt[i]-'a'))%26)+'a';
}
ctxt[i]='\0';
printf("Cipher text: %s\n",ctxt);
//DECRYPTION
//ad-bc
z = (key[0][0]*key[1][1]) - (key[0][1]*key[1][0]);
if(z<0)
z+=26;
for(i=0;i<26;i++)
if(z*i%26==1){
z=i;
break;
}
if(i==26){
printf("Can't Decrypt with this key");
getch();
return;
}
//adj matrix
t=key[0][0]; key[0][0]=(key[1][1]*z)%26; //swap a d
key[1][1] = (t*z)%26;
key[0][1]=(26-key[0][1])*z%26; key[1][0]=(26-key[1][0])*z%26; // -b -c
for(i=0;i<=(strlen(ctxt)/2)+1;){
dtxt[i++] =((key[0][0]*(ctxt[i]-'a')+key[0][1]*(ctxt[i+1]-'a'))%26)+'a';
dtxt[i++] =((key[1][0]*(ctxt[i-1]-'a')+key[1][1]*(ctxt[i]-'a'))%26)+'a';
}
dtxt[i]='\0';
printf("Decrypted text: %s\n",dtxt);
getch();
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
int inverse(int,int);
void main()
{
char msg[2];
int p1[2][1],pt[2][1],p[2][1] ,x,t,v,m,n[2][2],f,c[2][1],k[2][2],i,j,z,a,y,x1,x2;
clrscr();
printf("\nenter plaintext mssg:");
for(i=0;i<2;i++)
scanf("%c",&msg[i]);
for(i=0;i<2;i++)
printf(" %c ",msg[i]);
printf("\nconverting mssg char to number:\n");
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
p[i][j]=msg[i];
p[i][j]=p[i][j]%97;
printf(" %d ",p[i][j]);
}
printf("\n");
}
printf("enter key");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&k[i][j]);
}
}
printf(" %c ",c[i][j]+97);
}
printf("\n");
}
z=k[0][0]*k[1][1]-k[0][1]*k[1][0];
//y=inverse(z,26);
printf("ad-bc is z %d",z);
a=z%26;
printf("\na is %d",a);
for(v=1;v<26;v++)
{
if((a*v)%26==1)
t=v;
}
printf("\nt is %d",t);
x1=k[0][0];
x2=k[1][1];
k[0][0]=x2;
k[1][1]=x1;
k[0][1]=-k[0][1];
k[1][0]=-k[1][0];
printf("\ninverse matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
n[i][j]=t*k[i][j];
printf(" %d ",n[i][j]);
}
printf("\n");
}
printf("\nmod of inverse n matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(n[i][j]<0)
n[i][j]=n[i][j]+26;
else
n[i][j]=n[i][j]%26;
printf(" %d ",n[i][j]) ;
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
p1[i][j]=0;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
for(f=0;f<2;f++)
{
p1[i][j]+=n[i][f]*c[f][j];
}
}
}
printf("after decryption plain text is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{ pt[i][j]=p1[i][j]%26;
printf("%c",pt[i][j]+97);
}
printf("\n");
}
getch();
}
/*int inverse(int a,int m)
{
int x;
a=a%m;
for(x=1;x<m;x++)
{
if((a*x)%m==1)
return x;
}
} */
OUTPUT:
Experiment – 4
PROGRAM:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DataEncryptionStandard
{
private static byte[] toHexByteArray(String self)
{
byte[] bytes = new byte[self.length() / 2];
for (int i = 0; i < bytes.length; ++i) {
bytes[i] = ((byte) Integer.parseInt(self.substring(i * 2, i * 2 + 2), 16));
}
return bytes;
}
private static void printHexBytes(byte[] self, String label)
{
System.out.printf("%s: ", label);
/* for (byte b : self)
{
int bb = (b >= 0) ? ((int) b) : b + 256;
String ts = Integer.toString(bb, 16);
if (ts.length() < 2)
{
ts = "0" + ts;
}
System.out.print(ts);
}*/
System.out.println();
}
OUTPUT:
Encoded: 0000000000000000a913f4cb0bd30f97
Decoded: 8787878787878787
Experiment – 5
AIM: Write a C/JAVA program to implement the Blowfish algorithm logic.
PROGRAM:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
class Bfish {
public static void main(String[] args) throws Exception {
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey=keygenerator.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
String inputText = JOptionPane.showInputDialog("Input your message:");
byte[] encrypted = cipher.doFinal(inputText.getBytes());
byte[] decrypted = cipher.doFinal(encrypted);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "\nEncrypted text: " + new
String(encrypted) + "\n" +"\nDecrypted text: " + new String(decrypted));
System.exit(0);
}
}
OUTPUT:
Experiment –6
AIM: Write a C/JAVA program to implement the Rijndael algorithm logic.
PROGRAM:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class AES {
public static String asHex (byte buf[])
{
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); }
return strbuf.toString(); }
public static void main(String[] args) throws Exception { String message="AES still rocks!!";
KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal((args.length == 0 ? message :args[0]).getBytes());
System.out.println("encrypted string: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
System.out.println("Original string: " + originalString + " " + asHex(original));
}
}
OUTPUT:
Experiment –7
AIM: Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello world”
using Blowfish. Create your own key using Java key tool.
PROGRAM:
import java.io.*;
class rc4
{
public static void main(String args[])throws IOException
{
int temp=0;
String ptext;
String key;
int s[]=new int[256];
int k[]=new int[256];
DataInputStream in=new DataInputStream(System.in);
System.out.println("ENTER PLAIN TEXT");
ptext=in.readLine();
System.out.println("ENTER KEY TEXT");
key=in.readLine();
char ptextc[]=ptext.toCharArray();
char keyc[]=key.toCharArray();
int cipher[]=new int[ptext.length()];
int decrypt[]=new int[ptext.length()];
int ptexti[]=new int[ptext.length()];
int keyi[]=new int[key.length()];
for(int i=0;i<ptext.length();i++)
{
ptexti[i]=(int)ptextc[i];
}
for(int i=0;i<key.length();i++)
{
keyi[i]=(int)keyc[i];
}
for(int i=0;i<255;i++)
{
s[i]=i;
k[i]=keyi[i%key.length()];
}
int j=0;
for(int i=0;i<255;i++)
{
j=(j+s[i]+k[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
int i=0;
j=0;
int z=0;
for(int l=0;l<ptext.length();l++)
{
i=(l+1)%256;
j=(j+s[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
z=s[(s[i]+s[j])%256];
cipher[l]=z^ptexti[l];
decrypt[l]=z^cipher[l];
}
System.out.println("\nENCRYPTED:");
display(cipher);
System.out.println("\nDECRYPTED:");
display(decrypt);
}
static void display(int disp[])
{
char convert[]=new char[disp.length];
for(int l=0;l<disp.length;l++)
{
convert[l]=(char)disp[l];
System.out.print(convert[l]);
}
}
OUTPUT:
Experiment –8
AIM:. Write a Java program to implement RSA algorithm
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
BigInteger p = sc.nextBigInteger();
System.out.print("Enter another prime number: ");
BigInteger q = sc.nextBigInteger();
BigInteger n = p.multiply(q);
BigInteger n2 = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e = generateE(n2);
BigInteger d = e.modInverse(n2);
int y, intGCD;
BigInteger e;
BigInteger gcd;
do {
y = x.nextInt(fiofn.intValue()-1);
String z = Integer.toString(y);
e = new BigInteger(z);
gcd = fiofn.gcd(e);
intGCD = gcd.intValue();
return e;
OUTPUT:
Experiment –9
AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript.
PROGRAM:
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;
public class DiffeHellman
{
public final static int pValue = 47;
public final static int gValue = 71;
public final static int XaValue = 9;
public final static int XbValue = 14;
public static void main(String[] args) throws Exception
{
BigInteger p = new BigInteger(Integer.toString(pValue));
BigInteger g = new BigInteger(Integer.toString(gValue));
BigInteger Xa = new BigInteger(Integer.toString(XaValue));
BigInteger Xb =new BigInteger(Integer.toString(XbValue));
createKey();
int bitLength = 512;
SecureRandom rnd = new SecureRandom();
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p, g);
}
public static void createKey() throws Exception
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");
Experiment –10
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import java.security.*;
try
{
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(input.getBytes());
System.out.println();
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("");
catch (Exception e)
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
returnbuf.toString();
OUTPUT:
Experiment –11
AIM: Calculate the message digest of a text using the MD5 algorithm in JAVA
PROGRAM:
import java.security.*;
try
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(input.getBytes());
System.out.println();
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
catch (Exception e)
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
return buf.toString();
OUTPUT: