0% found this document useful (0 votes)
34 views24 pages

Cryptography Lab Manual for CSE Students

This document is a lab manual for Cryptography and Network Security, authored by Ramya Asalatha Busi. It includes various programming exercises such as implementing and breaking different ciphers, message authentication codes, cryptographic hash functions, and symmetric encryption algorithms. Each exercise is detailed with code examples and expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views24 pages

Cryptography Lab Manual for CSE Students

This document is a lab manual for Cryptography and Network Security, authored by Ramya Asalatha Busi. It includes various programming exercises such as implementing and breaking different ciphers, message authentication codes, cryptographic hash functions, and symmetric encryption algorithms. Each exercise is detailed with code examples and expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CRYPTOGRAPHY &

NETWORK SECURITY
Lab Manual

JNTUK – R20
AUTHOR: RAMYA ASALATHA BUSI
Assoc. Professor, CSE
[Link] Cryptography and Network Security Lab

[Link] Program Page no.


1 To Implement and Break Shift Cipher 2
2 To Implement Mono – Alphabetic Cipher 4
3 To Implement One – Time Pad Cipher 6
4 To Implement Message Authentication Codes (MD5) 9
5 To Implement Cryptographic Hash Function (SHA-256) 11
6 To Implement Symmetric Encryption Cipher DES 13
7 To Implement Symmetric Encryption Cipher AES 15
8 To Implement Diffie – Hellman Key Establishment 17
9 To Implement Public – Key Cryptosystems (RSA) 20
10 To Implement Digital Signatures (DSA) 22

VVIT
1
[Link] Cryptography and Network Security Lab

1. Aim: Program to break Shift Cipher

Program:
import [Link].*;
class CaesarCipher
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int shift,i,n,p,key;
String str;
String str1="";
[Link]("Enter the Plain Text");
str=[Link]();
str=[Link]();
n=[Link]();
char ch1[]=[Link]();
char ch4;
[Link]("Enter the value by which each letter of the string is to be shifted");
shift=[Link]();
[Link]();
[Link]("Encrypted text is:");
for(i=0;i<n;i++)
{
if([Link](ch1[i]))
{
ch4=(char)(((int)ch1[i]+shift-97)%26+97);
str1=str1+ch4;
}
else if(ch1[i]==' ')
{
str1=str1+ch1[i];
}
}
[Link](str1);
[Link]("Cipher Text:"+str1);
n=[Link]();
char ch2[]=[Link]();
char ch3;
[Link]();
[Link]("Possible Plain text is");
str1="";
for(key=26;key>=1;key--)
{
for(i=0;i<n;i++)
{
if([Link](ch2[i]))
{

VVIT
2
[Link] Cryptography and Network Security Lab

ch3=(char)(((int)ch2[i]+key-97)%26+97);
str1=str1+ch3;
}
else if(ch2[i]==' ')
{
str1=str1+ch2[i];
}
}
p=26-key;
[Link]("For Key "+p+":"+str1);
str1="";
}
}
}

Output:

VVIT
3
[Link] Cryptography and Network Security Lab

2. Aim: Program to break Mono-alphabetic cipher

Program:
import [Link];
public class MonoalphabeticCipher
{
public static char p[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z' };
public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
'V', 'B', 'N', 'M' };
static String str;
public static String doEncryption(String s)
{
char c[] = new char[([Link]())];
for (int i = 0; i < [Link](); i++)
{
for (int j = 0; j < 26; j++)
{
if (p[j] == [Link](i))
{
c[i] = ch[j];
break;
}
}
}
return (new String(c));
}

public static String doDecryption(String s)


{
char p1[] = new char[([Link]())];
for (int i = 0; i < [Link](); i++)
{
for (int j = 0; j < 26; j++)
{
if (ch[j] == [Link](i))
{
p1[i] = p[j];
break;
}
}
}
return (new String(p1));
}

public static void main(String args[])

VVIT
4
[Link] Cryptography and Network Security Lab

{
Scanner sc = new Scanner([Link]);
[Link]("Enter the message: ");
str=[Link]();
String en = doEncryption([Link]());
[Link]("Encrypted message: " + en);
[Link]("Decrypted message: " + doDecryption(en));
[Link]();
}
}

Output:

VVIT
5
[Link] Cryptography and Network Security Lab

3. Aim: Program to implement One-time pad

Program:
import [Link].*;
class msg{
int a=97;
char all[]=new char[27];
msg()
{
for(int i=0;i<26;i++)
{
all[i]=(char)a;
a++;
}
}
int Ipos(char c)
{
int i=0;
for(;i<26;i++)
{
if(all[i]==c)
{
break;
}
}
return i;
}
char Cpos(int c)
{
int i=0;
for(;i<c;i++)
{
}
return all[i];
}
}
class OneTimePadCipherImplementation{
String Encryption(String plaintext,String key)
{
plaintext=[Link]();
msg m1=new msg();
int pt[]=new int[[Link]()];
int k[]=new int[[Link]()];
int ct[]=new int[[Link]()];
for(int i=0;i < [Link]();i++)
{
pt[i]=[Link]([Link](i));
}

VVIT
6
[Link] Cryptography and Network Security Lab

for(int i=0;i < [Link]();i++)


{
k[i]=[Link]([Link](i));
}
int j=0;
for(int i=0;i < [Link]();i++)
{
ct[i]=pt[i]+k[j];
j++;
if(j==[Link]())
j=0;
if(ct[i]>26)
ct[i]=ct[i]%26;
}
String cipher="";
for(int i=0;i < [Link]();i++)
{
cipher+=[Link](ct[i]);
}
return cipher;
}
String Decryption(String ciphertext,String key)
{
String plaintext="";
msg m1=new msg();
int pt[]=new int[[Link]()];
int k[]=new int[[Link]()];
int ct[]=new int[[Link]()];
for(int i=0;i < [Link]();i++)
{
ct[i]=[Link]([Link](i));
}
for(int i=0;i < [Link]();i++)
{
k[i]=[Link]([Link](i));
}
int j=0;
for(int i=0;i < [Link]();i++)
{
pt[i]=ct[i]-k[j];
j++;
if(j==[Link]())
j=0;
if(pt[i] < 0)
pt[i]+=26;
}
String cipher="";
for(int i=0;i < [Link]();i++)
{
plaintext+=[Link](pt[i]);

VVIT
7
[Link] Cryptography and Network Security Lab

}
return plaintext;
}
}

class OneTimePad{
public static void main(String args[])throws Exception
{
String plaintext,key;
Scanner scn=new Scanner([Link]);
[Link]("Enter plaintext:");
plaintext=[Link]();
[Link]("Enter key:");
key=[Link]();
OneTimePadCipherImplementation OneTimePad=new
OneTimePadCipherImplementation();
String ciphertext=[Link](plaintext,key);
[Link]("Encrpted text is:"+ciphertext);
[Link]("Decrypted text is:"+[Link](ciphertext,key));
}
}

Output:

VVIT
8
[Link] Cryptography and Network Security Lab

4. Aim: Program to implement Message authentication codes (MD5)

Program:
import [Link];
import [Link];
import [Link];

// Java program to calculate MD5 hash value


public class MD5 {
public static String getMd5(String input)
{
try {

// Static getInstance method is called with hashing MD5


MessageDigest md = [Link]("MD5");

// digest() method is called to calculate message digest


// of an input digest() return array of byte
byte[] messageDigest = [Link]([Link]());

// Convert byte array into signum representation


BigInteger no = new BigInteger(1, messageDigest);

// Convert message digest into hex value


String hashtext = [Link](16);
while ([Link]() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}

// For specifying wrong message digest algorithms


catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "VVIT";
[Link]("Your HashCode Generated by MD5 is: " + getMd5(s));
}
}

VVIT
9
[Link] Cryptography and Network Security Lab

Output:

VVIT
10
[Link] Cryptography and Network Security Lab

5. Aim: Program to implement Cryptographic Hash Function (SHA-256)

Program:
import [Link];
import [Link];
import [Link];

// Java program to calculate MD5 hash value


public class SHA {
public static String getSHA(String input)
{
try {

// Static getInstance method is called with hashing MD5


MessageDigest hash = [Link]("SHA-256");

// digest() method is called to calculate message digest


// of an input digest() return array of byte
byte[] messageDigest = [Link]([Link]());

// Convert byte array into signum representation


BigInteger no = new BigInteger(1, messageDigest);

// Convert message digest into hex value


String hashtext = [Link](16);
while ([Link]() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}

// For specifying wrong message digest algorithms


catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "VVIT";
[Link]("Your HashCode Generated by SHA is: " + getSHA(s));
}
}

VVIT
11
[Link] Cryptography and Network Security Lab

Output:

VVIT
12
[Link] Cryptography and Network Security Lab

6. Aim: Program to implement DES Symmetric Encryption

Program:
import [Link];
import [Link];
import [Link];
import [Link].Base64;

class DesEncrypter {
Cipher ecipher;
Cipher dcipher;

DesEncrypter(SecretKey key) throws Exception {


ecipher = [Link]("DES");
dcipher = [Link]("DES");
[Link](Cipher.ENCRYPT_MODE, key);
[Link](Cipher.DECRYPT_MODE, key);
}

public String encrypt(String str) throws Exception {


// Encode the string into bytes using utf-8
byte[] utf8 = [Link]("UTF8");

// Encrypt
byte[] enc = [Link](utf8);

// Encode bytes to base64 to get a string


//return new [Link].BASE64Encoder().encode(enc);
return [Link]().encodeToString(enc);
}

public String decrypt(String str) throws Exception {


// Decode base64 to get bytes
//byte[] dec = new [Link].BASE64Decoder().decodeBuffer(str);
byte[] dec = [Link]().decode(str);
byte[] utf8 = [Link](dec);

// Decode using utf-8


return new String(utf8, "UTF8");
}
}

public class DES {


public static void main(String[] argv) throws Exception {
SecretKey key = [Link]("DES").generateKey();
DesEncrypter encrypter = new DesEncrypter(key);
String encrypted = [Link]("Don't tell anybody!");
[Link](encrypted);

VVIT
13
[Link] Cryptography and Network Security Lab

String decrypted = [Link](encrypted);


[Link](decrypted);
}
}

Output:

VVIT
14
[Link] Cryptography and Network Security Lab

7. Aim: Program to implement AES Symmetric Encryption

Program:
import [Link];
import [Link];
import [Link];
import [Link].Base64;

class AesEncrypter {
Cipher ecipher;

Cipher dcipher;

AesEncrypter(SecretKey key) throws Exception {


ecipher = [Link]("AES");
dcipher = [Link]("AES");
[Link](Cipher.ENCRYPT_MODE, key);
[Link](Cipher.DECRYPT_MODE, key);
}

public String encrypt(String str) throws Exception {


// Encode the string into bytes using utf-8
byte[] utf8 = [Link]("UTF8");

// Encrypt
byte[] enc = [Link](utf8);

// Encode bytes to base64 to get a string


//return new [Link].BASE64Encoder().encode(enc);
return [Link]().encodeToString(enc);
}

public String decrypt(String str) throws Exception {


// Decode base64 to get bytes
//byte[] dec = new [Link].BASE64Decoder().decodeBuffer(str);
byte[] dec = [Link]().decode(str);

byte[] utf8 = [Link](dec);

// Decode using utf-8


return new String(utf8, "UTF8");
}
}

public class AES {


public static void main(String[] argv) throws Exception {
SecretKey key = [Link]("AES").generateKey();
AesEncrypter encrypter = new AesEncrypter(key);

VVIT
15
[Link] Cryptography and Network Security Lab

String encrypted = [Link]("Don't tell anybody!");


[Link](encrypted);
String decrypted = [Link](encrypted);
[Link](decrypted);
}
}

Output:

VVIT
16
[Link] Cryptography and Network Security Lab

8. Aim: Program to implement Diffie – Hellman Key Establishment

Program:

[Link]

import [Link].*;
import [Link].*;

public class DHServer {


public static void main(String[] args) throws IOException
{
try {
int port = 8088;
// Server Key
int b = 3;
// Client p, g, and key
double clientP, clientG, clientA, B, Bdash;
String Bstr;

// Established the Connection


ServerSocket serverSocket = new ServerSocket(port);
[Link]("Waiting for client on port " + [Link]() +
"...");
Socket server = [Link]();
[Link]("Just connected to " + [Link]());

// Server's Private Key


[Link]("From Server : Private Key = " + b);

// Accepts the data from client


DataInputStream in = new DataInputStream([Link]());

clientP = [Link]([Link]()); // to accept p


[Link]("From Client : P = " + clientP);

clientG = [Link]([Link]()); // to accept g


[Link]("From Client : G = " + clientG);

clientA = [Link]([Link]()); // to accept A


[Link]("From Client : Public Key = " + clientA);

B = (([Link](clientG, b)) % clientP); // calculation of B


Bstr = [Link](B);

// Sends data to client


// Value of B
OutputStream outToclient = [Link]();
DataOutputStream out = new DataOutputStream(outToclient);

VVIT
17
[Link] Cryptography and Network Security Lab

[Link](Bstr); // Sending B
Bdash = (([Link](clientA, b)) % clientP); // calculation of Bdash
[Link]("Secret Key to perform Symmetric Encryption = "
+ Bdash);
[Link]();
}
catch (SocketTimeoutException s) {
[Link]("Socket timed out!");
}
catch (IOException e) {
}
}
}

[Link]

import [Link].*;
import [Link].*;

public class DHClient {


public static void main(String[] args)
{
try {
String pstr, gstr, Astr;
String serverName = "localhost";
int port = 8088;

// Declare p, g, and Key of client


int p = 23;
int g = 9;
int a = 4;
double Adash, serverB;

// Established the connection


[Link]("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
[Link]("Just connected to "
+ [Link]());

// Sends the data to client


OutputStream outToServer = [Link]();
DataOutputStream out = new DataOutputStream(outToServer);

pstr = [Link](p);
[Link](pstr); // Sending p

gstr = [Link](g);
[Link](gstr); // Sending g

VVIT
18
[Link] Cryptography and Network Security Lab

double A = (([Link](g, a)) % p); // calculation of A


Astr = [Link](A);
[Link](Astr); // Sending A

// Client's Private Key


[Link]("From Client : Private Key = " + a);

// Accepts the data


DataInputStream in = new DataInputStream([Link]());

serverB = [Link]([Link]());
[Link]("From Server : Public Key = " + serverB);

Adash = (([Link](serverB, a)) % p); // calculation of Adash

[Link]("Secret Key to perform Symmetric Encryption = "


+ Adash);
[Link]();
}
catch (Exception e) {
[Link]();
}
}
}

Output:

VVIT
19
[Link] Cryptography and Network Security Lab

9. Aim: Program to implement Public Key Cryptosystems (RSA)

Program:
import [Link];
import [Link];

public class RSADemo {


private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();

private BigInteger privateKey;


private BigInteger publicKey;
private BigInteger modulus;

// generate an N-bit (roughly) public and private key


RSADemo(int N) {
BigInteger p = [Link](N/2, random);
BigInteger q = [Link](N/2, random);
BigInteger phi = ([Link](one)).multiply([Link](one));
[Link]("prime p = " + p);
[Link]("prime q = " + q);

modulus = [Link](q);
[Link]("phi = " + phi);
publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1
privateKey = [Link](phi);
}

BigInteger encrypt(BigInteger message) {


return [Link](publicKey, modulus);
}

BigInteger decrypt(BigInteger encrypted) {


return [Link](privateKey, modulus);
}

public String toString() {


String s = "";
s += "public = " + publicKey + "\n";
s += "private = " + privateKey + "\n";
s += "modulus = " + modulus;
return s;
}

public static void main(String[] args) {


int N = [Link](args[0]);
RSADemo key = new RSADemo(N);
[Link](key);

VVIT
20
[Link] Cryptography and Network Security Lab

// create random message, encrypt and decrypt


BigInteger message = new BigInteger("8");

//// create message by converting string to integer


// String s = "test";
// byte[] bytes = [Link]();
// BigInteger message = new BigInteger(bytes);

BigInteger encrypt = [Link](message);


BigInteger decrypt = [Link](encrypt);
[Link]("message = " + message);
[Link]("encrypted = " + encrypt);
[Link]("decrypted = " + decrypt);
}
}

Output:

VVIT
21
[Link] Cryptography and Network Security Lab

10. Aim: Program to Implement Digital Signatures (DSS)

Program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;

public class CreatingDigitalSignature {


public static void main(String args[]) throws Exception {
//Accepting text from user
Scanner sc = new Scanner([Link]);
[Link]("Enter some text");
String msg = [Link]();

//Creating KeyPair generator object


KeyPairGenerator keyPairGen = [Link]("DSA");

//Initializing the key pair generator


[Link](2048);

//Generate the pair of keys


KeyPair pair = [Link]();

//Getting the private key from the key pair


PrivateKey privKey = [Link]();
PublicKey pubKey = [Link]();
[Link](privKey);
[Link](pubKey);

//Creating a Signature object


Signature sign = [Link]("SHA256withDSA");

//Initialize the signature


[Link](privKey);
byte[] bytes = "msg".getBytes();

//Adding data to the signature


[Link](bytes);

//Calculating the signature


byte[] signature = [Link]();

//Printing Signature to console


for(int i=0;i<[Link];i++)

VVIT
22
[Link] Cryptography and Network Security Lab

[Link](" "+signature[i]);
[Link]();

//Initializing the signature verification


[Link]([Link]());
[Link](bytes);

//Verifying the signature


boolean bool = [Link](signature);

if(bool) {
[Link]("Signature verified");
} else {
[Link]("Signature failed");
}
}
}

Output:

VVIT
23

You might also like