0% found this document useful (0 votes)
9 views83 pages

DWDM & IS Lab Manual - III

The document contains multiple C program implementations for various encryption algorithms including Caesar cipher, Substitution cipher, Hill cipher, and DES algorithm. Each section provides code snippets along with sample outputs demonstrating the encryption and decryption processes. The document serves as a practical guide for understanding and implementing these cryptographic techniques.

Uploaded by

Kalai
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)
9 views83 pages

DWDM & IS Lab Manual - III

The document contains multiple C program implementations for various encryption algorithms including Caesar cipher, Substitution cipher, Hill cipher, and DES algorithm. Each section provides code snippets along with sample outputs demonstrating the encryption and decryption processes. The document serves as a practical guide for understanding and implementing these cryptographic techniques.

Uploaded by

Kalai
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/ 83

WWW.VIDYARTHIPLUS.

COM

INFORMATION SECURITY LAB

1.Writea C program Implementation of Ceasar cipher

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
void main(){
int temp,i,j,f,n,t[1][50];
char a[1][26]={"abcdefghijklmnopqrstuvwxyz"},p[1][50],k[1][50],c[1][50];
printf("Encryption Process:\n");
printf("Enter the text to be encrypted:");
scanf("%s",k[0]);
n=strlen(k[0]);
//strcpy(k[0],strlwr(k[0]));
printf("Enter the shift position:");
scanf("%d",&f);
f=f%26;
for(i=0;i<n;i++)
for(j=0;j<26;j++)
if(k[0][i]==a[0][j])
{ t[0][i]=(j+f)%26;
temp=t[0][i];
c[0][i]=a[0][temp];
break;
}
c[0][i]='\0';
printf("The cipher text is:");
puts(c[0]);
printf("\nDecryption Process:\n");
for(i=0;i<n;i++){
temp=(t[0][i]-f+26)%26;
p[0][i]=a[0][temp];
}
printf("The plain text is:");
puts(p[0]);
}
OUTPUT:

Encryption Process:
Enter the text to be encrypted: hello
Enter the shift position: 3
The cipher text is: khoor

Decryption Process:
The plain text is: hello

DWDM/IS LAB Page 1


WWW.VIDYARTHIPLUS.COM

2. Writea C program Implementation of Substitution cipher


#include<stdio.h>

int main()
{
char *message,*emessage,*dmessage;
int i,j=0,k,key,temp;
clrscr();

printf("\nEnter the key\n");


scanf("%d",&key);
key=key%26;
printf("\nEnter message\n");
fflush(stdin);
gets(message);

for(i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);

for(i=0;message[i]!=NULL;i++)
{
//printf("%c ",message[i]);
if(message[i]==' ')
emessage[j++]=message[i];

else
{

if(message[i]>=48 && message[i]<=57)


{
temp=message[i]+key;
if(temp>57)
emessage[j++]=48+(temp-58);
else
emessage[j++]=temp;
}
else
{
if(message[i]>=97 && message[i]<=123)
{
temp=message[i]+key;
if(temp>122)
emessage[j++]=97+(temp-123);
else
emessage[j++]=temp;
}
else

DWDM/IS LAB Page 2


WWW.VIDYARTHIPLUS.COM

emessage[j++]=message[i];
}

// printf("%c ",emessage[j]);
}

emessage[j]='\0';
printf("\n\n\nEncrypted message is\n\n");
for(i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
// printf("\n end");

for(i=0,j=0;emessage[i]!=NULL;i++)
{

if(emessage[i]==' ')
dmessage[j++]=emessage[i];
else
{
if(emessage[i]>=48 && emessage[i]<=57)
{
temp=emessage[i]-key;
if(temp<48)
dmessage[j++]=58-(48-temp);
else
dmessage[j++]=temp;
}
else
{
if(emessage[i]>=97 && emessage[i]<=123)
{
temp=emessage[i]-key;
if(temp<97)
dmessage[j++]=123-(97-temp);
else
dmessage[j++]=temp;
}
else
dmessage[j++]=emessage[i];
}
}
}
dmessage[j]='\0';
printf("\n\n\nRetrieved message is\n\n");
for(i=0;dmessage[i]!=NULL;i++)
printf("%c",dmessage[i]);

DWDM/IS LAB Page 3


WWW.VIDYARTHIPLUS.COM

getch();
return(0);
}

OUTPUT:

Enter the key


3
Enter message
Hello

Encrypted message is
Khoor

Retrived message is
Hello

DWDM/IS LAB Page 4


WWW.VIDYARTHIPLUS.COM

3. Write a C program Implementation of Hill cipher

#include<stdio.h>

#include<string.h>

main()

char
ch,pt[30],ct[30],dg[25][2],ip[30]={'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'};

int len,det,i,s,j,k=0,h,u,v,r,m=0,n,ptm[25][2],ctm[25][2],ptm1[25][2],km[2][2];

printf("enter plain text\n");

scanf("%s",pt);

h=strlen(pt);

len=h;

if((len%2)==1)

len++;

for(i=0;i<len/2;i++)

for(j=0;j<2;j++)

dg[i][j]=pt[k];

k++;

if(!(dg[h/2][1]>=97 && dg[h/2][1]<=122))

dg[h/2][1]=dg[h/2][0];

DWDM/IS LAB Page 5


WWW.VIDYARTHIPLUS.COM

for(i=0;i<len/2;i++)

printf("%c%c\t",dg[i][0],dg[i][1]);

for(i=0;i<len/2;i++)

for(j=0;j<2;j++)

for(k=0;k<26;k++)

if(dg[i][j]==ip[k])

ptm[i][j]=k;

printf("\n");

for(i=0;i<len/2;i++)

printf("(%d,%d)\t",ptm[i][0],ptm[i][1]);

x:

printf("\nEnter key matrix of order 2*2\n");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

scanf("%d",&km[i][j]);

det=km[0][0]*km[1][1]-km[1][0]*km[0][1];

if(det==0)

DWDM/IS LAB Page 6


WWW.VIDYARTHIPLUS.COM

printf("\n entered key matrix is invalid");

goto x;

printf("\nEntered key matrix is\n");

for(i=0;i<2;i++)

for(j=0;j<2;j++)

printf("%d\t",km[i][j]);

printf("\n");

for(i=0;i<len/2;i++)

ptm1[i][0]=km[0][0]*ptm[i][0]+km[0][1]*ptm[i][1];

ptm1[i][1]=km[1][0]*ptm[i][0]+km[1][1]*ptm[i][1];

for(i=0;i<len/2;i++)

for(j=0;j<2;j++)

for(k=0;k<26;k++)

if(ptm1[i][j]%26==k)

DWDM/IS LAB Page 7


WWW.VIDYARTHIPLUS.COM

ct[m]=ip[k];

m++;

printf("\ncipher text generated is\n");

for(i=0;i<len;i++)

printf("%c",ct[i]) ;

//decryption

r=1;

do

s=(11*r)%26;

r++;

}while(s!=1);

r--;

u=km[0][0];

v=km[1][1];

km[0][0]=v;

km[0][1]=-km[0][1];

km[1][0]=-km[1][0];

km[1][1]=u;

DWDM/IS LAB Page 8


WWW.VIDYARTHIPLUS.COM

for(i=0;i<2;i++)

for(j=0;j<2;j++)

km[i][j]=r*km[i][j];

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

if(km[i][j]<0)

km[i][j]=km[i][j]+26;

for(i=0;i<len/2;i++)

ctm[i][0]=km[0][0]*ptm1[i][0]+km[0][1]*ptm1[i][1];

ctm[i][1]=km[1][0]*ptm1[i][0]+km[1][1]*ptm1[i][1];

printf("\n plain text generated back is\n");

for(i=0;i<len/2;i++)

for(j=0;j<2;j++)

for(k=0;k<26;k++)

if(ctm[i][j]==k)

pt[m]=ip[k];

m++;

for(i=0;i<len;i++)

printf("%c",pt[i]) ;

DWDM/IS LAB Page 9


WWW.VIDYARTHIPLUS.COM

printf("\n");

return 0;

OUTPUT:

enter plain text

welcometolab

we lc om et ol ab

(22,4) (11,2) (14,12) (4,19) (14,11) (0,1)

Enter key matrix of order 2*2

3211

Entered key matrix is

3 2

1 1

cipher text generated is

walnoayxmzcb

plain text generated back is

welcometolab

DWDM/IS LAB Page 10


WWW.VIDYARTHIPLUS.COM

4. Write a program Implementation of DES algorithm.

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

void hex_to_bin(char *,char *);


char* bin_to_hex(char *);
void permutation(char *,char *);
void make_half(char *,char *,char *);
void single_shift(char *,char *);
void double_shift(char *,char *);
void make_key(char *,char *,char *);
void permutation_32(char *,char *);
void permutation_48(char *,char *);
void permutation_64(char *,char *,char *);

void des_round(char *,char *,char *,char *,char *,char *,char *);


void des_round_decry(char *,char *,char *,char *,char *,char *,char *);
void copy(char *,char *);
void permut_48(char *,char *);
void xor(char *,char *,char *);
void xor_32(char *,char *,char *);
void common_permutation(char *,char *);
void hex_to_plain(char *,char *,int);
int switch_case(char );
char SB[32];

char *bin[]={
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"

DWDM/IS LAB Page 11


WWW.VIDYARTHIPLUS.COM

};

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

int PC1[8][7]={
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};

int PC2[8][6]={
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};

int IP[8][8]={
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};

int E_bit[8][6]={
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1};

char *look_up[]={
"00",

DWDM/IS LAB Page 12


WWW.VIDYARTHIPLUS.COM

"01",
"10",
"11"};

int sb_permutation[8][4]={
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25};

int s1[4][16]={
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13};

int s2[4][16]={
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9};

int s3[4][16]={
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12};

int s4[4][16]={
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14};

int s5[4][16]={
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3};

int s6[4][16]={
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,12,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,

DWDM/IS LAB Page 13


WWW.VIDYARTHIPLUS.COM

4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13};

int s7[4][16]={
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12};

int s8[4][16]={
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};

int ip_inverse[8][8]={
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};

void main()
{
char input[200],initial_hex[400];
int i,j,k=0,len,r,x,m,temp;
int d,e,f;
char hex_arr[25][16];

char input_hex[16],input_bin[64];
char key_hex[16]={'1','3','3','4','5','7','7','9','9','B','B','C','D','F','F','1'};
char key_bin[64],key_PC1[56];
char
ch,*decryption,*encryption,encryption_final[400],decryption_final_hex[400],decryption_fina
l_plain[200];
char encrypted[64],decrypted[64],encry_permut[64],decry_permut[64];
int length,p=-1,q=-1;

char C0[28],D0[28],
C1[28],D1[28],CD1[56],
C2[28],D2[28],CD2[56],
C3[28],D3[28],CD3[56],
C4[28],D4[28],CD4[56],
C5[28],D5[28],CD5[56],
C6[28],D6[28],CD6[56],

DWDM/IS LAB Page 14


WWW.VIDYARTHIPLUS.COM

C7[28],D7[28],CD7[56],
C8[28],D8[28],CD8[56],
C9[28],D9[28],CD9[56],
C10[28],D10[28],CD10[56],
C11[28],D11[28],CD11[56],
C12[28],D12[28],CD12[56],
C13[28],D13[28],CD13[56],
C14[28],D14[28],CD14[56],
C15[28],D15[28],CD15[56],
C16[28],D16[28],CD16[56];

char L0[32],R0[32],ER0[48];
char K1[48],L1[32],R1[32],ER1[48],F1[48],
K2[48],L2[32],R2[32],ER2[48],F2[48],
K3[48],L3[32],R3[32],ER3[48],F3[48],
K4[48],L4[32],R4[32],ER4[48],F4[48],
K5[48],L5[32],R5[32],ER5[48],F5[48],
K6[48],L6[32],R6[32],ER6[48],F6[48],
K7[48],L7[32],R7[32],ER7[48],F7[48],
K8[48],L8[32],R8[32],ER8[48],F8[48],
K9[48],L9[32],R9[32],ER9[48],F9[48],
K10[48],L10[32],R10[32],ER10[48],F10[48],
K11[48],L11[32],R11[32],ER11[48],F11[48],
K12[48],L12[32],R12[32],ER12[48],F12[48],
K13[48],L13[32],R13[32],ER13[48],F13[48],
K14[48],L14[32],R14[32],ER14[48],F14[48],
K15[48],L15[32],R15[32],ER15[48],F15[48],
K16[48],L16[32],R16[32],ER16[48],F16[48];

/******************* Input Plain Text *********************/


printf(">Enter plain text : ");
gets(input);

len=strlen(input);

for(i=0;i<len;i++)
{
while(input[i]!=0)
{
r=input[i]%16;
input[i]=input[i]/16;
if(r>9)
{
x=r-10;
r=65+x;
initial_hex[k]=r;
}

DWDM/IS LAB Page 15


WWW.VIDYARTHIPLUS.COM

else
initial_hex[k]=r+48;
k++;
}
}
for(i=0;i<k;i=i+2)
{
temp=initial_hex[i];
initial_hex[i]=initial_hex[i+1];
initial_hex[i+1]=temp;

}
/*for(i=0;i<k;i++)
prin`tf("%c",initial_hex[i]);*/

d=k/16;
e=k%16;
f=0;

for(i=0;i<=d;i++)
{
if(i<d)
{
for(j=0;j<=15;j++)
hex_arr[i][j]=initial_hex[f++];
}
else if(k%16==0)
break;
else
{
for(j=0;j<=15;j++)
{
if(j<e)
hex_arr[i][j]=initial_hex[f++];
else
{
hex_arr[i][j]='2';
hex_arr[i][++j]='0';
}
}
}

}
if(k%16!=0)
d++;

/*printf("\n");

DWDM/IS LAB Page 16


WWW.VIDYARTHIPLUS.COM

for(i=0;i<d;i++)
{
for(j=0;j<=15;j++)
printf("%c",hex_arr[i][j]);
printf("\n");
}*/

/******************* Key in Binary form*****************/


hex_to_bin(key_hex,key_bin);

printf("\n>Key in Hexadecimal used for encryption : ");


for(i=0;i<16;i++)
printf("%c",key_hex[i]);
/*printf("\n");
for(i=0;i<64;i++)
printf("%c",key_bin[i]); */

for(m=0;m<d;m++)
{

for(i=0;i<16;i++)
input_hex[i]=hex_arr[m][i];
/*printf("\n\n");
for(i=0;i<16;i++)
printf("%c",input_hex[i]);
printf("\n");*/
/******************* Plain Text in Binary *****************/
hex_to_bin(input_hex,input_bin);

/*printf("\n");
for(i=0;i<64;i++)
printf("%c",input_bin[i]);*/

/******************* First Round of Permutation *****************/


permutation(key_bin,key_PC1);
/*for(i=0;i<56;i++)
printf("%c",key_PC1[i]); */

make_half(key_PC1,C0,D0);

/*printf("\n\nC0 : ");
for(i=0;i<28;i++)
printf("%c",C0[i]);

printf("\n\nD0 : ");
for(i=0;i<28;i++)
printf("%c",D0[i]);*/

DWDM/IS LAB Page 17


WWW.VIDYARTHIPLUS.COM

/******************** Shifting Begins *********************/


single_shift(C0,C1);
single_shift(D0,D1);

/*printf("\n\nC1 : ");
for(i=0;i<28;i++)
printf("%c",C1[i]);

printf("\n\nD1 : ");
for(i=0;i<28;i++)
printf("%c",D1[i]); */

single_shift(C1,C2);
single_shift(D1,D2);

double_shift(C2,C3);
double_shift(D2,D3);

double_shift(C3,C4);
double_shift(D3,D4);

double_shift(C4,C5);
double_shift(D4,D5);

double_shift(C5,C6);
double_shift(D5,D6);

double_shift(C6,C7);
double_shift(D6,D7);

double_shift(C7,C8);
double_shift(D7,D8);

single_shift(C8,C9);
single_shift(D8,D9);

double_shift(C9,C10);
double_shift(D9,D10);

double_shift(C10,C11);
double_shift(D10,D11);

double_shift(C11,C12);
double_shift(D11,D12);

double_shift(C12,C13);
double_shift(D12,D13);

DWDM/IS LAB Page 18


WWW.VIDYARTHIPLUS.COM

double_shift(C13,C14);
double_shift(D13,D14);

double_shift(C14,C15);
double_shift(D14,D15);

single_shift(C15,C16);
single_shift(D15,D16);

/******************** Shifting Ends *********************/

/*************** 16 Keys Generation Begins **************/

make_key(C1,D1,CD1);
permutation_48(CD1,K1);

/*printf("\n\nCD1 : ");
for(i=0;i<56;i++)
printf("%c",CD1[i]);

printf("\n\nK1 : ");
for(i=0;i<48;i++)
printf("%c",K1[i]);*/

make_key(C2,D2,CD2);
permutation_48(CD2,K2);

make_key(C3,D3,CD3);
permutation_48(CD3,K3);

make_key(C4,D4,CD4);
permutation_48(CD4,K4);

make_key(C5,D5,CD5);
permutation_48(CD5,K5);

make_key(C6,D6,CD6);
permutation_48(CD6,K6);

make_key(C7,D7,CD7);
permutation_48(CD7,K7);

make_key(C8,D8,CD8);
permutation_48(CD8,K8);

make_key(C9,D9,CD9);
permutation_48(CD9,K9);

DWDM/IS LAB Page 19


WWW.VIDYARTHIPLUS.COM

make_key(C10,D10,CD10);
permutation_48(CD10,K10);

make_key(C11,D11,CD11);
permutation_48(CD11,K11);

make_key(C12,D12,CD12);
permutation_48(CD12,K12);

make_key(C13,D13,CD13);
permutation_48(CD13,K13);

make_key(C14,D14,CD14);
permutation_48(CD14,K14);

make_key(C15,D15,CD15);
permutation_48(CD15,K15);

make_key(C16,D16,CD16);
permutation_48(CD16,K16);

/*************** 16 Keys Generation Ends **************/

permutation_64(input_bin,L0,R0);

/************ 16 Rounds of Encryption *****************/

des_round(L1,R1,L0,R0,ER0,K1,F1);

/*printf("\n\nL1 : ");
for(i=0;i<32;i++)
printf("%c",L1[i]);
printf("\n\nR1 : ");
for(i=0;i<32;i++)
printf("%c",R1[i]);*/

des_round(L2,R2,L1,R1,ER1,K2,F2);
des_round(L3,R3,L2,R2,ER2,K3,F3);
des_round(L4,R4,L3,R3,ER3,K4,F4);
des_round(L5,R5,L4,R4,ER4,K5,F5);
des_round(L6,R6,L5,R5,ER5,K6,F6);
des_round(L7,R7,L6,R6,ER6,K7,F7);
des_round(L8,R8,L7,R7,ER7,K8,F8);
des_round(L9,R9,L8,R8,ER8,K9,F9);
des_round(L10,R10,L9,R9,ER9,K10,F10);
des_round(L11,R11,L10,R10,ER10,K11,F11);
des_round(L12,R12,L11,R11,ER11,K12,F12);

DWDM/IS LAB Page 20


WWW.VIDYARTHIPLUS.COM

des_round(L13,R13,L12,R12,ER12,K13,F13);
des_round(L14,R14,L13,R13,ER13,K14,F14);
des_round(L15,R15,L14,R14,ER14,K15,F15);
des_round(L16,R16,L15,R15,ER15,K16,F16);

for(i=0;i<32;i++)
{
encrypted[i]=R16[i];
encrypted[i+32]=L16[i];
}
common_permutation(encrypted,encry_permut);
//encry_permut[64]='\0';
encryption=bin_to_hex(encry_permut);

for(i=0;i<16;i++)
{
encryption_final[++p]=*(encryption+i);
// encryption_final1[i]=*(encryption+i);
// printf("%c ",encryption_final[p]);
}

/****************** 16 Rounds of Decryption ****************/

des_round_decry(L16,R16,L15,R15,ER15,K16,F16);
des_round_decry(L15,R15,L14,R14,ER14,K15,F15);
des_round_decry(L14,R14,L13,R13,ER13,K14,F14);
des_round_decry(L13,R13,L12,R12,ER12,K13,F13);
des_round_decry(L12,R12,L11,R11,ER11,K12,F12);
des_round_decry(L11,R11,L10,R10,ER10,K11,F11);
des_round_decry(L10,R10,L9,R9,ER9,K10,F10);
des_round_decry(L9,R9,L8,R8,ER8,K9,F9);
des_round_decry(L8,R8,L7,R7,ER7,K8,F8);
des_round_decry(L7,R7,L6,R6,ER6,K7,F7);
des_round_decry(L6,R6,L5,R5,ER5,K6,F6);
des_round_decry(L5,R5,L4,R4,ER4,K5,F5);
des_round_decry(L4,R4,L3,R3,ER3,K4,F4);
des_round_decry(L3,R3,L2,R2,ER2,K3,F3);
des_round_decry(L2,R2,L1,R1,ER1,K2,F2);
des_round_decry(L1,R1,L0,R0,ER0,K1,F1);
for(i=0;i<32;i++)
{
decrypted[i]=L0[i];
decrypted[i+32]=R0[i];
}
common_permutation(decrypted,decry_permut);
//decry_permut[64]='\0';

DWDM/IS LAB Page 21


WWW.VIDYARTHIPLUS.COM

decryption=bin_to_hex(decry_permut);
// printf("%s\n",decryption);

for(i=0;i<16;i++)
{
decryption_final_hex[++q]=*(decryption+i);
}
}

encryption_final[p+1]='\0';
printf("\n\n>Encrypted Output : ");
printf("%s",encryption_final);

decryption_final_hex[q+1]='\0';
printf("\n\n>Decrypted Output in Hexadecimal: ");
printf("%s",decryption_final_hex);

hex_to_plain(decryption_final_hex,decryption_final_plain,q+1);

printf("\n>Decrypted Output in Plain Text: ");


printf("%s\n",decryption_final_plain);
}

void hex_to_bin(char *input,char *in)


{
short i,j,k,lim=0;
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
if(*(input+i)==hex[j])
{
for(k=0;k<4;k++)
{
*(in+lim)=bin[j][
k];
lim++;
}
}
}
}
}

char* bin_to_hex(char *bit)


{
char tmp[5],*out;
short lim=0,i,j;

DWDM/IS LAB Page 22


WWW.VIDYARTHIPLUS.COM

out=(char*)malloc(16*sizeof(char));
for(i=0;i<64;i=i+4)
{
tmp[0]=bit[i];
tmp[1]=bit[i+1];
tmp[2]=bit[i+2];
tmp[3]=bit[i+3];
tmp[4]='\0';
for(j=0;j<16;j++)
{
if((strcmp(tmp,bin[j]))==0)
{
out[lim++]=hex[j];
break;
}
}
}
out[lim]='\0';
return out;
}

void hex_to_plain(char *in,char *out,int t)


{
int i,j=0,z,sum;
char temp[3];

for(i=0;i<t;i=i+2)
{
sum=0;
temp[0]=in[i];
if(temp[0]>=65 && temp[0]<=71)
z=switch_case(temp[0]);
else
z=temp[0]-48;
sum=sum+z*16;
temp[1]=in[i+1];
if(temp[1]>=65 && temp[1]<=71)
z=switch_case(temp[1]);
else
z=temp[1]-48;

sum=sum+z*1;
temp[2]='\0';

*(out+j)=sum;
j++;
}

DWDM/IS LAB Page 23


WWW.VIDYARTHIPLUS.COM

*(out+j)='\0';
}

int switch_case(char a)
{
switch(a)
{
case 'A':
return(10);
break;

case 'B':
return(11);
break;

case 'C':
return(12);
break;

case 'D':
return(13);
break;

case 'E':
return(14);
break;

case 'F':
return(15);
break;

}
}

void permutation(char *key_bin,char *key_PC1)


{
short i,j,k=0,temp;

for(i=0;i<8;i++)
{
for(j=0;j<7;j++)
{
temp=PC1[i][j]-1;
*(key_PC1+k)=*(key_bin+temp);
k++;
}
}
}

DWDM/IS LAB Page 24


WWW.VIDYARTHIPLUS.COM

void make_half(char *key_PC1,char *a,char *b)


{
int i,j=0;
for(i=0;i<56;i++)
{
if(i<28)
*(a+i)=*(key_PC1+i);
else
{
*(b+j)=*(key_PC1+i);
j++;
}
}
}

void single_shift(char *p,char *q)


{
int i;
*(q+27)=*(p+0);
for(i=0;i<27;i++)
*(q+i)=*(p+(i+1));
}

void double_shift(char *p,char *q)


{
int i;
*(q+26)=*(p+0);
*(q+27)=*(p+1);
for(i=0;i<26;i++)
*(q+i)=*(p+(i+2));
}

void make_key(char *a,char *b,char *c)


{
int i;
for(i=0;i<28;i++)
*(c+i)=*(a+i);
for(i=28;i<56;i++)
*(c+i)=*(b+(i-28));
}

void permutation_48(char *CD,char *K)


{
short i,j,m=0,temp;

for(i=0;i<8;i++)
{

DWDM/IS LAB Page 25


WWW.VIDYARTHIPLUS.COM

for(j=0;j<6;j++)
{
temp=PC2[i][j]-1;
*(K+m)=*(CD+temp);
m++;
}
}
}

void permutation_64(char *in,char *L,char *R)


{
int i,j,m=0,temp;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
temp=IP[i][j]-1;
*(L+m)=*(in+temp);
m++;
}
}
m=0;
for(i=4;i<8;i++)
{
for(j=0;j<8;j++)
{
temp=IP[i][j]-1;
*(R+m)=*(in+temp);
m++;
}
}
}

void des_round(char *L1,char *R1,char *L0,char *R0,char *ER0,char *K1,char *F1)


{
char t[3],tp[5],f[32];
int temp,i,row,column,j,limit=0;
copy(L1,R0);
permut_48(R0,ER0);
/*printf("\nER0 : ");
for(i=0;i<48;i++)
printf("%c",ER0[i]);*/

xor(K1,ER0,F1);

/*printf("\nF1 : ");
for(i=0;i<48;i++)
printf("%c",F1[i]);*/

DWDM/IS LAB Page 26


WWW.VIDYARTHIPLUS.COM

for(i=0;i<48;i=i+6)
{
t[0]=F1[i];
t[1]=F1[i+5];
t[2]='\0';
for(j=0;j<4;j++)
{
if(strcmp(t,look_up[j])==0)
{
row=j;
/*printf("%d",row);*/
break;
}
}
tp[0]=F1[i+1];
tp[1]=F1[i+2];
tp[2]=F1[i+3];
tp[3]=F1[i+4];
tp[4]='\0';
for(j=0;j<16;j++)
{
if(strcmp(tp,bin[j])==0)
{
column=j;
break;
}
}
switch(i)
{
case 0:
temp=s1[row][column];
break;
case 6:
temp=s2[row][column];
break;
case 12:
temp=s3[row][column];
break;
case 18:
temp=s4[row][column];
break;
case 24:
temp=s5[row][column];
break;
case 30:
temp=s6[row][column];
break;

DWDM/IS LAB Page 27


WWW.VIDYARTHIPLUS.COM

case 36:
temp=s7[row][column];
break;
case 42:
temp=s8[row][column];
break;
}
for(j=0;j<4;j++)
{
SB[limit]=bin[temp][j];
limit++;
}
}
SB[limit]='\0';
/*printf("\nSB : %s",SB); */
permutation_32(SB,f);
SB[0]='\0';
xor_32(L0,f,R1);
}

void des_round_decry(char *L1,char *R1,char *L0,char *R0,char *ER0,char *K1,char *F1)


{
char tp[5],f[32];
short temp,i,row,column,j,limit=0;
copy(L1,R0);
permut_48(R0,ER0);
xor(K1,ER0,F1);
for(i=0;i<48;i=i+6)
{
tp[0]=F1[i];
tp[1]=F1[i+5];
tp[2]='\0';
for(j=0;j<4;j++)
{
if(strcmp(tp,look_up[j])==0)
{
row=j;
break;
}
}
tp[0]=F1[i+1];
tp[1]=F1[i+2];
tp[2]=F1[i+3];
tp[3]=F1[i+4];
tp[4]='\0';
for(j=0;j<16;j++)
{
if(strcmp(tp,bin[j])==0)

DWDM/IS LAB Page 28


WWW.VIDYARTHIPLUS.COM

{
column=j;
break;
}
}
switch(i)
{
case 0:
temp=s1[row][column];
break;
case 6:
temp=s2[row][column];
break;
case 12:
temp=s3[row][column];
break;
case 18:
temp=s4[row][column];
break;
case 24:
temp=s5[row][column];
break;
case 30:
temp=s6[row][column];
break;
case 36:
temp=s7[row][column];
break;
case 42:
temp=s8[row][column];
break;
}
for(j=0;j<4;j++)
{
SB[limit]=bin[temp][j];
limit++;
}
}
SB[limit]='\0';
permutation_32(SB,f);
SB[0]='\0';
xor_32(L0,f,R1);
}

void copy(char *L,char *R)


{
int i;
for(i=0;i<32;i++)

DWDM/IS LAB Page 29


WWW.VIDYARTHIPLUS.COM

*(L+i)=*(R+i);
}

void permut_48(char *R,char *ER)


{
short i,j,m=0,temp;

for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=E_bit[i][j]-1;
*(ER+m)=*(R+temp);
m++;
}
}
}

void xor(char *K,char *ER,char *F)


{
int i,m=0;
for(i=0;i<48;i++)
{
if((*(K+i)=='1' && *(ER+i)=='1') || (*(K+i)=='0' && *(ER+i)=='0'))
{
*(F+m)='0';
m++;
}
else
{
*(F+m)='1';
m++;
}
}
}

void xor_32(char *L0,char *f,char *R1)


{
short i,m=0;
for(i=0;i<32;i++)
{
if((*(L0+i)=='1' && *(f+i)=='1') || (*(L0+i)=='0' && *(f+i)=='0'))
{
*(R1+m)='0';
m++;
}
else
{

DWDM/IS LAB Page 30


WWW.VIDYARTHIPLUS.COM

*(R1+m)='1';
m++;
}
}
}

void permutation_32(char *SB1,char *f)


{
short i,j,m=0,temp;

for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
temp=sb_permutation[i][j]-1;
*(f+m)=*(SB1+temp);
m++;
}
}
}

void common_permutation(char *in,char *out)


{
short i,j,temp,m=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
temp=ip_inverse[i][j]-1;
out[m]=in[temp];
m++;
}} }

OUTPUT:

Enter plain text : welcometolab

>Key in Hexadecimal used for encryption : 133457799BBCDFF1

>Encrypted Output : 21BC301EA19E1FC0085740881414E582

>Decrypted Output in Hexadecimal: 77656C636F6D65746F6C616220202020

>Decrypted Output in Plain Text: welcometolab

DWDM/IS LAB Page 31


WWW.VIDYARTHIPLUS.COM

5.Write a C Program to implement Blowfish Algoirthm logic?

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

int
main (void)
{
BLOWFISH_CTX ctx;
int n;

/* must be less than 56 bytes */


char *key = "a random number string would be a better key";
int keylen = strlen(key);

uint8_t *plaintext_string = "this is our message";


int plaintext_len = strlen(plaintext_string);

uint8_t ciphertext_buffer[256];
uint8_t *ciphertext_string = &ciphertext_buffer[0];
int ciphertext_len = 0;

uint32_t message_left;
uint32_t message_right;
int block_len;

#if 1
/* sanity test, encrypts a known message */
n = Blowfish_Test(&ctx);
printf("Blowfish_Test returned: %d.%s\n", n, n ? " Abort." : "");
if (n) return n;
#endif

Blowfish_Init(&ctx, key, keylen);

printf("Plaintext message string is: %s\n", plaintext_string);

/* encrypt the plaintext message string */


printf("Encrypted message string is: ");

while (plaintext_len)
{
message_left = message_right = 0UL;

DWDM/IS LAB Page 32


WWW.VIDYARTHIPLUS.COM

/* crack the message string into a 64-bit block (ok, really two 32-bit blocks); pad with zeros
if necessary */
for (block_len = 0; block_len < 4;="" block_len++)="">
{
message_left = message_left <>
if (plaintext_len)
{
message_left += *plaintext_string++;
plaintext_len--;
}
else message_left += 0;
}
for (block_len = 0; block_len < 4;="" block_len++)="">
{
message_right = message_right <>
if (plaintext_len)
{
message_right += *plaintext_string++;
plaintext_len--;
}
else message_right += 0;
}
/* encrypt and print the results */
Blowfish_Encrypt(&ctx, &message_left, &message_right);
printf("%lx%lx", message_left, message_right);

/* save the results for decryption below */


*ciphertext_string++ = (uint8_t)(message_left >> 24);
*ciphertext_string++ = (uint8_t)(message_left >> 16);
*ciphertext_string++ = (uint8_t)(message_left >> 8);
*ciphertext_string++ = (uint8_t)message_left;
*ciphertext_string++ = (uint8_t)(message_right >> 24);
*ciphertext_string++ = (uint8_t)(message_right >> 16);
*ciphertext_string++ = (uint8_t)(message_right >> 8);
*ciphertext_string++ = (uint8_t)message_right;
ciphertext_len += 8;
printf("\n");

/* reverse the process */


printf("Decrypted message string is: ");

ciphertext_string = &ciphertext_buffer[0];
while(ciphertext_len)

DWDM/IS LAB Page 33


WWW.VIDYARTHIPLUS.COM

{
message_left = message_right = 0UL;

for (block_len = 0; block_len < 4;="" block_len++)="">


{
message_left = message_left <>
message_left += *ciphertext_string++;
if (ciphertext_len)
ciphertext_len--;
}
for (block_len = 0; block_len < 4;="" block_len++)="">
{
message_right = message_right <>
message_right += *ciphertext_string++;
if (ciphertext_len)
ciphertext_len--;
}

Blowfish_Decrypt(&ctx, &message_left, &message_right);

/* if plaintext message string padded, extra zeros here */

printf("%c%c%c%c%c%c%c%c",
(int)(message_left >> 24), (int)(message_left >> 16),
(int)(message_left >> 8), (int)(message_left),
(int)(message_right >> 24), (int)(message_right >> 16),
(int)(message_right >> 8), (int)(message_right));
}

printf("\n");

return 0;
}

OUTPUT:
Plaintext message string is: Welcome to griet
Encrypted message string is: sYrjgkl6voswuYbrasvAKvgdnDEkud29hyege9
Decrypted message string is : Welcome to griet

DWDM/IS LAB Page 34


WWW.VIDYARTHIPLUS.COM

6.Write a C Program to implement AES Algorthim logic

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* MCrypt API available online:
* http://linux.die.net/man/3/mcrypt
*/
#include <mcrypt.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
int encrypt(
void* buffer,
int buffer_len, /* Because the plaintext could include null bytes*/
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){return 1;}
mcrypt_generic_init(td, key, key_len, IV);
mcrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
int decrypt(
void* buffer,
int buffer_len,
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){return 1;}
mcrypt_generic_init(td, key, key_len, IV);
mdecrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
void display(char* ciphertext, int len){
int v;
for (v=0; v<len; v++){

DWDM/IS LAB Page 35


WWW.VIDYARTHIPLUS.COM

printf("%d ", ciphertext[v]);


}
printf("\n");
}
int main()
{
MCRYPT td, td2;
char * plaintext = "test text 123";
char* IV = "AAAAAAAAAAAAAAAA";
char *key = "0123456789abcdef";
int keysize = 16; /* 128 bits */
char* buffer;
int buffer_len = 16;
buffer = calloc(1, buffer_len);
strncpy(buffer, plaintext, buffer_len);
printf("==C==\n");
printf("Plain Text: %s\n", plaintext);
encrypt(buffer, buffer_len, IV, key, keysize);
printf("Cipher Text: "); display(buffer , buffer_len);
decrypt(buffer, buffer_len, IV, key, keysize);
printf("Decrypt Text: %s\n", buffer);
return 0;
}

OUTPUT:

Plain Text : AES SymmetricEncryptionDecryption

Cipher Text
:sY6vkQrWRg0fvRzbqSAYxepeBIXg4AySj7Xh3x4vDv8TBTkNiTfca7wW/dxiMMJl

Decrypted Text : AES Symmetric Encryption Decryption

DWDM/IS LAB Page 36


WWW.VIDYARTHIPLUS.COM

7.Write the RC4 logic injava.


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.print(“\nENTER PLAIN TEXT\t”);
ptext=in.readLine();
System.out.print(“\n\nENTER KEY TEXT\t\t”);
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];
}

DWDM/IS LAB Page 37


WWW.VIDYARTHIPLUS.COM

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.print(“\n\nENCRYPTED:\t\t”);
display(cipher);
System.out.print(“\n\nDECRYPTED:\t\t”);

DWDM/IS LAB Page 38


WWW.VIDYARTHIPLUS.COM

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:
ENTER PLAIN TEXT :RC4 PROGRAM

ENTER KEY TEXT A

ENCRYPTED: ??-??±?µFJ|

DECRYPTED: RC4 PROGRAM

DWDM/IS LAB Page 39


WWW.VIDYARTHIPLUS.COM

8.Write a C Program to Implement RSA algorithm.

#include <stdio.h>

int ex_gcd(int a,int b,int n) //computes the GCD using the Extended Euclid method
{
int x=0,y=1,lastx=1,lasty=0;
int temp,q;
while(b!=0)
{
temp =b;
q = a/b;
b = a%b;
a = temp;

temp=x;
x = lastx - q*x;
lastx = temp;

temp =y;
y = lasty - q*y;
lasty = temp;
}
if(n==1) return a;
else return lasty;
}

long en_de(int base, int exp,int n)


{
int b[30],i,c=0;
long d=1;
for(i=0;exp!=0;exp/=2,i++)
b[i]=exp%2;

i--;
for(;i>=0;i--)
{
c = 2*c;
d = (d*d) %n;
if(b[i]==1)
{
c = c+1;
d = (d*base)%n;
}
}
return d;
}
void main(){

DWDM/IS LAB Page 40


WWW.VIDYARTHIPLUS.COM

int p,q,i;
int pt,ct;
int e,d,et,n,temp;

printf("Enter prime No.s p,q :");


scanf("%d %d",&p,&q);

n = p*q;
et=(p-1)*(q-1);

for(i=2;i<et;i++)
if(ex_gcd(et,i,1) == 1)
printf("%d\t",i);

printf("\nSelect e value:");
scanf("%d",&e);

temp = ex_gcd(et,e,2);
d = et+temp;

printf("\nPublic Key KU = {%d,%d}\n",e,n);


printf("Private Key KR = {%d,%d}\n",d,n);

printf("Enter Plain text M Integer (0<M<%d):",n);


scanf("%d",&pt);

ct = en_de(pt,e,n);

printf("\nCipher Text = %d",ct);


printf("\n\nPlan Text After decrtion :%d",en_de(ct,d,n));
}
OUTPUT:

enter two prime numbers 11 3

enter e value such that 1<=e<=20: 7

Public key : (7,33)

Private key : (3,33)

Enter the plain text : 20

Cipher text : 26

plain text : 20

DWDM/IS LAB Page 41


WWW.VIDYARTHIPLUS.COM

9.Implement the the Diffie-Helman key Exchange mechanism

#include <stdio.h>
#include <math.h>
void main()
{
int q,alpha,xa,xb,ya,yb,ka,kb, x,y,z,count,ai[20][20];
printf("Enter a Prime Number \"q\":");
scanf("%d",&q);
printf("Enter a No \"xa\" which is lessthan value of q:");
scanf("%d",&xa);
printf("Enter a No \"xb\" which is lessthan value of q:");
scanf("%d",&xb);
for(x=0;x<q-1;x++) //Primitive Root Calculation
for(y=0;y<q-1;y++)
ai[x][y] = ((int)pow(x+1,y+1))%q;
for(x=0;x<q-1;x++)
{
count = 0;
for(y=0;y<q-2;y++)
{
for(z=y+1;z<q-1;z++)
if(ai[x][y] == ai[x][z])
{
count = 1;
break;
}
if(count == 1)
break;
}
if (count == 0 )
{
alpha = x+1;
break;
}
}
printf("alpha = %d\n",alpha);
ya = ((int)pow(alpha,xa))%q;
yb = ((int)pow(alpha,xb))%q;
ka = ((int)pow(yb,xa))%q;
kb = ((int)pow(yb,xb))%q;
printf("ya = %d\nyb = %d\nka = %d\nkb = %d\n",ya,yb,ka,kb);
if(ka == kb)
printf("The keys exchanged are same");
else
printf("The keys exchanged are not same");
}

DWDM/IS LAB Page 42


WWW.VIDYARTHIPLUS.COM

OUTPUT:
Enter a Prime number “q”: 23
Enter a no “xa” which is lessthan value of q:7
Enter a no “xb” which is lessthan value of q:11
Alpha = 134514977
ya = -2
yb = -2
ka = -2
kb = -2
The keys exchanged are same.

DWDM/IS LAB Page 43


WWW.VIDYARTHIPLUS.COM

10.Implementation of SHA-1 Algorithm

#include<stdio.h>
#include<string.h>
#define uchar unsigned char
#define unit unsigned long
#define DBL_INT_ADD (a,b,c) if(a>0 xfffff..(c))++b;a+=c;
#define ROTLEFT(a,b)(((a)<<(b)))||(a)>>32-(b))
#define ROTRIGHT(a,b)(((a)>>(b)))||((a)<<32-(b))
#define CH(x,y,z) (((x)&(y)̂( ̃(x)&(z)))
#define MAJ(x,y,z)(((x)&(y))̂ ((x)&(z))̂ ((y)& (z))
#define Ep0(x) (ROTRIGHT(x,z)̂ROTRIGHT(x,13)̂ROTRIGHT(x,22))
#define Ep1(x)(ROTRIGHT(x,6)^ROTRIGHT(x,4)^ROTRIGHT(x,23))
#define SIG0(x) (ROTRIGHT(x,7)̂ROTRIGHT(x,18)̂ROTRIGHT(x,13))
#define SIG1(x) (ROTRIGHT(x,17)̂ROTRIGHT(x,19)̂ROTRIGHT(x)>>10))
Typedef struct {
uchar data[64];
uint datalen;
uint bitlen[2];
uint state[8];
} SHA256_CTX;
Unit
K[64]={0X48a2fa8,0X7347449,0Xb5c0fbef,0Xe95bda5,0X59f11f19,0X923f82a4,0Xab1c5e
b5,0Xbefg984a,0X4a7484aa,0X51ab5632,0X32fb5c32,0X88ac5k32};
Void sha256_transform(SHA256_CTX, *ctx,uchar data[])
{
Unit a,b,c,d,e,f,g,h,i,j,t1,t2,m[64];
for(i=0,j=0;i<16;i++;j+=4)
m[i]=(data[j]<<24)|(data[j+1]<<16)|(data[j+2]<<18|data[j+3]);
for(i=0,j=0;i<64;i++)
m[i]=SIG1(m[i-2]+m[i-7]+SIG0(m[i-15]+m[i-16]);
a=ctx->state[0];
b=ctx->state[1];
c=ctx->state[2];
d=ctx->state[3];
n=ctx->state[7];
for(i=0;i<64;++i)
{
t1=h+ep1(e) +CH(e,f,g)+k[i]+m[i];
t2=ei0(a)+MAJ(a,b,c);
h=g;
g=f;
f=e;
e=d+t1;
d=c,e=b,b=a,a=t1+t2;
}
Ctx->state[0]+=a;
Ctx->state[1]+=b;

DWDM/IS LAB Page 44


WWW.VIDYARTHIPLUS.COM

.
.
.
Ctx->state[7]+=c;
}
Void sha256_int(SHA256_CTX *ctx)
{
Ctx->datalen=0;
Ctx->bitlen[0]=0;
Ctx->bitlen[1]=0;
Ctx->state[0]=0x6a09e667;
Ctx->state[7]=0x5be0cd19;
Ctx->state[7]=0x5be0cd19;
}
Void sha256_upadte(SHA256_CTX *ctx,uchar data[],unit lent){
uint t,i;
for(i=0;i<len;i++)
{
Ctx->data[ctx->datalen]=data[i];
Ctx->datalen++;
If(ctx->datalen==64)
{
SHA256_transform9ctx,ctx->data);
Ctx->datalen=0;
}
}
}
void sha256_final(SHA256_CTX *ctx,uchar hash[])
{
Uint I;
i=ctx-.datalen;
if(ctx->datalen<56)
{
Ctx->data[i++]=ox80;
While(i<56)
Ctx->data[i++]=ox00;
}
Else
{
Ctx->data[i++]=0x80;
While(i<64)
{
Ctx->data[i++]=0x00;
SHA256_transform(ctx,ctx->data);
Memset(ctx->data,0,56);
DBL_INT_ADD(ctx_bitlen[0],ctx_bitlen[1],ctx->datalen *8);
Ctx->data[1B]=ctx->bitlen[0];
Ctx->data[62]=ctx->bitlen[0]>>8;

DWDM/IS LAB Page 45


WWW.VIDYARTHIPLUS.COM

Ctx->data[56]=ctx->bitlen[1]>>24;
Sha256_transform(ctx,ctx->data);
}
Void print_hash(unsigned char hash[])
{
Int idx;
For(idx=0;idx<32;idx++)
Printf(“%02X”,hash[idx]);
Printf(‘\n”);
}
Viod main()
{
Unsigned char text1[]={“abcd”},text3[]={“aaaa”};has[32],
text2[]={“abcdefghijklmnopqrstuvwxyz”}
SHA256_CTX ctx;
SHA256_init(&ctx);
SHA256_upadte(&ctx,text1,strlen(text1));
SHA256_final(&ctx,hash);
Print_hash(hash);
Sha256_int(&ctx);
Sha256_update(&ctx,text2,strlen(text2));
Sha256_final(&ctx,hash);
for(idx=0;idx<10000;++idx)
sha256_upadte(&ctx,text3,strlen(text3));
sha256_final(&ctx,hash);
print_hash(hash);
getchar();
return 0;
}

OUTPUT:

ba7816bf8f0ioc1fea414ode5dae2223boo361a396177a9cb410ff61f20015ad248d6a61d2063ab
8e5c02b930c33ce4596ff2167f6ccdd419db06ccdc76e5c9914fb928191c7e2848a497200e04bd
39ccc7112cd0a.

DWDM/IS LAB Page 46


WWW.VIDYARTHIPLUS.COM

11.Write a program of MD5 alagorithm

// Implementation of MD5 Algorithm

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

typedef union uwb {


unsigned w;
unsigned char b[4];
} MD5union;

typedef unsigned DigestArray[4];

unsigned func0( unsigned abcd[] ){


return ( abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);}

unsigned func1( unsigned abcd[] ){


return ( abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);}

unsigned func2( unsigned abcd[] ){


return abcd[1] ^ abcd[2] ^ abcd[3];}

unsigned func3( unsigned abcd[] ){


return abcd[2] ^ (abcd[1] |~ abcd[3]);}

typedef unsigned (*DgstFctn)(unsigned a[]);

/*Use binary integer part of the sines of integers (Radians) as constants*/


/*or*/
/* Hardcode the below table but i want generic code that why coded it
using calcuate table function
k[ 0.. 3] := { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee }
k[ 4.. 7] := { 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 }
k[ 8..11] := { 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be }
k[12..15] := { 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 }
k[16..19] := { 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa }
k[20..23] := { 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 }
k[24..27] := { 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed }
k[28..31] := { 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a }
k[32..35] := { 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c }
k[36..39] := { 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 }
k[40..43] := { 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 }
k[44..47] := { 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 }
k[48..51] := { 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 }
k[52..55] := { 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 }

DWDM/IS LAB Page 47


WWW.VIDYARTHIPLUS.COM

k[56..59] := { 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 }


k[60..63] := { 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }*/

unsigned *calctable( unsigned *k)


{
double s, pwr;
int i;

pwr = pow( 2, 32);


for (i=0; i<64; i++) {
s = fabs(sin(1+i));
k[i] = (unsigned)( s * pwr );
}
return k;
}

/*Rotate Left r by N bits


or
We can directly hardcode below table but as i explained above we are opting generic code so
shifting the bit manually.

r[0..15] := {7,12,17,22,7, 12, 17, 22, 7, 12, 17, 22, 7, 12,17, 22}
r[16..31] := {5, 9, 14, 20, 5, 9, 14, 20, 5, 9,14, 20, 5, 9, 14, 20}
r[32..47] := {4,11,16, 23, 4, 11, 16, 23, 4, 11, 16,23, 4,11,16, 23}
r[48..63] := {6,10, 15, 21,6,10, 15, 21, 6, 10, 15, 21,6, 10,15, 21}
*/
unsigned rol( unsigned r, short N )
{
unsigned mask1 = (1<<N) -1;
return ((r>>(32-N)) & mask1) | ((r<<N) & ~mask1);
}

unsigned *md5( const char *msg, int mlen)


{
/*Initialize Digest Array as A , B, C, D */
static DigestArray h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3 };
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7,12,17,22};
static short rot1[] = { 5, 9,14,20};
static short rot2[] = { 4,11,16,23};
static short rot3[] = { 6,10,15,21};
static short *rots[] = {rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned *k;

static DigestArray h;

DWDM/IS LAB Page 48


WWW.VIDYARTHIPLUS.COM

DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
short *rotn;
union {
unsigned w[16];
char b[64];
}mm;
int os = 0;
int grp, grps, q, p;
unsigned char *msg2;

if (k==NULL) k= calctable(kspace);

for (q=0; q<4; q++) h[q] = h0[q]; // initialize

{
grps = 1 + (mlen+8)/64;
msg2 = malloc( 64*grps);
memcpy( msg2, msg, mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64*grps){ msg2[q] = 0; q++ ; }
{
MD5union u;
u.w = 8*mlen;
q -= 8;
memcpy(msg2+q, &u.w, 4 );
}
}

for (grp=0; grp<grps; grp++)


{
memcpy( mm.b, msg2+os, 64);
for(q=0;q<4;q++) abcd[q] = h[q];
for (p = 0; p<4; p++) {
fctn = ff[p];
rotn = rots[p];
m = M[p]; o= O[p];
for (q=0; q<16; q++) {
g = (m*q + o) % 16;
f = abcd[1] + rol( abcd[0]+ fctn(abcd) + k[q+16*p] +
mm.w[g], rotn[q%4]);

abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];

DWDM/IS LAB Page 49


WWW.VIDYARTHIPLUS.COM

abcd[1] = f;
}
}
for (p=0; p<4; p++)
h[p] += abcd[p];
os += 64;
}
return h;
}

int main( int argc, char *argv[] )


{
int j,k;
const char *msg = "This code has been provided by C codechamp";
printf("----------------------------------------------------\n");
printf("\t MD5 ENCRYPTION ALGORITHM IN C \n\n");
printf("----------------------------------------------------\n");

printf("Input String to be Encrypted using MD5 : \n\t%s",msg);


unsigned *d = md5(msg, strlen(msg));
MD5union u;
printf("\n\n\nThe MD5 code for input string is : \n");
printf("\t= 0x");
for (j=0;j<4; j++){
u.w = d[j];
for (k=0;k<4;k++) printf("%02x",u.b[k]);
}
printf("\n");
printf("\n\t MD5 Encyption Successfully Completed!!!\n\n");
getch();
system("pause");
return 0;
}

OUTPUT:

Input string to be encrypted using MD5


welcome to dwdm and is lab

---------------------------------------------------
MD5 ENCRYPTION ALGORITHM IN C \n\n");
----------------------------------------------------

0x8c09f08e0000f08e0000f08e0000f08e

MD5 Encyption Successfully Completed!!!

DWDM/IS LAB Page 50


WWW.VIDYARTHIPLUS.COM

DATA WAREHOUSING AND MINING LAB

Introduction to Weka (Data Mining Tool)

Weka is a collection of machine learning algorithms for data mining tasks. The
algorithms can either be applied directly to a dataset (using GUI) or called from your own
Java code (using Weka Java library).

Tools (or functions) in Weka include:


• Data preprocessing (e.g., Data Filters),
• Classification (e.g., BayesNet, KNN, C4.5 Decision Tree, Neural
Networks, SVM),
• Regression (e.g., Linear Regression, Isotonic Regression, SVM for
Regression),
• Clustering (e.g., Simple K-means, Expectation Maximization (EM)),
• Association rules (e.g., Apriori Algorithm, Predictive Accuracy,
Confirmation Guided),
• Feature Selection (e.g., Cfs Subset Evaluation, Information Gain, Chisquared
Statistic), and
• Visualization (e.g., View different two-dimensional plots of the data).

Launching WEKA

The Weka GUI Chooser (class weka.gui.GUIChooser) provides a starting point for
launching Weka‘s main GUI applications and supporting tools. If one prefers a MDI
(―multiple document interface‖) appearance, then this is provided by an alternative
launcher called ― Main‖ (class weka.gui.Main). The GUI Chooser consists of four
buttons one for each of the four major Weka applications and four menus. The
buttons can be used to start the following applications:

• Explorer:- An environment for exploring data with WEKA (the rest of this
documentation deals with this application in more detail).
• Experimenter:- An environment for performing experiments and conducting
statistical tests between learning schemes.
• Knowledge Flow:- This environment supports essentially the same functions as the
Explorer but with a drag-and-drop interface. One advantage is that it supports
incremental learning.
• Simple CLI:- Provides a simple command-line interface that allows direct execution
of WEKA commands for operating systems that do not provide their own command
line interface.

DWDM/IS LAB Page 51


WWW.VIDYARTHIPLUS.COM

TASK1: List all the categorical (or nominal) attributes and the real-valued attributes
separately.
Aim: To list all the categorical(or nominal) attributes and the real valued attributes using
Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure:

1) Open the Weka GUI Chooser.

2) Select EXPLORER present in Applications.

3) Select Preprocess Tab.

4) Go to OPEN file and browse the file that is already stored in the system “credit-g.arff”.

5) Clicking on any attribute in the left panel will show the basic statistics on that selected
attribute.

OUTPUT:-
Attributes:-
1. checking_status
2. duration
3. credit history
4. purpose
5. credit amount
6. savings_status
7. employment duration
8. installment rate
9. personal status
10. debitors
11. residence_since
12. property
14. installment plans
15. housing
16. existing credits
17. job
18. num_dependents
19. telephone
20. foreign worker
(i) Categorical or Nomianal attributes:-
1. checking_status
2. credit history

DWDM/IS LAB Page 52


WWW.VIDYARTHIPLUS.COM

3. purpose
4. savings_status
5. employment
6. personal status
7. debtors
8. property
9. installment plans
10. housing
11. job
12. telephone
13. foreign worker
(ii) Real valued attributes:-
1. duration
2. credit amount
3. credit amount
4. residence
5. age
6. existing credits
7. num_dependents

TASK 2: What attributes do you think might be crucial in making the credit
assessement ? Come up with some simple rules in plain English using your selected
attributes.

OUTPUT:-
According to me the following attributes may be crucial in making the credit risk
assessment.
1. Credit_history
2. Employment
3. Property_magnitude
4. job
5. duration
6. crdit_amount
7. installment
8. existing credit

Basing on the above attributes, we can make a decision whether to give credit or not.

DWDM/IS LAB Page 53


WWW.VIDYARTHIPLUS.COM

TASK 3: One type of model that you can create is a Decision Tree - train a Decision
Tree using the complete dataset as the training data. Report the model obtained after
training.

Aim: To create a Decision tree by training data set using Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure:

1) Open Weka GUI Chooser.

2) Select EXPLORER present in Applications.

3) Select Preprocess Tab.

4) Go to OPEN file and browse the file that is already stored in the system “credit-g.arff”.

5) Go to Classify tab.

6) Here the c4.5 algorithm has been chosen which is entitled as j48 in Java and can be
selected by clicking the button choose

7) and select tree j48

9) Select Test options “Use training set”

10) if need select attribute.

11) Click Start .

12)now we can see the output details in the Classifier output.

13) right click on the result list and select ” visualize tree “option .

OUTPUT:-
J48 pruned tree
------------------
checking_status = <0
| foreign_worker = yes
| | duration <= 11
| | | existing_credits <= 1
| | | | property_magnitude = real estate: good (8.0/1.0)
| | | | property_magnitude = life insurance
| | | | | own_telephone = none: bad (2.0)
| | | | | own_telephone = yes: good (4.0)
| | | | property_magnitude = car: good (2.0/1.0)

DWDM/IS LAB Page 54


WWW.VIDYARTHIPLUS.COM

| | | | property_magnitude = no known property: bad (3.0)


| | | existing_credits > 1: good (14.0)
| | duration > 11
| | | job = unemp/unskilled non res: bad (5.0/1.0)
| | | job = unskilled resident
| | | | purpose = new car
| | | | | own_telephone = none: bad (10.0/2.0)
| | | | | own_telephone = yes: good (2.0)
| | | | purpose = used car: bad (1.0)
| | | | purpose = furniture/equipment
| | employment = unemployed: good (0.0)
| | employment = <1: bad (3.0)
| | employment = 1<=X<4: good (4.0)
| | employment = 4<=X<7: good (1.0)
| | employment = >=7: good (2.0)
| purpose = radio/tv
| | existing_credits <= 1: bad (10.0/3.0)
| | existing_credits > 1: good (2.0)
| purpose = domestic appliance: bad (1.0)
| purpose = repairs: bad (1.0)
| purpose = education: bad (1.0)
| purpose = vacation: bad (0.0)
| purpose = retraining: good (1.0)
| purpose = business: good (3.0)
| purpose = other: good (1.0)
job = skilled
| other_parties = none
| | duration <= 30
| | | savings_status = <100
| | | | credit_history = no credits/all paid: bad (8.0/1.0)
| | | | credit_history = all paid: bad (6.0)
| | | | credit_history = existing paid
| | | | | own_telephone = none
| | | | | | existing_credits <= 1
| | | | | | | property_magnitude = real estate
| | | | | | | | age <= 26: bad (5.0)
| | | | | | | | age > 26: good (2.0)
| | | | | | | property_magnitude = life insurance: bad (7.0/2.0)
| | | | | | | property_magnitude = car
| | | | | | | | credit_amount <= 1386: bad (3.0)
| | | | | | | | credit_amount > 1386: good (11.0/1.0)
| | | | | | | property_magnitude = no known property: good (2.0)

DWDM/IS LAB Page 55


WWW.VIDYARTHIPLUS.COM

| | | | | | existing_credits > 1: bad (3.0)


| | | | | own_telephone = yes: bad (5.0)
| | | | credit_history = delayed previously: bad (4.0)
| | | | credit_history = critical/other existing credit: good (14.0/4.0)
| | | savings_status = 100<=X<500
| | | | credit_history = no credits/all paid: good (0.0)
| | | | credit_history = all paid: good (1.0)
| | | | credit_history = existing paid: bad (3.0)
| | | | credit_history = delayed previously: good (0.0)
| | | | credit_history = critical/other existing credit: good (2.0)
| | | savings_status = 500<=X<1000: good (4.0/1.0)
| | | savings_status = >=1000: good (4.0)
| | | savings_status = no known savings
| | | | existing_credits <= 1
| | | | | own_telephone = none: bad (9.0/1.0)
| | | | | own_telephone = yes: good (4.0/1.0)
| | | | | | | existing_credits > 1: good (2.0)
| | | | | duration > 30: bad (30.0/3.0)
| | | | other_parties = co applicant: bad (7.0/1.0)
| | | | other_parties = guarantor: good (12.0/3.0)
| | | job = high qualif/self emp/mgmt: good (30.0/8.0)
| foreign_worker = no: good (15.0/2.0)
checking_status = 0<=X<200
| credit_amount <= 9857
| | savings_status = <100
| | | other_parties = none
| | | | duration <= 42
| | | | | personal_status = male div/sep: bad (8.0/2.0)
| | | | | personal_status = female div/dep/mar
| | | | | | purpose = new car: bad (5.0/1.0)
| | | | | | purpose = used car: bad (1.0)
| | | | | | purpose = furniture/equipment
| | | | | | | duration <= 10: bad (3.0)
| | | | | | | duration > 10
| | | | | | | | duration <= 21: good (6.0/1.0)
| | | | | | | | duration > 21: bad (2.0)
| | | | | | purpose = radio/tv: good (8.0/2.0)
| | | | | | purpose = domestic appliance: good (0.0)
| | | | | | purpose = repairs: good (1.0)
| | | | | | purpose = education: good (4.0/2.0)
| | | | | | purpose = vacation: good (0.0)
| | | | | | purpose = retraining: good (0.0)

DWDM/IS LAB Page 56


WWW.VIDYARTHIPLUS.COM

| | | | | | purpose = business
| | | | | | | residence_since <= 2: good (3.0)
| | | | | | | residence_since > 2: bad (2.0)
| | | | | | purpose = other: good (0.0)
| | | | | personal_status = male single: good (52.0/15.0)
| | | | | personal_status = male mar/wid
| | | | | | duration <= 10: good (6.0)
| | | | | | duration > 10: bad (10.0/3.0)
| | | | | personal_status = female single: good (0.0)
| | | | duration > 42: bad (7.0)
| | | other_parties = co applicant: good (2.0)
| | | other_parties = guarantor
| | | | purpose = new car: bad (2.0)
| | | | purpose = used car: good (0.0)
| | | | purpose = furniture/equipment: good (0.0)
| | | | purpose = radio/tv: good (18.0/1.0)
| | | | purpose = domestic appliance: good (0.0)
| | | | purpose = repairs: good (0.0)
| | | | purpose = education: good (0.0)
| | | | purpose = vacation: good (0.0)
| | | | purpose = retraining: good (0.0)
| | | | purpose = business: good (0.0)
| | | | purpose = other: good (0.0)
| | savings_status = 100<=X<500
| | | purpose = new car: bad (15.0/5.0)
| | | purpose = used car: good (3.0)
| | | purpose = furniture/equipment: bad (4.0/1.0)
| | | purpose = radio/tv: bad (8.0/2.0)
| | | purpose = domestic appliance: good (0.0)
| | | purpose = repairs: good (2.0)
| | | purpose = education: good (0.0)
| | | purpose = vacation: good (0.0)
| | | purpose = retraining: good (0.0)
| | | purpose = business
| | | | housing = rent
| | | | | existing_credits <= 1: good (2.0)
| | | | | existing_credits > 1: bad (2.0)
| | | | housing = own: good (6.0)
| | | | housing = for free: bad (1.0)
| | | purpose = other: good (1.0)
| | savings_status = 500<=X<1000: good (11.0/3.0)
| | savings_status = >=1000: good (13.0/3.0)

DWDM/IS LAB Page 57


WWW.VIDYARTHIPLUS.COM

| | savings_status = no known savings: good (41.0/5.0)


| credit_amount > 9857: bad (20.0/3.0)
checking_status = >=200: good (63.0/14.0)
checking_status = no checking: good (394.0/46.0)

Number of Leaves : 103

Size of the tree : 140

Time taken to build model: 0.03 seconds

=== Evaluation on training set ===


=== Summary ===
Correctly Classified Instances 855 85.5 %
Incorrectly Classified Instances 145 14.5 %
Kappa statistic 0.6251
Mean absolute error 0.2312
Root mean squared error 0.34
Relative absolute error 55.0377 %
Root relative squared error 74.2015 %
Total Number of Instances 1000

TASK 4: Suppose you use your above model trained on the complete dataset, and
classify credit good/bad for each of the examples in the dataset. What % of examples
can you classify correctly? (This is also called testing on the training set) Why do you
think you cannot get 100 % training accuracy?

In the above model we trained complete dataset and we classified credit good/bad for each of
the examples in the dataset.

For example:
IF
purpose=vacation THEN
credit=bad
ELSE
purpose=business THEN
Credit=good

In this way we classified each of the examples in the dataset.


We classified 85.5% of examples correctly and the remaining 14.5% of examples are

DWDM/IS LAB Page 58


WWW.VIDYARTHIPLUS.COM

incorrectly classified. We can’t get 100% training accuracy because out of the 20 attributes,
we have some unnecessary attributes which are also been analyzed and trained.
Due to this the accuracy is affected and hence we can’t get 100% training accuracy.

Task 5: Is testing on the training set as you did above a good idea? Why or Why not?
According to the rules, for the maximum accuracy, we have to take 2/3 of the dataset
as training set and the remaining 1/3 as test set. But here in the above model we have
taken complete dataset as training set which results only 85.5% accuracy.

This is done for the analyzing and training of the unnecessary attributes which does not make
a crucial role in credit risk assessment. And by this complexity is increasing and finally it
leads to the minimum accuracy.
If some part of the dataset is used as a training set and the remaining as test set then it
leads to the accurate results and the time for computation will be less.

This is why, we prefer not to take complete dataset as training set.

Task 6: One approach for solving the problem encountered in the previous question is
using cross-validation? Describe what cross-validation is briefly. Train a Decision Tree
again using cross-validation and report your results. Does your accuracy
increase/decrease? Why?

Cross validation:-

In k-fold cross-validation, the initial data are randomly portioned into ‘k’ mutually
exclusive subsets or folds D1, D2, D3, . . . . . ., Dk. Each of approximately equal size.
Training and testing is performed ‘k’ times. In iteration I, partition Di is reserved as the test
set and the remaining partitions are collectively used to train the model. That is in the first
iteration subsets D2, D3, . . . . . ., Dk collectively serve as the training set in order to obtain as
first model. Which is tested on Di. The second trained on the subsets D1, D3, . . . . . ., Dk and
test on the D2 and so on....

J48 pruned tree :-


------------------
checking_status = <0
| foreign_worker = yes
| | duration <= 11
| | | existing_credits <= 1
| | | | property_magnitude = real estate: good (8.0/1.0)
| | | | property_magnitude = life insurance
| | | | | own_telephone = none: bad (2.0)
| | | | | own_telephone = yes: good (4.0)

DWDM/IS LAB Page 59


WWW.VIDYARTHIPLUS.COM

| | | | property_magnitude = car: good (2.0/1.0)


| | | | property_magnitude = no known property: bad (3.0)
| | | existing_credits > 1: good (14.0)
| | duration > 11
| | | job = unemp/unskilled non res: bad (5.0/1.0)
| | | job = unskilled resident
| | | | purpose = new car
| | | | | own_telephone = none: bad (10.0/2.0)
| | | | | own_telephone = yes: good (2.0)
| | | | purpose = used car: bad (1.0)
| | | | purpose = furniture/equipment
| | | | | employment = unemployed: good (0.0)
| | | | | employment = <1: bad (3.0)
| | | | | employment = 1<=X<4: good (4.0)
| | | | | employment = 4<=X<7: good (1.0)
| | | | | employment = >=7: good (2.0)
| | | | purpose = radio/tv
| | | | | existing_credits <= 1: bad (10.0/3.0)
| | | | | existing_credits > 1: good (2.0)
| | | | purpose = domestic appliance: bad (1.0)
| | | | purpose = repairs: bad (1.0)
| | | | purpose = education: bad (1.0)
| | | | purpose = vacation: bad (0.0)
| | | | purpose = retraining: good (1.0)
| | | | purpose = business: good (3.0)
| | | | purpose = other: good (1.0)
| | | job = skilled
| | | | other_parties = none
| | | | | duration <= 30
| | | | | | savings_status = <100
| | | | | | | credit_history = no credits/all paid: bad (8.0/1.0)
| | | | | | | credit_history = all paid: bad (6.0)
| | | | | | | credit_history = existing paid
| | | | | | | | own_telephone = none
| | | | | | | | | existing_credits <= 1
| | | | | | | | | | property_magnitude = real estate
| | | | | | | | | | | age <= 26: bad (5.0)
| | | | | | | | | | | age > 26: good (2.0)
| | | | | | | | | | property_magnitude = life insurance: bad (7.0/2.0)
| | | | | | | | | | property_magnitude = car
| | | | | | | | | | | credit_amount <= 1386: bad (3.0)
| | | | | | | | | | | credit_amount > 1386: good (11.0/1.0)

DWDM/IS LAB Page 60


WWW.VIDYARTHIPLUS.COM

| | | | | | | | | | property_magnitude = no known property: good (2.0)


| | | | | | | | | existing_credits > 1: bad (3.0)
| | | | | | | | own_telephone = yes: bad (5.0)
| | | | | | | credit_history = delayed previously: bad (4.0)
| | | | | | | credit_history = critical/other existing credit: good (14.0/4.0)
| | | | | | savings_status = 100<=X<500
| | | | | | | credit_history = no credits/all paid: good (0.0)
| | | | | | | credit_history = all paid: good (1.0)
| | | | | | | credit_history = existing paid: bad (3.0)
| | | | | | | credit_history = delayed previously: good (0.0)
| | | | | | | credit_history = critical/other existing credit: good (2.0)
| | | | | | savings_status = 500<=X<1000: good (4.0/1.0)
| | | | | | savings_status = >=1000: good (4.0)
| | | | | | savings_status = no known savings
| | | | | | | existing_credits <= 1
| | | | | | | | own_telephone = none: bad (9.0/1.0)
| | | | | | | | own_telephone = yes: good (4.0/1.0)
| | | | | | | existing_credits > 1: good (2.0)
| | | | | duration > 30: bad (30.0/3.0)
| | | | other_parties = co applicant: bad (7.0/1.0)
| | | | other_parties = guarantor: good (12.0/3.0)
| | | job = high qualif/self emp/mgmt: good (30.0/8.0)
| foreign_worker = no: good (15.0/2.0)
checking_status = 0<=X<200
| credit_amount <= 9857
| | savings_status = <100
| | | other_parties = none
| | | | duration <= 42
| | | personal_status = male div/sep: bad (8.0/2.0)
| | | personal_status = female div/dep/mar
| | | | purpose = new car: bad (5.0/1.0)
| | | | purpose = used car: bad (1.0)
| | | | purpose = furniture/equipment
| | | | | duration <= 10: bad (3.0)
| | | | | duration > 10
| | | | | | duration <= 21: good (6.0/1.0)
| | | | | | duration > 21: bad (2.0)
| | | | purpose = radio/tv: good (8.0/2.0)
| | | | purpose = domestic appliance: good (0.0)
| | | | purpose = repairs: good (1.0)
| | | | purpose = education: good (4.0/2.0)
| | | | purpose = vacation: good (0.0)

DWDM/IS LAB Page 61


WWW.VIDYARTHIPLUS.COM

| | | | purpose = retraining: good (0.0)


| | | | purpose = business
| | | | | residence_since <= 2: good (3.0)
| | | | | residence_since > 2: bad (2.0)
| | | | purpose = other: good (0.0)
| | | personal_status = male single: good (52.0/15.0)
| | | personal_status = male mar/wid
| | | | duration <= 10: good (6.0)
| | | | duration > 10: bad (10.0/3.0)
| | | personal_status = female single: good (0.0)
| | duration > 42: bad (7.0)
| other_parties = co applicant: good (2.0)
| other_parties = guarantor
| | purpose = new car: bad (2.0)
| | purpose = used car: good (0.0)
| | purpose = furniture/equipment: good (0.0)
| | purpose = radio/tv: good (18.0/1.0)
| | purpose = domestic appliance: good (0.0)
| | purpose = repairs: good (0.0)
| | purpose = education: good (0.0)
| | purpose = vacation: good (0.0)
| | purpose = retraining: good (0.0)
| | purpose = business: good (0.0)
| | purpose = other: good (0.0)
savings_status = 100<=X<500
| purpose = new car: bad (15.0/5.0)
| purpose = used car: good (3.0)
| purpose = furniture/equipment: bad (4.0/1.0)
| purpose = radio/tv: bad (8.0/2.0)
| purpose = domestic appliance: good (0.0)
| purpose = repairs: good (2.0)
| purpose = education: good (0.0)
| purpose = vacation: good (0.0)
| purpose = retraining: good (0.0)
| | | purpose = business
| | | | housing = rent
| | | | | existing_credits <= 1: good (2.0)
| | | | | existing_credits > 1: bad (2.0)
| | | | housing = own: good (6.0)
| | | | housing = for free: bad (1.0)
| | | purpose = other: good (1.0)
| | savings_status = 500<=X<1000: good (11.0/3.0)

DWDM/IS LAB Page 62


WWW.VIDYARTHIPLUS.COM

| | savings_status = >=1000: good (13.0/3.0)


| | savings_status = no known savings: good (41.0/5.0)
| credit_amount > 9857: bad (20.0/3.0)
checking_status = >=200: good (63.0/14.0)
checking_status = no checking: good (394.0/46.0)

Number of Leaves : 103

Size of the tree : 140

Time taken to build model: 0.07 seconds

=== Stratified cross-validation ===


=== Summary ===
Correctly Classified Instances 705 70.5 %
Incorrectly Classified Instances 295 29.5 %
Kappa statistic 0.2467
Mean absolute error 0.3467
Root mean squared error 0.4796
Relative absolute error 82.5233 %
Root relative squared error 104.6565 %
Total Number of Instances 1000

Task 7: Check to see if the data shows a bias against “foreign works”(attribute 20) or
personal status(attribute 9).one way to do this is to remove these attributes from the
dataset and see if decision tree is created in those cases is significantly different from
the full dataset
case which you have already done.To remove an attribute you can use the preprocess
tab in weka's GUI Explorer.Did removing this attribute have any significant
effect?Discuss.

Aim: To create a Decision tree by training data set using Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure:

1) Open Weka GUI Chooser.

2) Select EXPLORER present in Applications.

3) Select Preprocess Tab.

DWDM/IS LAB Page 63


WWW.VIDYARTHIPLUS.COM

4) Select the Attributes which you want remove.

5) Click on Remove button.

4) Go to OPEN file and browse the file that is already stored in the system “credit-g.arff”.

5) Go to Classify tab.

6) Here the c4.5 algorithm has been chosen which is entitled as j48 in Java and can
be selected by clicking the button choose

7) and select tree j48

9) Select Test options “Use training set”

10) Click Start .

11)now we can see the output details in the Classifier output.

12.Right click on the result list and select ” visualize tree “option .

OUTPUT:-
J48 pruned tree
------------------
checking_status = <0
| foreign_worker = yes
| | duration <= 11
| | | existing_credits <= 1
| | | | property_magnitude = real estate: good (8.0/1.0)
| | | | property_magnitude = life insurance
| | | | | own_telephone = none: bad (2.0)
| | | | | own_telephone = yes: good (4.0)
| | | | property_magnitude = car: good (2.0/1.0)
| | | | property_magnitude = no known property: bad (3.0)
| | | existing_credits > 1: good (14.0)
| | duration > 11
| | | job = unemp/unskilled non res: bad (5.0/1.0)
| | | job = unskilled resident
| | | | purpose = new car
| | | | | own_telephone = none: bad (10.0/2.0)
| | | | | own_telephone = yes: good (2.0)
| | | | purpose = used car: bad (1.0)
| | | | purpose = furniture/equipment
| | employment = unemployed: good (0.0)
| | employment = <1: bad (3.0)

DWDM/IS LAB Page 64


WWW.VIDYARTHIPLUS.COM

| | employment = 1<=X<4: good (4.0)


| | employment = 4<=X<7: good (1.0)
| | employment = >=7: good (2.0)
| purpose = radio/tv
| | existing_credits <= 1: bad (10.0/3.0)
| | existing_credits > 1: good (2.0)
| purpose = domestic appliance: bad (1.0)
| purpose = repairs: bad (1.0)
| purpose = education: bad (1.0)
| purpose = vacation: bad (0.0)
| purpose = retraining: good (1.0)
| purpose = business: good (3.0)
| purpose = other: good (1.0)
job = skilled
| other_parties = none
| | duration <= 30
| | | savings_status = <100
| | | | credit_history = no credits/all paid: bad (8.0/1.0)
| | | | credit_history = all paid: bad (6.0)
| | | | credit_history = existing paid
| | | | | own_telephone = none
| | | | | | existing_credits <= 1
| | | | | | | property_magnitude = real estate
| | | | | | | | age <= 26: bad (5.0)
| | | | | | | | age > 26: good (2.0)
| | | | | | | property_magnitude = life insurance: bad (7.0/2.0)
| | | | | | | property_magnitude = car
| | | | | | | | credit_amount <= 1386: bad (3.0)
| | | | | | | | credit_amount > 1386: good (11.0/1.0)
| | | | | | | property_magnitude = no known property: good (2.0)
| | | | | | existing_credits > 1: bad (3.0)
| | | | | own_telephone = yes: bad (5.0)
| | | | credit_history = delayed previously: bad (4.0)
| | | | credit_history = critical/other existing credit: good (14.0/4.0)
| | | savings_status = 100<=X<500
| | | | credit_history = no credits/all paid: good (0.0)
| | | | credit_history = all paid: good (1.0)
| | | | credit_history = existing paid: bad (3.0)
| | | | credit_history = delayed previously: good (0.0)
| | | | credit_history = critical/other existing credit: good (2.0)
| | | savings_status = 500<=X<1000: good (4.0/1.0)
| | | savings_status = >=1000: good (4.0)

DWDM/IS LAB Page 65


WWW.VIDYARTHIPLUS.COM

| | | savings_status = no known savings


| | | | existing_credits <= 1
| | | | | own_telephone = none: bad (9.0/1.0)
| | | | | own_telephone = yes: good (4.0/1.0)
| | | | | | | existing_credits > 1: good (2.0)
| | | | | duration > 30: bad (30.0/3.0)
| | | | other_parties = co applicant: bad (7.0/1.0)
| | | | other_parties = guarantor: good (12.0/3.0)
| | | job = high qualif/self emp/mgmt: good (30.0/8.0)
| foreign_worker = no: good (15.0/2.0)
checking_status = 0<=X<200
| credit_amount <= 9857
| | savings_status = <100
| | | other_parties = none
| | | | duration <= 42
| | | | | personal_status = male div/sep: bad (8.0/2.0)
| | | | | personal_status = female div/dep/mar
| | | | | | purpose = new car: bad (5.0/1.0)
| | | | | | purpose = used car: bad (1.0)
| | | | | | purpose = furniture/equipment
| | | | | | | duration <= 10: bad (3.0)
| | | | | | | duration > 10
| | | | | | | | duration <= 21: good (6.0/1.0)
| | | | | | | | duration > 21: bad (2.0)
| | | | | | purpose = radio/tv: good (8.0/2.0)
| | | | | | purpose = domestic appliance: good (0.0)
| | | | | | purpose = repairs: good (1.0)
| | | | | | purpose = education: good (4.0/2.0)
| | | | | | purpose = vacation: good (0.0)
| | | | | | purpose = retraining: good (0.0)
| | | | | | purpose = business
| | | | | | | residence_since <= 2: good (3.0)
| | | | | | | residence_since > 2: bad (2.0)
| | | | | | purpose = other: good (0.0)
| | | | | personal_status = male single: good (52.0/15.0)
| | | | | personal_status = male mar/wid
| | | | | | duration <= 10: good (6.0)
| | | | | | duration > 10: bad (10.0/3.0)
| | | | | personal_status = female single: good (0.0)
| | | | duration > 42: bad (7.0)
| | | other_parties = co applicant: good (2.0)
| | | other_parties = guarantor

DWDM/IS LAB Page 66


WWW.VIDYARTHIPLUS.COM

| | | | purpose = new car: bad (2.0)


| | | | purpose = used car: good (0.0)
| | | | purpose = furniture/equipment: good (0.0)
| | | | purpose = radio/tv: good (18.0/1.0)
| | | | purpose = domestic appliance: good (0.0)
| | | | purpose = repairs: good (0.0)
| | | | purpose = education: good (0.0)
| | | | purpose = vacation: good (0.0)
| | | | purpose = retraining: good (0.0)
| | | | purpose = business: good (0.0)
| | | | purpose = other: good (0.0)
| | savings_status = 100<=X<500
| | | purpose = new car: bad (15.0/5.0)
| | | purpose = used car: good (3.0)
| | | purpose = furniture/equipment: bad (4.0/1.0)
| | | purpose = radio/tv: bad (8.0/2.0)
| | | purpose = domestic appliance: good (0.0)
| | | purpose = repairs: good (2.0)
| | | purpose = education: good (0.0)
| | | purpose = vacation: good (0.0)
| | | purpose = retraining: good (0.0)
| | | purpose = business
| | | | housing = rent
| | | | | existing_credits <= 1: good (2.0)
| | | | | existing_credits > 1: bad (2.0)
| | | | housing = own: good (6.0)
| | | | housing = for free: bad (1.0)
| | | purpose = other: good (1.0)
| | savings_status = 500<=X<1000: good (11.0/3.0)
| | savings_status = >=1000: good (13.0/3.0)
| | savings_status = no known savings: good (41.0/5.0)
| credit_amount > 9857: bad (20.0/3.0)
checking_status = >=200: good (63.0/14.0)
checking_status = no checking: good (394.0/46.0)

Number of Leaves : 103

Size of the tree : 140

Time taken to build model: 0.03 seconds

=== Evaluation on training set ===

DWDM/IS LAB Page 67


WWW.VIDYARTHIPLUS.COM

=== Summary ===


Correctly Classified Instances 865 86.5 %
Incorrectly Classified Instances 145 14.5 %
Kappa statistic 0.6251
Mean absolute error 0.2312
Root mean squared error 0.34
Relative absolute error 55.0377 %
Root relative squared error 74.2015 %
Total Number of Instances 1000
By removing these attributes efficiency increases.because these attributes does not play
important role in training data set.

Task 8: Another question might be, do you really need to input so many attributes to get
good results? Maybe only a few would do. For example, you could try just having
attributes 2, 3, 5, 7, 10, 17 (and 21, the class attribute (naturally)). Try out some
combinations. (You had removed two attributes in problem 7. Remember to reload the
arff data file to get all the attributes initially before you start selecting the ones you
want.)

=== Classifier model (full training set) ===


J48 pruned tree
------------------
credit_history = no credits/all paid: bad (40.0/15.0)
credit_history = all paid
| employment = unemployed
| | duration <= 36: bad (3.0)
| | duration > 36: good (2.0)
| employment = <1
| | duration <= 26: bad (7.0/1.0)
| | duration > 26: good (2.0)
| employment = 1<=X<4: good (15.0/6.0)
| employment = 4<=X<7: bad (10.0/4.0)
| employment = >=7
| | job = unemp/unskilled non res: bad (0.0)
| | job = unskilled resident: good (3.0)
| | job = skilled: bad (3.0)
| | job = high qualif/self emp/mgmt: bad (4.0)
credit_history = existing paid
| credit_amount <= 8648
| | duration <= 40: good (476.0/130.0)
| | duration > 40: bad (27.0/8.0)
| credit_amount > 8648: bad (27.0/7.0)

DWDM/IS LAB Page 68


WWW.VIDYARTHIPLUS.COM

credit_history = delayed previously


| employment = unemployed
| | credit_amount <= 2186: bad (4.0/1.0)
| | credit_amount > 2186: good (2.0)
| employment = <1
| | duration <= 18: good (2.0)
| | duration > 18: bad (10.0/2.0)
| employment = 1<=X<4: good (33.0/6.0)
| employment = 4<=X<7
| | credit_amount <= 4530
| | | credit_amount <= 1680: good (3.0)
| | | credit_amount > 1680: bad (3.0)
| | credit_amount > 4530: good (11.0)
| employment >=7
| | job = unemp/unskilled non res: good (0.0)
| | job = unskilled resident: good (2.0/1.0)
| | job = skilled: good (14.0/4.0)
| | job = high qualif/self emp/mgmt: bad (4.0/1.0)
credit_history = critical/other existing credit: good (293.0/50.0)

Number of Leaves : 27

Size of the tree : 40

=== Classifier model (full training set) ===


J48 pruned tree
------------------
credit_history = no credits/all paid: bad (40.0/15.0)

DWDM/IS LAB Page 69


WWW.VIDYARTHIPLUS.COM

credit_history = all paid


| employment = unemployed
| | duration <= 36: bad (3.0)
| | duration > 36: good (2.0)
| employment = <1
| | duration <= 26: bad (7.0/1.0)
| | duration > 26: good (2.0)
| employment = 1<=X<4: good (15.0/6.0)
| employment = 4<=X<7: bad (10.0/4.0)
| employment = >=7
| | job = unemp/unskilled non res: bad (0.0)
| | job = unskilled resident: good (3.0)
| | job = skilled: bad (3.0)
| | job = high qualif/self emp/mgmt: bad (4.0)
credit_history = existing paid
| credit_amount <= 8648
| | duration <= 40: good (476.0/130.0)
| | duration > 40: bad (27.0/8.0)
| credit_amount > 8648: bad (27.0/7.0)
credit_history = delayed previously
| employment = unemployed
| | credit_amount <= 2186: bad (4.0/1.0)
| | credit_amount > 2186: good (2.0)
| employment = <1
| | duration <= 18: good (2.0)
| | duration > 18: bad (10.0/2.0)
| employment = 1<=X<4: good (33.0/6.0)
| employment = 4<=X<7
| | credit_amount <= 4530
| | | credit_amount <= 1680: good (3.0)
| | | credit_amount > 1680: bad (3.0)
| | credit_amount > 4530: good (11.0)
| employment >=7
| | job = unemp/unskilled non res: good (0.0)
| | job = unskilled resident: good (2.0/1.0)
| | job = skilled: good (14.0/4.0)
| | job = high qualif/self emp/mgmt: bad (4.0/1.0)
credit_history = critical/other existing credit: good (293.0/50.0)

Number of Leaves : 27

Size of the tree : 40

DWDM/IS LAB Page 70


WWW.VIDYARTHIPLUS.COM

Time taken to build model: 0.01 seconds

=== Stratified cross-validation ===


=== Summary ===

Task 9: Sometimes, the cost of rejecting an applicant who actually has a good credit
(case 1) might be higher than accepting an applicant who has bad credit (case 2).
Instead of counting the misclassifications equally in both cases, give a higher cost to the
first case (say cost 5) and lower cost to the second case. You can do this by using a cost
matrix in Weka. Train your Decision Tree again and report the Decision Tree and
cross-validation results. Are they significantly different from results obtained in
problem 6 (using equal cost)?

DWDM/IS LAB Page 71


WWW.VIDYARTHIPLUS.COM

TASK 10: Do you think it is a good idea to prefer simple decision trees instead of
having long complex decision trees? How does the complexity of a Decision Tree relate
to the bias of the model?

When we consider long complex decision trees, we will have many unnecessary attributes in
the tree which results in increase of the bias of the model. Because of this, the accuracy of the
model can also effected.

This problem can be reduced by considering simple decision tree. The attributes will be less
and it decreases the bias of the model. Due to this the result will be more accurate.So it is a
good idea to prefer simple decision trees instead of long complex trees.

Task 11: You can make your Decision Trees simpler by pruning the nodes. One
approach is to use Reduced Error Pruning - Explain this idea briefly. Try reduced error
pruning for training your Decision Trees using cross-validation (you can do this in
Weka) and report the Decision Tree you obtain ? Also, report your accuracy using the
pruned model. Does your accuracy increase ?
Aim: To create a Decision tree by cross validation training data set using Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure:

1) Given the Bank database for mining.

2) Use the Weka GUI Chooser.

3) Select EXPLORER present in Applications.

4) Select Preprocess Tab.

5) Go to OPEN file and browse the file that is already stored in the system “credit-g.arff”.

6) Go to Classify tab.

7) Choose Classifier “Tree”

8) Select J48

9) Select Test options “Cross-validation”.

DWDM/IS LAB Page 72


WWW.VIDYARTHIPLUS.COM

10) Set “Folds” Ex:10

11) if need select attribute.

12) now Start weka.

13)now we can see the output details in the Classifier output.

14)Compare the output results with that of the 4th experiment

15) check whether the accuracy increased or decreased?

OUTPUT:-

Reduced-error pruning :-
The idea of using a separate pruning set for pruning—which is applicable to
decision trees as well as rule sets—is called reduced-error pruning. The variant described
previously prunes a rule immediately after it has been grown and is called incremental
reduced-error pruning.

Another possibility is to build a full, unpruned rule set first, pruning it afterwards by
discarding individual tests.

However, this method is much slower. Of course, there are many different ways to assess the
worth of a rule based on the pruning set. A simple measure is to consider how well the rule
would do at discriminating the predicted class from other classes if it were the only rule in the
theory, operating under the closed world assumption. If it gets p instances right out of the t
instances that it covers, and there are P instances of this class out of a total T of instances
altogether, then it gets p positive instances right. The instances that it does not cover include
N - n negative ones, where n = t – p
is the number of negative instances that the rule covers and N = T - P is the total number of
negative instances.
Thus the rule has an overall success ratio of [p +(N - n)] T , and this quantity, evaluated on
the test set, has been used to evaluate the success of a rule when using reduced-error pruning.

J48 pruned tree


------------------
checking_status = <0
| foreign_worker = yes
| | credit_history = no credits/all paid: bad (11.0/3.0)
| | credit_history = all paid: bad (9.0/1.0)
| | credit_history = existing paid
| | | other_parties = none
| | | | savings_status = <100

DWDM/IS LAB Page 73


WWW.VIDYARTHIPLUS.COM

| | | | | existing_credits <= 1
| | | | | | purpose = new car: bad (17.0/4.0)
| | | | | | purpose = used car: good (3.0/1.0)
| | | | | | purpose = furniture/equipment: good (22.0/11.0)
| | | | | | purpose = radio/tv: good (18.0/8.0)
| | | | | | purpose = domestic appliance: bad (2.0)
| | | | | | purpose = repairs: bad (1.0)
| | | | | | purpose = education: bad (5.0/1.0)
| | | | | | purpose = vacation: bad (0.0)
| | | | | | purpose = retraining: bad (0.0)
| | | | | | purpose = business: good (3.0/1.0)
| | | | | | purpose = other: bad (0.0)
| | | | | existing_credits > 1: bad (5.0)
| | | | savings_status = 100<=X<500: bad (8.0/3.0)
| | | | savings_status = 500<=X<1000: good (1.0)
| | | | savings_status = >=1000: good (2.0)
| | | | savings_status = no known savings
| | | | | job = unemp/unskilled non res: bad (0.0)
| | | | | job = unskilled resident: good (2.0)
| | | | | job = skilled
| | | | | | own_telephone = none: bad (4.0)
| | | | | | own_telephone = yes: good (3.0/1.0)
| | | | | job = high qualif/self emp/mgmt: bad (3.0/1.0)
| | | other_parties = co applicant: good (4.0/2.0)
| | | other_parties = guarantor: good (8.0/1.0)
| | credit_history = delayed previously: bad (7.0/2.0)
| | credit_history = critical/other existing credit: good (38.0/10.0)
| foreign_worker = no: good (12.0/2.0)
checking_status = 0<=X<200
| other_parties = none
| | credit_history = no credits/all paid
| | | other_payment_plans = bank: good (2.0/1.0)
| | | other_payment_plans = stores: bad (0.0)
| | | other_payment_plans = none: bad (7.0)
| | credit_history = all paid: bad (10.0/4.0)
| | credit_history = existing paid
| | | credit_amount <= 8858: good (70.0/21.0)
| | | credit_amount > 8858: bad (8.0)
| | credit_history = delayed previously: good (25.0/6.0)
| | credit_history = critical/other existing credit: good (26.0/7.0)
| other_parties = co applicant: bad (7.0/1.0)
| other_parties = guarantor: good (18.0/4.0)

DWDM/IS LAB Page 74


WWW.VIDYARTHIPLUS.COM

checking_status = >=200: good (44.0/9.0)


checking_status = no checking
| other_payment_plans = bank: good (30.0/10.0)
| other_payment_plans = stores: good (12.0/2.0)
| other_payment_plans = none
| | credit_history = no credits/all paid: good (4.0)
| | credit_history = all paid: good (1.0)
| | credit_history = existing paid
| | | existing_credits <= 1: good (92.0/7.0)
| | | existing_credits > 1
| | | | installment_commitment <= 2: bad (4.0/1.0)
| | | | installment_commitment > 2: good (5.0)
| | credit_history = delayed previously: good (22.0/6.0)
| | credit_history = critical/other existing credit: good (92.0/3.0)

Number of Leaves : 47

Size of the tree : 64

Time taken to build model: 0.49 seconds

=== Stratified cross-validation ===


=== Summary ===

Task 12: (Extra Credit): How can you convert a Decision Trees into "if-then-else
rules". Make up your own small Decision Tree consisting of 2-3 levels and convert it
into a set of rules. There also exist different classifiers that output the model in the
form of rules - one such classifier in Weka is rules.PART, train this model and report
the set of rules obtained. Sometimes just one attribute can be good enough in making
the decision, yes, just one ! Can you predict what attribute that might be in this dataset?
OneR classifier uses a single attribute to make decisions (it chooses the attribute based

DWDM/IS LAB Page 75


WWW.VIDYARTHIPLUS.COM

on minimum error). Report the rule obtained by training a one R classifier. Rank the
performance of j48, PART and oneR.

Aim: To compare OneR classifier which uses single attribute and rule with J48 and PART
classifier’s, by training data set using Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure for “PART”:

1) Given the German Credit database for mining.

2) Use the Weka GUI Chooser.

3) Select EXPLORER present in Applications.

4) Select Preprocess Tab.

5) Go to OPEN file and browse the file that is already stored in the system “credit-.arff”.

6) select some of the attributes from attributes list

7) Go to Classify tab.

8) Choose Classifier “Rules”

9) Select “PART” .

10) Select Test options “Use training set”

11) if need select attribute.

12) now Start weka.

13)now we can see the output details in the Classifier output.

OUTPUT:-
In weka, rules.PART is one of the classifier which converts the decision trees into
“IF-THEN-ELSE” rules.

Converting Decision trees into “IF-THEN-ELSE” rules using rules.PART classifier:-


PART decision list

outlook = overcast: yes (4.0)


windy = TRUE: no (4.0/1.0)
outlook = sunny: no (3.0/1.0)
: yes (3.0)

DWDM/IS LAB Page 76


WWW.VIDYARTHIPLUS.COM

Number of Rules : 4

Yes, sometimes just one attribute can be good enough in making the decision.
In this dataset (Weather), Single attribute for making the decision is “outlook”

outlook:
sunny -> no
overcast -> yes
rainy -> yes

(10/14 instances correct)

With respect to the time, the oneR classifier has higher ranking and J48 is in 2nd place and
PART gets 3rd place.

DWDM/IS LAB Page 77


WWW.VIDYARTHIPLUS.COM

TASK 2: Hospital management system

Q.) Design Hospital management system data warehouse using all schemes.

“Data warehouse is a subject oriented, integrated, timevariant, nonvolatile collection of data in


decision making process”.

Data warehouse consists of dimension table and fact table. Fact table contains bulk of data with no
redundancy. Dimension table contains smaller attendant tables one for each dimension object
having name attributes, primary key. Dimension consist of a set of levels and a set of hierarchies
defined over those levels. The levels represent levels of aggregation hierarchies describe parent child
relationships among a set of levels.

Consider calendar dimension could contain five levels. two hierarchies can be defined on these
levels.

H1: yearL>quarterL>monthL>weekL>dayL

H2: yearL>weekL>dayL

These hierarchies are described from parent to child,so that year is the parent of the quarter,quarter
is the parent of month.

Design a hospital management system data warehouse(TARGET) consists of dimensions


patient,medicine,supplier,time.where measures are 'NO UNITS','unit price'.

PATIENT(patient-name,Age,address,token number,sick type)

MEDICINE( medicine-brand-name,drug-name,medicine-brand-name,Address)

SUPPLIER( supplier-name,medicine-brand-name,address)

Hospital management system datawarehouse using all schemes

Star Scheme:

DWDM/IS LAB Page 78


WWW.VIDYARTHIPLUS.COM

Star scheme is a simplest among all schemes.it consists of fact table and dimension tables.

Fig:star scheme for hospital management system

Fact table: Hospital management system

Dimension tables: Medicine ,patient,time,supplier

DWDM/IS LAB Page 79


WWW.VIDYARTHIPLUS.COM

Snow flake scheme:

Snowflake is a same as star scheme but in snowflake scheme dimension tables are normalized in
to additional tables.

Fig:Snowflake scheme for hospital management system

Fact table: Hospital management system

Dimension tables: Medicine ,patient,time,supplier,city

Fact constellation scheme:

DWDM/IS LAB Page 80


WWW.VIDYARTHIPLUS.COM

Fact constellation is also called as galaxy scheme.More than one fact table is involved in this
scheme.Dimension tables are shared among fact tables.

Fig: Fact constellation scheme for hospital and pharmacy company

Fact table: Hospital management system,pharmacy company

Dimension tables: Medicine ,patient,time,supplier,distributor

DWDM/IS LAB Page 81


WWW.VIDYARTHIPLUS.COM

Introduction to Clementine Data mining system

Clementine is a mature data mining toolkit which aims to allow domain experts (normal
users) to do their own data mining.
Clementine has a visual programming or data flow interface, which simplifies the data
mining process.
Clementine applications include customer segmentation/profiling for marketing companies,
fraud detection, credit scoring, load forecasting for utility companies, and profit prediction
for retailers.

Q.Market Basket Analysis - Representation of product and price used in basket analysis using
climentine software?

Output:

In market basket analysis used for knowing buying habits of our customers.In this program
represent the product and related price using climentine software.

Q.How to visualize the data mining process using Clementine?

Clementine has a visual programming or data flow interface, which simplifies the
data mining process. By using Clementine process the data mining processes.

1.Acess the input data

2.prepare data and model

3.Deploy

4.Visualize

DWDM/IS LAB Page 82


WWW.VIDYARTHIPLUS.COM

DWDM/IS LAB Page 83

You might also like