0% found this document useful (0 votes)
36 views17 pages

LAB Exercises

Computer Science material

Uploaded by

Lone Faizan
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)
36 views17 pages

LAB Exercises

Computer Science material

Uploaded by

Lone Faizan
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
You are on page 1/ 17

LAB MANUAL: Fourth Year CSE- Semester 7

CRYPTOGRAPHY & NETWORK SECURITY LAB

L1

Explain security concepts, Ethics in Network Security.

L2

i) Write a Java program to perform encryption and decryption using the


following algorithms:

a. Ceaser Cipher

b. Substitution Cipher

c. Hill Cipher

a) Ceaser Cipher

import java.io.BufferedReader; import java.io.IOException; import


java.io.InputStreamReader; import

java.util.Scanner;

public class CeaserCipher {

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

staticBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));public
static void

main(String[] args) throws IOException {

// TODO code application logic here

System.out.print("Enter any String: "); String str = br.readLine();

System.out.print("\nEnter the Key: ");


int key = sc.nextInt();

String encrypted = encrypt(str, key);

System.out.println("\nEncrypted String is: " +encrypted);

Stringdecrypted=decrypt(encrypted, key); System.out.println("\nDecrypted String is:


"+decrypted);

System.out.println("\n");

public static String encrypt(String str, int key)

String encrypted ="";

for(int i = 0; i < str.length(); i++)

int c= str.charAt(i);

if (Character.isUpperCase(c))

c = c + (key % 26);

if (c > 'Z')

c = c - 26;

else if (Character.isLowerCase(c)) {

c = c + (key % 26);
if (c > 'z')

c = c - 26;

encrypted += (char) c;

return encrypted;

public static String decrypt(String str, int key)

String decrypted = "";

for(int i= 0; i< str.length(); i++)

int c= str.charAt(i);

if(Character.isUpperCase(c))

c = c - (key % 26);

if (c < 'A')

c = c + 26;

else if (Character.isLowerCase(c))

{
c = c - (key % 26);

if (c < 'a')

c = c + 26;

Output:

Enterany String: HelloWorld Enter the Key: 5

Encrypted String is: MjqqtBtwqi DecryptedStringis:HelloWor

b) Substitution Cipher

PROGRAM:

import java.io.*;

import java.util.*;

public class SubstitutionCipher

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

staticBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in))

;public static void main(String[] args) throws IOException

// TODO code application logic here

String a

String a= "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";

System.out.print("Enter any string: ");

String str = br.readLine();

String decrypt = "";

char c;

for(int i=0;i<str.length();i++)

c=str.charAt(i);

int j = a.indexOf(c);

decrypt = decrypt+b.charAt(j);

System.out.println("The encrypted data is: " +decrypt);

Output:

Enter any string: aceho

The encrypted data is: zxvsl

c)HillCipher

PROGRAM:

import java.io.*;

import java.util.*;
import java.io.*; public class HillCipher {

staticfloat[][] decrypt= newfloat[3][1];

staticfloat[][] a= newfloat[3][3];

static float[][]b=newfloat[3][3];

staticfloat[][] mes=newfloat[3][1];

staticfloat[][]res= new float[3][1];

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

public static void main(String[] args) throws IOException {

//TODOcode applicationlogic here getkeymes();

for(int i=0;i<3;i++)

for(int j=0;j<1;j++)

(int k=0;k<3;k++) {

res[i][j]=res[i][j]+a[i][k]*mes[k][j];

System.out.print("\nEncrypted string is : ");

for(int i=0;i<3;i++)

{ System.out.print((char)(res[i][0]%26+97));

[i][0]=res[i][0];

inverse();
for(int i=0;i<3; i++)

for(int j=0;j<1; j++)

for(int k=0; k<3;k++)

decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; } System.out.print("\nDecrypted string is :


");

for(inti =0; i<3; i++)

System.out.print((char)(decrypt[i][0]% 26+97));

System.out.print("\n");

public static void getkeymes() throws IOException

System.out.println("Enter 3x3 matrix for key (It should be inversible): ");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

a[i][j]= sc.nextFloat();

System.out.print("\nEnter a 3 letter string: ");

String msg = br.readLine();

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


= msg.charAt(i)-97;

public static void inverse()

floatp,q;

float[][] c= a;

for(int i=0;i<3;i++)

for(int j=0;j<3;j++) {

//a[i][j]=sc.nextFl oat();

if(i==j)

b[i][j]=1;

else

b[i][j]=0;

fo r(int k=0;k<3; k++)

for(int i=0;i<3;i++)

p = c[i][k];

q = c[k][k];

for(int j=0;j<3;j++)
{

if(i!=k)

c[i][j] = c[i][j]*q-p*c[k][j];

b[i][j] =b[i][j]*q-p*b[k][j];

}}}}

for(int i=0;i<3;i++) for(int j=0;j<3;j++)

{ b[i][j] = b[i][j]/c[i][i]; }

System.out.println(""); System.out.println("\nInverse Matrix is : "); for(int i=0;i<3;i++)


{

for(int j=0;j<3;j++) System.out.print(b[i][j] + " ");

System.out.print("\n"); }

}}

Output:

Entera3letterstring:hai Encrypted string is :fdx Inverse Matrix is:

0.083333336 0.41666666 -0.33333334

-0.41666666 -0.083333336 0.6666667

0.5833333 -0.083333336 -0.33333334

Decrypted string is: hai

ii) Write a C program to perform encryption and decryption using the following
algorithms:

a. Ceaser Cipher
b. RSA

c. Hill Cipher

#include <stdio.h>
#include <string.h>

// Function to encrypt the text


void encrypt(char text[], int shift) {
for (int i = 0; text[i] != '\0'; ++i) {
char ch = text[i];
if (ch >= 'a' && ch <= 'z') {
ch = (ch - 'a' + shift) % 26 + 'a';
} else if (ch >= 'A' && ch <= 'Z') {
ch = (ch - 'A' + shift) % 26 + 'A';
}
text[i] = ch;
}
}

// Function to decrypt the text


void decrypt(char text[], int shift) {
for (int i = 0; text[i] != '\0'; ++i) {
char ch = text[i];
if (ch >= 'a' && ch <= 'z') {
ch = (ch - 'a' - shift + 26) % 26 + 'a';
} else if (ch >= 'A' && ch <= 'Z') {
ch = (ch - 'A' - shift + 26) % 26 + 'A';
}
text[i] = ch; }
}int main() {
char text[100];
int shift;
printf("Enter a message to encrypt: ");
gets(text);
printf("Enter shift amount: ");
scanf("%d", &shift);

encrypt(text, shift);
printf("Encrypted message: %s\n", text);

decrypt(text, shift);
printf("Decrypted message: %s\n", text);

return 0;
}

b. RSA

//Write a C program to implement RSA Algorithm


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;


char msg[100];

int prime(long int);


void ce();
long int cd(long int);
void encrypt();
void decrypt();

int main() {
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%ld", &q);
flag = prime(q);
if (flag == 0 || p == q) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%[^\n]", msg);
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i = 0; i < j - 1; i++)
printf("%ld\t%ld\n", e[i], d[i]);
encrypt();
decrypt();
return 0;
}

int prime(long int pr) {


int i;
if (pr == 1)
return 0;
for (i = 2; i <= sqrt(pr); i++) {
if (pr % i == 0)
return 0;
}
return 1;
}

void ce() {
int k = 0;
for (i = 2; i < t; i++) {
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q) {
e[k] = i;
flag = cd(e[k]);
if (flag > 0) {
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}

long int cd(long int x) {


long int k = 1;
while (1) {
k = k + t;
if (k % x == 0)
return (k / x);
}
}

void encrypt() {
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i != len) {
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++) {
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}

void decrypt() {
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1) {
ct = temp[i];
k = 1;
for (j = 0; j < key; j++) {
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
}

c) Hill Cipher
// C++ code to implement Hill Cipher
#include <iostream>
using namespace std;

// Following function generates the


// key matrix for the key string
void getKeyMatrix(string key, int keyMatrix[][3])
{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key[k]) % 65;
k++;
}
}
}

// Following function encrypts the message


void encrypt(int cipherMatrix[][1],
int keyMatrix[][3],
int messageVector[][1])
{
int x, i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
cipherMatrix[i][j] = 0;

for (x = 0; x < 3; x++)


{
cipherMatrix[i][j] +=
keyMatrix[i][x] * messageVector[x][j];
}

cipherMatrix[i][j] = cipherMatrix[i][j] % 26;


}
}
}

// Function to implement Hill Cipher


void HillCipher(string message, string key)
{
// Get key matrix from the key string
int keyMatrix[3][3];
getKeyMatrix(key, keyMatrix);

int messageVector[3][1];

// Generate vector for the message


for (int i = 0; i < 3; i++)
messageVector[i][0] = (message[i]) % 65;

int cipherMatrix[3][1];

// Following function generates


// the encrypted vector
encrypt(cipherMatrix, keyMatrix, messageVector);

string CipherText;

// Generate the encrypted text from


// the encrypted vector
for (int i = 0; i < 3; i++)
CipherText += cipherMatrix[i][0] + 65;

// Finally print the ciphertext


cout << " Ciphertext:" << CipherText;
}

// Driver function for above code


int main()
{
// Get the message to be encrypted
string message = "ACT";

// Get the key


string key = "GYBNQKURP";

HillCipher(message, key);

return 0;
}

Snap_Shorts of Crypttool_Exercise
Snap_Shorts of Wireshark_Exercise

You might also like