0% found this document useful (0 votes)
23 views

C&NS Lab Manual 1-11

The document is a lab manual for a Cryptography and Network Security course aimed at B.Tech students, detailing objectives and outcomes related to encryption techniques using C and Java. It includes a series of experiments that guide students through implementing various encryption algorithms, such as XOR operations, DES, AES, RSA, and hashing functions like MD5 and SHA-1. Additionally, it provides reference books for further study in the field of cryptography and network security.

Uploaded by

sahasrana7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

C&NS Lab Manual 1-11

The document is a lab manual for a Cryptography and Network Security course aimed at B.Tech students, detailing objectives and outcomes related to encryption techniques using C and Java. It includes a series of experiments that guide students through implementing various encryption algorithms, such as XOR operations, DES, AES, RSA, and hashing functions like MD5 and SHA-1. Additionally, it provides reference books for further study in the field of cryptography and network security.

Uploaded by

sahasrana7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

CRYPTOGRAPHY & NETWORK SECURITY

B.Tech. III Year II Sem. L T P C


Course Code: CS603PC 0 0 3 4
Course Objectives:
To write programs in C/Java to encrypt / Encode using XOR operations.
To write programs in C/Java to encrypt using Substitution Techniques.
To write programs in C/Java to encrypt using Symmetric and Asymmetric Encryption.
To write programs in C/Java to to perform HASH functions using MD5, SHA-1.
Course Outcomes:
Ability to write programs in C/Java to encrypt messages using encryption techniques
such as DES,AES,RSA & Diffie-Hellman key Exchage.
List of Experiments:
1. 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.
2. 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.
3. Write a Java program to perform encryption and decryption using the following
algorithms
a. Ceaser cipher b. Substitution cipher c. Hill Cipher
4. Write a C/JAVA program to implement the DES algorithm logic.
5. Write a C/JAVA program to implement the Blowfish algorithm logic.
6. Write a C/JAVA program to implement the Rijndael algorithm logic.
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello
world” using Blowfish. Create your own key using Java key tool.
8. Write a Java program to implement RSA algorithm.
9. Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript.
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
11. Calculate the message digest of a text using the MD5 algorithm in JAVA

Department of Computer Science and Engineering,PEC Page 1


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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.

Department of Computer Science and Engineering,PEC Page 2


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 3


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

OUTPUT:

Department of Computer Science and Engineering,PEC Page 4


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 5


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

printf("\nEXOR with 127\n");


for(i=0;i<len;i++)
{
str3[i] = str2[i]^127;
printf("%c ",str3[i]);
}
printf("\n");
getch();
}

OUTPUT:

Department of Computer Science and Engineering,PEC Page 6


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 7


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

plain[i]=plain[i]+26;
if(islower(cipher[i])&&(plain[i]<'a'))
plain[i]=plain[i]+26;
printf("%c",plain[i]);
}
getch();
}
OUTPUT:

Department of Computer Science and Engineering,PEC Page 8


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 9


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

}
dtxt[i]='\0';
printf("Decrypted text: %s\n",dtxt);

getch();
}

OUTPUT:

Department of Computer Science and Engineering,PEC Page 10


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

PROGRAM: HILL CIPHER

#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]);
}
}

Department of Computer Science and Engineering,PEC Page 11


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

printf("\nkey is\n ");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",k[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
c[i][j]=0;
}
}
printf("encryption to get cipher");
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
for(f=0;f<2;f++)
{
c[i][j]+=k[i][f]*p[f][j];
}
}
}
printf("\ncipher is\n ");
for(i=0;i<2;i++)
{
for(j=0;j<1;j++)
{
c[i][j]=c[i][j]%26;

Department of Computer Science and Engineering,PEC Page 12


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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++)

Department of Computer Science and Engineering,PEC Page 13


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

{
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");
}

Department of Computer Science and Engineering,PEC Page 14


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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:

Department of Computer Science and Engineering,PEC Page 15


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

Experiment – 4

AIM: Write a C/JAVA program to implement the DES algorithm logic.

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

Department of Computer Science and Engineering,PEC Page 17


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

public static void main(String[] args) throws Exception


{
String strKey = "0e329232ea6d0d73";
byte[] keyBytes = toHexByteArray(strKey);
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
Cipher encCipher = Cipher.getInstance("DES");
encCipher.init(Cipher.ENCRYPT_MODE, key);
String strPlain = "8787878787878787";
byte[] plainBytes = toHexByteArray(strPlain);
byte[] encBytes = encCipher.doFinal(plainBytes);
printHexBytes(encBytes, "Encoded");
Cipher decCipher = Cipher.getInstance("DES");
decCipher.init(Cipher.DECRYPT_MODE, key);
byte[] decBytes = decCipher.doFinal(encBytes);
printHexBytes(decBytes, "Decoded");
}
}

OUTPUT:
Encoded: 0000000000000000a913f4cb0bd30f97
Decoded: 8787878787878787

Department of Computer Science and Engineering,PEC Page 18


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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:

Department of Computer Science and Engineering,PEC Page 19


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 20


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

OUTPUT:

Department of Computer Science and Engineering,PEC Page 21


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

Department of Computer Science and Engineering,PEC Page 22


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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++)

Department of Computer Science and Engineering,PEC Page 23


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

{
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++)

Department of Computer Science and Engineering,PEC Page 24


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

{
convert[l]=(char)disp[l];
System.out.print(convert[l]);
}
}

OUTPUT:

Department of Computer Science and Engineering,PEC Page 25


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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;

public class RSA

static Scanner sc = new Scanner(System.in);


public static void main(String[] args)
{
System.out.print("Enter a Prime number: ");

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

System.out.println("Encryption keys are: " + e + ", " + n);

System.out.println("Decryption keys are: " + d + ", " + n);

public static BigIntegergenerateE(BigIntegerfiofn)

int y, intGCD;

Department of Computer Science and Engineering,PEC Page 26


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

BigInteger e;

BigInteger gcd;

Random x = new Random();

do {

y = x.nextInt(fiofn.intValue()-1);

String z = Integer.toString(y);

e = new BigInteger(z);

gcd = fiofn.gcd(e);

intGCD = gcd.intValue();

while(y <= 2 || intGCD != 1);

return e;

Department of Computer Science and Engineering,PEC Page 27


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

OUTPUT:

Department of Computer Science and Engineering,PEC Page 28


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

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

Department of Computer Science and Engineering,PEC Page 29


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

DHPublicKeySpec kspec = (DHPublicKeySpec)


kfactory.getKeySpec(kp.getPublic(),DHPublicKeySpec.class);
System.out.println("Public key is: " +kspec);
}
public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception
{
KeyPairGenerator kpg =KeyPairGenerator.getInstance("DiffieHellman");
DHParameterSpec param = new DHParameterSpec(p, g);
kpg.initialize(param);
KeyPair kp = kpg.generateKeyPair();
KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec kspec = (DHPublicKeySpec)
kfactory.getKeySpec(kp.getPublic(),DHPublicKeySpec.class);
System.out.println("\nPublic key is : " +kspec);
}
}
OUTPUT:

Department of Computer Science and Engineering,PEC Page 30


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

Experiment –10

AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.

PROGRAM:
import java.security.*;

public class SHA1

public static void main(String[] a)

try
{

MessageDigest md = MessageDigest.getInstance("SHA1");

System.out.println("Message digest object info: ");

System.out.println(" Algorithm = " +md.getAlgorithm());

System.out.println(" Provider = " +md.getProvider());

System.out.println(" ToString = " +md.toString());

String input = "";

md.update(input.getBytes());

byte[] output = md.digest();

System.out.println();

System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));

input = "abc";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));

input = "abcdefghijklmnopqrstuvwxyz";

Department of Computer Science and Engineering,PEC Page 31


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output));

System.out.println("");

catch (Exception e)

System.out.println("Exception: " +e);

public static String bytesToHex(byte[] b)

char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

StringBufferbuf = new StringBuffer();

for (int j=0; j<b.length; j++)

buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

buf.append(hexDigit[b[j] & 0x0f]);

returnbuf.toString();

Department of Computer Science and Engineering,PEC Page 32


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

OUTPUT:

Department of Computer Science and Engineering,PEC Page 33


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

Experiment –11

AIM: Calculate the message digest of a text using the MD5 algorithm in JAVA

PROGRAM:
import java.security.*;

public class MD5

public static void main(String[] a)

try

MessageDigest md = MessageDigest.getInstance("MD5");

System.out.println("Message digest object info: ");

System.out.println(" Algorithm = " +md.getAlgorithm());

System.out.println(" Provider = " +md.getProvider());

System.out.println(" ToString = " +md.toString());

String input = "";

md.update(input.getBytes());

byte[] output = md.digest();

System.out.println();

System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));

input = "abc";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));

Department of Computer Science and Engineering,PEC Page 34


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

input = "abcdefghijklmnopqrstuvwxyz";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("MD5(\"" +input+"\") ="+bytesToHex(output)); System.out.println("");

catch (Exception e)

System.out.println("Exception: " +e);

public static String bytesToHex(byte[] b)

char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

StringBufferbuf = new StringBuffer();

for (int j=0; j<b.length; j++)

buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

buf.append(hexDigit[b[j] & 0x0f]);

return buf.toString();

Department of Computer Science and Engineering,PEC Page 35


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

OUTPUT:

Department of Computer Science and Engineering,PEC Page 36


CRYPTOGRAPHY & NETWORK SECURITY LAB MANUAL

Department of Computer Science and Engineering,PEC Page 37

You might also like