Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 47

INDEX

PAGE
EXP. NO DATE EXPERIMENT NAME SIGNATURE
NO

a. Caesar Cipher 1

b. Playfair Cipher 3

c. Hill Cipher 9
1.
d. Vigenere Cipher 12

e. Rail fence – row & column transformation 15

f. Affine Cipher 17

a. DES Algorithm 21

b. RSA Algorithm 24

2. c. Diffie-Hellman Algorithm 26

d. MD5 Algorithm 28

e. SHA-1 Algorithm 30

3. Digital Signature Algorithm (DSA) 32

4. Honey Pot Setup 36

5. Wireless Audit 38

6. SNORT IDS 42

0
Exp. No: 1a
Caesar Cipher
Date:

Aim:
To write a Java program to encrypt a message using Caesar cipher encryption technique.
Algorithm:
Step 1: Read the key and message to be encrypted from input.
Step 2: Initialize an empty string for the encrypted message and set an index counter to zero.
Step 3: For each character in the message string, perform the following:
- If the character is an uppercase letter, convert by shifting within the uppercase range
using the given integer.
- If the character is a lowercase letter, convert by shifting within the lowercase range
using the given integer.
Step 4: Append each converted character to the result string and increase the index counter by
one.
Step 5: Display the encrypted message string.
Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int n;
char msg[100];
char result[100];
int k=0;
scanf("%d",&n);
scanf("%s",msg);
for (int i = 0; i < strlen(msg); i++)
{
if (isupper(msg[i]))
result[k++]=(char)(((msg[i] + n - 65)%26)+65);
else

1
result[k++] =(char)(((msg[i] + n - 97) % 26) + 65);
}
printf("%s",result);
}

Output:
[root@localhost security lab]# gcc caes.c -o caesar
[root@localhost security lab]# ./caesar
Plaintext: abc
Cipher text: def

Result:
Thus the above program is executed successfully and the output is verified.

2
Exp. No: 1b
Play Fair Cipher
Date:

Aim:
To write a C program to encrypt a message using Playfair cipher encryption technique.
Algorithm:
Step 1: Input the keyword string and message string from user. Construct a 5x5 matrix from a
keyword, omitting duplicate letters, then fill remaining spaces with unused alphabet letters,
excluding one to fit the matrix.
Step 2: Remove spaces and non-alphabetic characters from the plaintext, convert to uppercase,
and replace any occurrences of 'J' with 'I'. Add an extra letter to ensure even length.
Step 3: Divide the adjusted plaintext into digraphs (pairs of letters), inserting a filler letter
(typically 'X') if a pair contains the same letter.
Step 4: Encrypt each digraph using the matrix:
- For same-row pairs, shift right.
- For same-column pairs, shift down.
- For rectangle-formed pairs, swap columns within the same row.
Step 5: Concatenate the encrypted pairs to form the ciphertext and output it.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++)
{
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;

}
}

3
int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
{
if (plain[i] != ' ')
plain[count++] = plain[i];
}
plain[count] = '\0';
return count;

}
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++)
{
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++)
{
if (dicty[key[k] - 97] == 2)
{
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5)
{

4
i++;
j = 0;

}
}
}
for (k = 0; k < 26; k++)
{
if (dicty[k] == 0)
{
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5)
{
i++;
j = 0;
}
}
}
}
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (keyT[i][j] == a)
{

5
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b)
{
arr[2] = i;
arr[3] = j;
}
}
}
}
int mod5(int a)
{
return (a % 5);
}
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
void encrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2)
{
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2])
{
str[i] = keyT[a[0]][mod5(a[1] + 1)];

6
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
else if (a[1] == a[3])
{
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else
{
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
int main()
{
char str[SIZE], key[SIZE];
scanf("%s",key);
scanf("%s",str);

7
encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);
return 0;
}

Output:
[root@localhost security lab]# gcc playfair.c
[root@localhost security lab]# ./a.out
**********Playfair Cipher************
Enter the length of the Key. 8
Enter the Key. monarchy
The table is as follows:
monar
chybd
12
efgik
lpqst
uvwxz
Enter the length length of plain text.(without spaces) 12
Enter the Plain text. moveforwardx
The replaced text(j with i)
moveforwardx
The cipher has to enter 0 bogus char.It is either 'x' or 'z'
Value of length is 12.
The final text is:
moveforwardx
The Cipher text is:
onufphnzrmbz
[root@localhost security lab]#

Result:
Thus the above program is executed successfully and the output is verified.

8
Exp. No: 1c
Hill Cipher
Date:

Aim:
To write a C program to encrypt a message using Hill cipher encryption technique.
Algorithm:
Step 1: Read an integer size matrix and a plaintext mesage string; pad the plaintext if
necessary to match the length required by the matrix dimensions.
Step 2: Convert the plaintext into numerical vectors based on alphabetical positions (A=0,
B=1, ..., Z=25).
Step 3: For each plaintext vector, multiply it by the matrix and apply modulo 26 to each
element of the resulting vector to keep values within the range of alphabets.
Step 4: Convert the numerical values of the resulting vectors back into letters to form the
encrypted blocks of text.
Step 5: Concatenate all encrypted blocks to display the final encrypted message.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getKeyMatrix(char *key, int keyMatrix[3][3])
{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key[k] - 'A') % 65;
k++;
}
}
}

9
void encrypt(int cipherMatrix[3][1], int keyMatrix[3][3], int
messageVector[3][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;
}
}
}
void HillCipher(char *message, char *key)
{
getKeyMatrix(key, keyMatrix);
int messageVector[3][1];
for (int i = 0; i < 3; i++)
messageVector[i][0] = (message[i] - 'A') % 65;
int cipherMatrix[3][1];
encrypt(cipherMatrix, keyMatrix, messageVector);
char cipherText[4];

cipherText[3] = '\0';
for (int i = 0; i < 3; i++)
cipherText[i] = (char)(cipherMatrix[i][0] + 'A');
printf("Ciphertext: %s", cipherText);

10
int main()
{
char message[100];
char key[100];
int keyMatrix[3][3]
scanf("%s",message);
scanf("%s",key);
HillCipher(message, key);
return 0;
}

Output:
[root@localhost security lab]# gcc hillcipher.c
[root@localhost security lab]# ./a.out
Enter plain text
PAY
15 0 24
Encrypted Cipher Text : R R L
Decrypted Cipher Text : P A Y
[root@localhost security lab]#

Result:
Thus the above program is executed successfully and the output is verified.

11
Exp. No: 1d
Vigenere Cipher
Date:

Aim:
To write a C program to encrypt a message using Vigenere cipher encryption
technique.
Algorithm:
Step 1: Input a key and a message string from the user.
Step 2: Determine the length of the key.
Step 3: For each character in the message, use the corresponding character in the key
(repeated if necessary) to calculate the new encrypted character.
Step 4: Convert and combine the ASCII values of the message and key characters, adjust by
subtracting the base ASCII value of 'a', and then apply modulo 26 to ensure the alphabet
wraps around. Convert back to a character.
Step 5: Display the encrypted message.
Program:
#include<stdio.h>
void main()
{
int I, kl, pl;
char p[pl], k[kl];
printf("Enter the length of the key stream. ");
scanf("%d",&kl);
printf("Enter the length of the plain text stream.(Without spaces)
");
scanf("%d",&pl); printf("\
nEnter the Key. "); for(i=-
1;i<kl;++i)
scanf("%c",&k[i]);
printf("\nEnter the Plain text. ");
for(i=-1;i<pl;++i)
scanf("%c",&p[i]);
int s[3][pl];

12
for(i=0;i<pl;++i)
{
if(65<=p[i] && p[i]<=91)
s[0][i]=p[i]%65;
else s[0]
[i]=p[i]%97;

}
for(i=0;i<pl;++i)
printf("%d ",s[0][i]);
int count=0;
while(count<pl)
{
for(i=0;i<kl;++i)
{
if(65<=k[i] && k[i]<=91)
s[1][count+i]=k[i]%65;
else s[1][count+i]=k[i]
%97;
}
count=count+kl;
}
printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[1][i]);
printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[2][i]);
printf("\n\nThe cipher text is: ");
char cipher[kl];
for(i=0;i<pl;++i)
{ s[2][i]=(s[0][i]+s[1][i])
%26;

13
cipher[i]=(char)(s[2][i]+65);
printf("%c ",cipher[i]);
}
}

Output:
[root@localhost security lab]# gcc vigenere2.c
[root@localhost security lab]# ./a.out
Enter the length of the key stream. 9
Enter the length of the plain text stream.(Without spaces) 27
Enter the Key. deceptive
Enter the Plain text. wearediscoveredsaveyourself
22 4 0 17 4 3 8 18 2 14 21 4 17 4 3 18 0 21 4 24 14 20 17 18 4 11 5
3 4 2 4 15 19 8 21 4 3 4 2 4 15 19 8 21 4 3 4 2 4 15 19 8 21 4
The cipher text is: Z I C V T W Q N G R Z G V T W A V Z H C Q Y G L M G J
[root@localhost security lab]#

Result:
Thus the above program is executed successfully and the output is verified.

14
Exp. No: 1e
Rail Fence- Row & Column Transformation
Date:

Aim:
To write a C program to encrypt a message using Rail Fence cipher encryption
technique.
Algorithm:
Step 1: Read a message string and a key integer from input.
Step 2: For each level up to the second-to-last, alternate characters are placed in the output
based on a pattern that skips characters by a certain number at each step.
Step 3: Adjust the number of characters skipped between placements, which changes
depending on the current level and its position in the cycle.
Step 4: For the last level, add every character from the input string that fits the regular
spacing pattern established for this level.
Step 5: Combine all characters placed from the previous steps into the output string and
display the encrypted message.
Program:
#include <stdio.h>
#include<string.h>

void railfence_encipher(int key, const char *plaintext, char


*ciphertext)
{
int line, i, skip, length = strlen(plaintext), j=0,k=0;
for(line = 0; line < key-1; line++)
{
skip = 2*(key - line - 1);
k=0;
for(i = line; i < length;)
{
ciphertext[j++] = plaintext[i];
if((line==0) || (k%2 == 0))
i+=skip;
else

i+=2*(key-1)-skip;

15
k++;
}
}
for(i=line; i<length; i+=2*(key-1))
ciphertext[j++] = plaintext[i];
ciphertext[j] = '\0';
}
int main()
{
char plaintext[100];
scanf("%s",plaintext);
char ciphertext[100];
int key;
scanf("%d",&key);
railfence_encipher(key, plaintext,ciphertext);
printf("Ciphertext: %s\n", ciphertext);
return 0;
}

Output:
[root@localhost security lab]# gcc railfence.c
[root@localhost security lab]# ./a.out
shootdowntheaircraft
Cipher text: sotonharrfhodwteicat

Result:
Thus the above program is executed successfully and the output is verified.

16
Exp. No: 1f
Affine Cipher
Date:

Aim:
To write a C program to implement Affine Cipher technique.
Algorithm:
Step 1: Get the input string from the user.
Step 2: Convert the input string to uppercase letters and store the numeric value in str.
Step 3: Prompt the user to enter alpha value between 1 and 25.
Step 4: Calculate the GCD value of alpha and 26 to be equal to 1. If not, repeat step 3.
Step 5: Prompt the user to enter beta value between 0 and 25.
Step 6: For each numeric value i in str, compute: (alpha x str[i] + beta) mod 26 and print the
corresponding cipher text.
Program:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int CalcGCD(int);
main()
{
int i,j,k,gcd,alpha,beta,numstr[100],numcipher[100];
char str[100],cipher[100];
printf("Enter a string\n");
gets(str);
//converting entered string to Capital letters
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;

17
}
else
{
str[j]=' ';
j++;
}
}
str[j]='\0';
printf("Entered string is : %s \n",str);
printf("Enter Alpha value and must be between 1 and 25 both
included\n");
scanf("%d",&alpha);
if(alpha<1 || alpha>25)
{
printf("Alpha should lie in between 1 and 25\nSorry Try again
!\n");
exit(0);
}
gcd=CalcGCD(alpha);
if(gcd!=1)

{
printf("gcd(alpha,26)=1 but \n gcd(%d,26)=%d\nSorry Try again
!\n",alpha,gcd);
exit(0);
}
printf("Enter Beta value and must be between 0 and 25 both
included\n");
scanf("%d",&beta);

if(beta<0 || beta>25)
{
printf("Beta value should lie between 0 and 25\nSorry Try again
!\n");
exit(0);
}

18
//Storing string in terms of ascii and to restore spaces, used -20
for(i=0;i<strlen(str);i++)

{
if(str[i]!=' ')
numstr[i]=str[i]-'A';
else

numstr[i]=-20;
}
//Ciphering Process: If numcipher is more than 25 .We need to
convert and ensure that lie
//in between 0 and 25.(indicating Alphabets)
printf("Affine Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
if(numstr[i]!=-20)
{
numcipher[i]=((alpha*numstr[i])+beta)%26; printf("%c",(numcipher[i]
+'A'));
}
else
{
printf(" ");
}
}
printf("\n");
}
int CalcGCD(int alpha)
{
int x;
int temp1=alpha;
int temp2=26;
while(temp2!=0)
{

19
x=temp2;
temp2=temp1%temp2;
temp1=x;
}
return(temp1);
}

Output:
[root@localhost Downloads]# ./a.out
Enter a string
INDIA IS MY COUNTRY
Entered string is : INDIA IS MY COUNTRY
Enter Alpha value and must be between 1 and 25 both included
5
Enter Beta value and must be between 0 and 25 both included
9
Affine Cipher text is
XWYXJ XV RZ TBFWAQZ

Result:
Thus the above program is executed successfully and the output is verified.

20
Exp. No: 2a
DES Algorithm
Date:

Aim:
To write a C program to implement DES symmetric cipher technique.
Algorithm:
Step 1: Initialize the value of the key and the plaintext.
Step 2: Allocate memory for plaintext and key.
Step 3: Print the hexadecimal value of the plaintext
Step 4: Print the hexadecimal value of the key
Step 5: Prepare the key for use with DES_cfb64_encrypt
Step 6: Set the parity of the key passed to odd using DES_set_odd_parity( ).
Step 7: Check the passed key is weak by using DES_set_key_checked( ).
Step 8: Encrypt the message in Cipher Feedback Mode using DES_cfb64_encrypt( ).
Step 9: Print the obtained cipher text message.
Step 10: Decrypt the ciphert text using DES_cfb64_decrypt( ).
Step 11: Print the obtained plain text message
Program:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <openssl/des.h>
char *Encrypt( char *Key, char *Msg, int size)
{
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = (char *)malloc(size);
/* Prepare the key for use with DES_cfb64_encrypt */
memcpy( Key2, Key,8);

21
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );

/* Encryption occurs here */


DES_cfb64_encrypt( ( unsigned char * ) Msg, ( unsigned char * )
Res, size,&schedule, &Key2, &n, DES_ENCRYPT );
return (Res);
}
char *Decrypt( char *Key, char *Msg, int size)
{
static char* Res;
int n=0;
DES_cblock Key2;
DES _key_schedule schedule;
Res = (char *)malloc(size);
/* Prepare the key for use with DES_cfb64_encrypt */
memcpy( Key2, Key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule);
/* Decryption occurs here */
DES_cfb64_encrypt( ( unsigned char * ) Msg, ( unsigned char * )
Res,size,&schedule, &Key2, &n, DES_DECRYPT );
return (Res);
}
int main()
{
char key[]="BENEDICT";
char plaintext[]="ENJOYCNS";
char *decrypted;
char *encrypted;
encrypted=malloc(sizeof(plaintext));
decrypted=malloc(sizeof(plaintext)); printf("\
nPlaintext\t\t\t : %s",plaintext);
printf("\nPlaintext in Hex Code \t\t :%X",plaintext);

22
printf("\nKey in Hex Code \t\t :%X",key);

memcpy(encrypted,Encrypt(key,plaintext,sizeof(plaintext)),sizeof(pla
intext));
printf("\nEncrypted Text\t\t\t : %s",encrypted);

memcpy(decrypted,Decrypt(key,encrypted,sizeof(plaintext)),sizeof(pla
intext));
printf("\nDecrypted Text\t\t\t : %s",decrypted); printf("\
nDecrypted Text in Hex Code \t : %X\n",decrypted); return
(0);
}

Output:
[root@localhost security lab]# gcc desopen.c -lssl-lcrypto
[root@localhost security lab]# ./a.out
Plaintext : ENJOYCNS
Plaintext in Hex Code : BFD28266
Key in Hex Code : BFD2826F
Encrypted Text : {SS
Decrypted Text : ENJOYCNS
Decrypted Text in Hex Code : 848D030

Result:
Thus the above program is executed successfully and the output is verified.

23
Exp. No: 2b
RSA Algorithm
Date:

Aim:
To write a C program to encrypt a message using RSA Algorithm.
Algorithm:
Step 1: Select two large prime numbers p and q
Step 2: Compute n=pxq
Step 3: Choose system modulus: Ø(n)=(p-1)x(q-1)
Step 4: Select a random encryption key e such that gcd(e,Ø(n)=1
Step 5: Decrypt by computing d=1 mod Ø(n)
Step 6: Print the public key{e,n}
Step 7: Print the private key{d,n}
Program:
#include <stdio.h>
#include <math.h>
double gcd(long a, long h)
{
double temp;
while (1)
{
temp = fmod(a, h);
if (temp == 0)
return h;
a = h;
h = temp;
}
}
int main()
{
long p, q, n, e, phi, msg, c;
printf("p: ");

24
scanf("%ld", &p);
printf("q: ");
scanf("%ld", &q);
n = p * q;
printf("e: ");
scanf("%ld", &e);
phi = (p - 1) * (q - 1);
while (e < phi)
{
if (gcd(e, phi) == 1)
break;
else
e++;
}
printf("Enter the message: ");
scanf("%ld", &msg);
c = pow(msg, e);
c = fmod(c, n);
printf("Encrypted data = %ld\n", c);
return 0;
}

Output:
[root@localhost security lab]# gcc rsa.c -lm
[root@localhost security lab]# ./a.out
P: 7
Q: 17
E: 12
ENTER THE MESSAGE: hello
ENCRYPTED DATA: cc

Result:
Thus the above program is executed successfully and the output is verified.

25
Exp. No: 2c
Diffie-Hellman Algorithm
Date:

Aim:
To write a C program to encrypt a message using Diffie-Hellman Algorithm.
Algorithm:
Step 1: Read the modulus P and the base G, then read private keys a and b for two users.
Step 2: Calculate the public key for the first user by raising G to the power of a modulo P.
Step 3: Calculate the public key for the second user by raising G to the power of b modulo
P.
Step 4: Compute the shared secret key for the first user by raising the second user's public
key to the power of a modulo P, and vice versa for the second user.
Step 5: Verify if both computed shared secret keys are equal, and print the shared key;
otherwise, indicate a potential attack.
Program:
#include <math.h>
#include <stdio.h>

long long int power(long long int a, long long int b,long long int P)
{
if (b == 1)
return a;

else

return (((long long int)pow(a, b)) % P);


}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
printf("P: ");

scanf("%lld",&P);
printf("G: ");
scanf("%lld",&G);
printf("The private key of a: ");
scanf("%lld",&a);

26
x = power(G, a, P);
printf("The private key of b: ");
scanf("%lld",&b);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
ka==kb?printf("Encryption successful with key: %lld”,kb)
:printf("Attack has happened");
return 0;
}

Output:
[root@localhost security lab]# gcc diffiehellman.c -lm
[root@localhost security lab]# ./a.out
P:23
G:2
The private key of a: 5
The private key of b: 7
Encryption successful with key 4

Result:
Thus the above program is executed successfully and the output is verified.

27
Exp. No: 2d
MD5 Algorithm
Date:

Aim:
To write a C program to implement MD-5 hash technique.
Algorithm:
Step 1: Get the value of string from command line argument.
Step 2: Initialize the digest table.
Step 3: Use EVP_get_digestbyname function and pass on the argument MD5
Step 4: Initialize digest context.
Step 5: Use digestUpdate() function to hash bytes of data at d into the digest contexts
Step 6: Use EVP_DigestFinal_ex() function to retrieve the digest value from ctx and places in
md.
Step 7: Cleanup the digest context ctx.
Step 8: Print the value of digest computed.

Program:
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
void main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char input[80];
unsigned char output[EVP_MAX_MD_SIZE];
int output_len, i;
strcpy(input,argv[1]);
/* Initialize digests table */
OpenSSL_add_all_digests();
/* You can pass the name of another algorithm supported by your
version of OpenSSL */

28
/* For instance, MD2, MD4, SHA1, RIPEMD160 etc. Check the OpenSSL
documentation for details */
md = EVP_get_digestbyname("MD5");
if(!md)

{
printf("Unable to init MD5 digest\n");
exit(1);

}
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, input, strlen(input));
/* to add more data to hash, place additional calls to
EVP_DigestUpdate here */
EVP_DigestFinal_ex(&mdctx, output, &output_len);
EVP_MD_CTX_cleanup(&mdctx);
/* Now output contains the hash value, output_len contains length of
output, which is 128 bit or 16 byte in case of MD5 */
printf("Digest is: ");
for(i = 0; i < output_len; i++)
printf("%02x", output[i]);
printf("\n");
}

Output:
[root@localhost security lab]# gcc md5final.c -lssl -lcrypto
[root@localhost security lab]# ./a.out REC
Digest is: d6d269952320c4fb5e50f278c94a098c
[root@localhost security lab]# ./a.out IIT
Digest is: 1ce322ec4920fa4d0f5673f226fa8988

Result:
Thus the above program is executed successfully and the output is verified.

29
Exp. No: 2e
SHA-1 Algorithm
Date:

Aim:
To write a C program to implement SHA-1 hash technique.
Algorithm:
Step 1: Get the input string from command line arguments.
Step 2: Check if the number of arguments is not equal to 2. If so print error and return.
Step 3: Generate hash string for argv[1] by passing it to sha1 function.
Step 4: The value returned is stored in temp variable.
Step 5: Loop through the contents of temp and put into buf variable.
Step 6: Print the contents of buf variable.
Program:
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
int main(int argn, char *argv[])
{
int i = 0;
unsigned char temp[SHA_DIGEST_LENGTH];
char buf[SHA_DIGEST_LENGTH*2];

if ( argn != 2 )
{
printf("Usage: %s string\n", argv[0]);
return -1;
}
memset(buf, 0x0, SHA_DIGEST_LENGTH*2);
memset(temp, 0x0, SHA_DIGEST_LENGTH);
SHA1((unsigned char *)argv[1], strlen(argv[1]), temp);
for (i=0; i < SHA_DIGEST_LENGTH; i++)
sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
printf("SHA1 of %s is %s\n", argv[1], buf);

30
return 0;
}

Output:
[root@localhost security lab]# gcc sha1.c -lssl -lcrypto
[root@localhost security lab]# ./a.out REC
SHA1 of REC is 09ebb92a1478021f08e37a2ffe4ce10e8ced419f

Result:
Thus the above program is executed successfully and the output is verified.

31
Exp. No: 3
Digital Signature Algorithm
Date:

Aim:
To write a C program to implement digital signature scheme.
Algorithm:
Step 1: Generate private key and public key using RSA algorithm.
Step 2: Enable all algorithms using OpenSSL_add_all_algorithms() function.
Step 3: Allocate empty PKEY structure to put the private key.
Step 4: Read the private key and store in PEM format.
Step 5: Check the read RSA private key is valid or not.
Step 6: If valid print the details of the key.
Program:
#include <stdio.h>
#include <string.h>
#include <openssl/x509v3.h>
#include <openssl/objects.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
int main() {
EVP_PKEY *privkey;
FILE *fp;
RSA *rsakey;
/* *
* Next function is essential to enable openssl functions *
*/
OpenSSL_add_all_algorithms();
privkey = EVP_PKEY_new();
fp = fopen ("test-key.pem", "r");
PEM_read_PrivateKey( fp, &privkey, NULL, NULL);
fclose(fp);
rsakey = EVP_PKEY_get1_RSA(privkey);

32
if(RSA_check_key(rsakey)) {
printf("RSA key is valid.\n");
}
else {
printf("Error validating RSA key.\n");
}
RSA_print_fp(stdout, rsakey, 3);
PEM_write_PrivateKey(stdout,privkey,NULL,NULL,0,0,NULL);
exit(0);
}

Output
[root@localhost security lab]# openssl genrsa -out test-key.pem 512
Generating RSA private key, 512 bit long modulus
........++++++++++++
.....++++++++++++
e is 65537 (0x10001)
[root@localhost security lab]# gcc digitalsign.c -lssl -lcrypto
[root@localhost security lab]# ./a.out
RSA key is valid.
Private-Key: (512 bit)
modulus:
00:d6:03:7a:02:19:5b:70:fb:9d:a9:f4:cc:6f:01:
35:52:48:84:b0:aa:b1:3c:5c:ab:1d:34:95:3d:bd:
fa:ca:64:ed:67:89:a2:33:83:83:2f:1f:c1:2e:9e:
d4:13:cc:df:9e:5c:1d:34:f5:60:cf:53:cd:49:01:
95:11:55:17:ef
publicExponent: 65537 (0x10001)
privateExponent:
00:af:bc:25:18:ca:27:ab:2c:02:38:48:1b:02:df:
d4:20:20:0a:4d:63:ac:ab:eb:50:5b:68:0d:50:a8:
ca:e2:1b:e3:b8:aa:41:aa:7c:5a:3e:d5:1d:82:84:

33
4b:d6:ea:a3:d9:0d:18:7a:d1:4d:3d:7c:65:63:18:
2e:fd:8b:eb:d1
prime1:
00:f1:89:83:42:b2:38:e6:4c:f7:1f:a7:96:76:f4:
6b:ba:33:f6:b3:ac:7f:c4:cc:28:90:78:d7:ac:76:
1b:09:b7
prime2:
00:e2:d4:0f:1a:fc:63:a5:48:92:3e:be:9c:2d:71:
17:f5:d2:aa:7a:26:58:b7:03:ab:8c:bb:da:6b:09:
3e:43:89
exponent1:
3f:3c:67:57:20:dd:f0:bd:99:bd:79:dc:d4:cb:ed:
20:54:d6:73:f7:e7:83:98:87:ce:3b:35:0b:fb:e7:
dc:45
exponent2:
1e:8a:5e:de:4b:4d:3f:5b:de:15:04:a5:12:99:3f:
98:a1:9c:c2:85:97:3c:4d:0a:34:10:b6:ff:e2:66:
40
b7:c1
coefficient:
76:a4:63:4d:e8:af:b3:b1:ac:81:15:13:6f:10:eb:
82:f9:c6:6a:b0:c6:b5:39:2e:9b:35:0a:8d:c7:38:
7d:d1
-----BEGIN PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA1gN6AhlbcPudqfT
M
bwE1UkiEsKqxPFyrHTSVPb36ymTtZ4miM4ODLx/BLp7UE8zfnlwdNPVgz1PNSQGV
EVUX7wIDAQABAkEAr7wlGMonqywCOEgbAt/UICAKTWOsq+tQW2gNUKjK4hvju
KpB
qnxaPtUdgoRL1uqj2Q0YetFNPXxlYxgu/Yvr0QIhAPGJg0KyOOZM9x+nlnb0a7oz

34
9rOsf8TMKJB416x2Gwm3AiEA4tQPGvxjpUiSPr6cLXEX9dKqeiZYtwOrjLvaawk+
Q4kCID88Z1cg3fC9mb153NTL7SBU1nP354OYh847NQv759xFAiAeil7eS00/W94V
BKUSmT+YoZzChZc8TQo0ELb/4ma3wQIgdqRjTeivs7GsgRUTbxDrgvnGarDGtTku
mzUKjcc4fdE=
-----END PRIVATE KEY-----

Result:
Thus the above program is executed successfully and the output is verified.

35
Exp. No: 4
Honey Pot Setup
Date:

Aim:
To setup a honeypot using honeyd in Linux machine and test it from windows
machine.

Algorithm:
Step 1: Install honeyd on one of the system.
Step 2: Create honeyd configuration file.
Step 3: Launch honeyd with options -d and -f after configuration files are created.
Step 4: Ping from windows machine to the honeyd machine with it's IP address.
Step 5: After honeyd successful deployment, check required port of honeyd machine are
open Step 6: Use nmap to scan the open ports of honeyd machine.
Step 7: If the required ports are open, the honeyd is functioning correctly.

Output:
[root@localhost security lab]# dnf install honeyd
[root@localhost security lab]# cd /etc/
[root@localhost security lab]# vi honeyd.conf
create default
set default default tcp action block
set default default udp action block
set default default icmp action block
create windows
set windows personality "Microsoft Windows XP Professional SP1"
set windows default tcp action reset
add windows tcp port 135 open
add windows tcp port 139 open
add windows tcp port 445 open
set windows ethernet "00:00:24:ab:8c:12"

36
dhcp windows on eth0
[root@localhost security lab]# honeyd -d -f honeyd.conf
[root@localhost security lab]# nmap -p 135,139,445,1337 192.168.99.135
Starting Nmap 5.00 ( http://nmap.org ) at 2011-05-06 13:13 EDT
Interesting ports on someone (172.20.73.77):
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
1337/tcp closed waste
MAC Address: 00:00:24:26:C4:ED (Connect AS)
Nmap done: 1 IP address (1 host up) scanned in 0.37 seconds

Result:
Thus the above program is executed successfully and the output is verified.

37
Exp. No: 5
Wireless Audit
Date:

Aim:
To perform wireless audit on Access Point and decrypt WPA keys using aircrack-ng
tool in Kalilinux OS.
Algorithm:
Step 1: Check the current wireless interface with iwconfig command.
Step 2: Get the channel number, MAC address and ESSID with iwlist command.
Step 3: Start the wireless interface in monitor mode on specific AP channel with airmon-ng.
Step 4: If processes are interfering with airmon-ng then kill those process.
Step 5: Again start the wireless interface in monitor mode on specific AP channel with
airmon-ng.
Step 6: Start airodump-ng to capture Initialization Vectors(IVs).
Step 7: Capture IVs for atleast 5 to 10 minutes and then press Ctrl + C to stop the operation.
Step 8: List the files to see the captured files
Step 9: Run aircrack-ng to crack key using the IVs collected and using the dictionary file
rockyou.txt
Step 10: If the passphrase is found in dictionary then Key Found message displayed; else
print Key Not Found.
Output:
root@kali:~#iwconfig
eth0 no wireless extensions.
wlan0 IEEE 802.11bgn ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=20 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
lo no wireless extensions.
root@kali:~# iwlist wlan0 scanning
wlan0 Scan completed :

38
Cell 01 - Address: 14:F6:5A:F4:57:22
Channel:6
Frequency:2.437 GHz (Channel 6)
Quality=70/70 Signal level=-27 dBm
Encryption key:on
ESSID:"BENEDICT"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s
Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s
36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Master
Extra:tsf=00000000425b0a37
Extra: Last beacon: 548ms ago
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : CCMP TKIP
Authentication Suites (1) : PSK
root@kali:~# airmon-ng start wlan0
Found 2 processes that could cause trouble.
If airodump-ng, aireplay-ng or airtun-ng stops working
after a short period of time, you may want to kill (some of)
them!
PID Name
1148 NetworkManager
1324 wpa_supplicant
PHY Interface Driver Chipset
phy0 wlan0 ath9k_htc Atheros Communications, Inc. AR9271
802.11n
Newly created monitor mode interface wlan0mon is *NOT* in monitor mode.
Removing non-monitor wlan0mon interface...
WARNING: unable to start monitor mode, please run "airmon-ng check kill"

39
root@kali:~# airmon-ng check kill
Killing these processes:
PID Name
1324 wpa_supplicant
root@kali:~# airmon-ng start wlan0
PHY Interface Driver Chipset
phy0 wlan0 ath9k_htc Atheros Communications, Inc. AR9271
802.11n
(mac80211 monitor mode vif enabled for [phy0]wlan0 on
[phy0]wlan0mon) (mac80211 station mode vif disabled for [phy0]wlan0)
root@kali:~# airodump-ng -w atheros -c 6 --bssid 14:F6:5A:F4:57:22 wlan0mon
[CH 6 ][ Elapsed: 5 mins ][ 2016-10-05 01:35 ][ WPA handshake: 14:F6:5A:F4:57:22
BSSID PWR RXQ Beacons #Data, #/s CH MB ENC CIPHER AUTH E
14:F6:5A:F4:57:22 -31 10
0
3104 10036 0 6 54e. WPA CCMP PSK B
BSSID STATION PW
R
Rate Lost Frames Probe
14:F6:5A:F4:57:22 70:05:14:A3:7E:3E -32 2e- 0 0 10836
root@kali:~# ls -l
total 10348
-rw-r--r-- 1 root root 10580359 Oct 5 01:35 atheros-01.cap
-rw-r--r-- 1 root root 481 Oct 5 01:35
atheros-01.csv
-rw-r--r-- 1 root root 598 Oct 5 01:35 atheros-01.kismet.csv
-rw-r--r-- 1 root root 2796 Oct 5 01:35 atheros-01.kismet.netxml
root@kali:~# aircrack-ng -a 2 atheros-01.cap -w /usr/share/wordlists/rockyou.txt
[00:00:52] 84564 keys tested (1648.11 k/s)
KEY FOUND! [ rec12345 ]

40
Master Key : CA 53 9B 5C 23 16 70 E4 84 53 16 9E FB 14 77 49 A9
7AA0 2D 9F BB 2B C3 8D 26 D2 33 54 3D 3A 43
Transient Key : F5 F4 BA AF 57 6F 87 04 58 02 ED 18 62 37 8A 53
38 86 F1 A2 CA 0D 4A 8D D6 EC ED 0D 6C 1D C1
AF 81 58 81 C2 5D 58 7F FA DE 13 34 D6 A2 AE FE
05 F6 53 B8 CAA0 70 EC 02 1B EA 5F 7A DA 7A
EC 7D
EAPOL HMAC 0A 12 4C 3D ED BD EE C0 2B C9 5A E3 C1 65 A8 5C

Result:
Thus the above program is executed successfully and the output is verified.

41
Exp. No: 6
SNORT IDS
Date:

Aim:
To demonstrate Intrusion Detection System(IDS) using snort tool.
Algorithm:
Step 1: Download and extract the latest version of snort
Step 2: Install development packages - libpcap and pcre.
Step 3: Install snort
Step 4: Verify the installation is correct.
Step 5: Create the configuration file, rule file and log file directory
Step 6: Create snort.conf and icmp.rules files
Step 7: Execute snort from the command line
Step 8: Ping to yahoo website from another terminal
Step 9: Watch the alert messages in the log files
Output:
[root@localhost security lab]# cd /usr/src
[root@localhost security lab]# wget
https://www.snort.org/downloads/snort/snort-2.9.8.3.tar.gz
[root@localhost security lab]# tar xvzf snort-2.9.8.3.tar.gz
[root@localhost security lab]# yum install libpcap* pcre* -y
[root@localhost security lab]# cdsnort-2.9.8.3
[root@localhost security lab]# . /configure
[root@localhost security lab]# make
[root@localhost security lab]# make install
[root@localhost security lab]# snort --version
,,_ -*> Snort! <*-
o" )~ Version 2.9.8.2 GRE (Build 335)
'''' By Martin Roesch & The Snort Team:
http://www.snort.org/contact#team Copyright (C) 2014-2015
Cisco and/or its affiliates. All rights reserved. Copyright (C)

42
1998-2013 Sourcefire, Inc., et al.
Using libpcap version 1.7.3
Using PCRE version: 8.38 2015-11-23
Using ZLIB version: 1.2.8
[root@localhost security lab]# mkdir /etc/snort
[root@localhost security lab]# mkdir /etc/snort/rules
[root@localhost security lab]# mkdir /var/log/snort
[root@localhost security lab]# vi /etc/snort/snort.conf
add this line- include /etc/snort/rules/icmp.rules
[root@localhost security lab]# vi /etc/snort/rules/icmp.rules
alert icmp any any -> any any (msg:"ICMP Packet"; sid:477; rev:3;)
[root@localhost security lab]# snort -i p4p1 -c /etc/snort/snort.conf -l /var/log/snort/
Another terminal :
[root@localhost security lab]# ping www.yahoo.com
Ctrl + C
[root@localhost security lab]# vi /var/log/snort/alert
[**] [1:477:3] ICMP Packet
[**] [Priority: 0]
10/06-15:03:11.187877 192.168.43.148 ->
106.10.138.240 ICMP TTL:64 TOS:0x0 ID:45855
IpLen:20 DgmLen:84 DF Type:8 Code:0 ID:14680
Seq:64 ECHO
[**] [1:477:3] ICMP Packet [**]
[Priority: 0]
10/06-15:03:11.341739 106.10.138.240 ->
192.168.43.148 ICMP TTL:52 TOS:0x38 ID:2493
IpLen:20 DgmLen:84 Type:0 Code:0 ID:14680 Seq:64
ECHO REPLY
[**] [1:477:3] ICMP Packet
[**] [Priority: 0]

43
10/06-15:03:12.189727 192.168.43.148 ->
106.10.138.240 ICMP TTL:64 TOS:0x0 ID:46238
IpLen:20 DgmLen:84 DF Type:8 Code:0 ID:14680
Seq:65 ECHO
[**] [1:477:3] ICMP Packet [**]
[Priority: 0]
10/06-15:03:12.340881 106.10.138.240 ->
192.168.43.148 ICMP TTL:52 TOS:0x38 ID:7545
IpLen:20 DgmLen:84 Type:0 Code:0 ID:14680 Seq:65
ECHO REPLY

Result:
Thus the above program is executed successfully and the output is verified.

44

You might also like