DWDM & IS Lab Manual - III
DWDM & IS Lab Manual - III
COM
#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
int main()
{
char *message,*emessage,*dmessage;
int i,j=0,k,key,temp;
clrscr();
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
{
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]);
getch();
return(0);
}
OUTPUT:
Encrypted message is
Khoor
Retrived message is
Hello
#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];
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++;
dg[h/2][1]=dg[h/2][0];
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:
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)
goto x;
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)
ct[m]=ip[k];
m++;
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;
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];
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]) ;
printf("\n");
return 0;
OUTPUT:
welcometolab
we lc om et ol ab
3211
3 2
1 1
walnoayxmzcb
welcometolab
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
char *bin[]={
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};
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",
"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,
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],
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];
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;
}
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");
for(i=0;i<d;i++)
{
for(j=0;j<=15;j++)
printf("%c",hex_arr[i][j]);
printf("\n");
}*/
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]);*/
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]);*/
/*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);
double_shift(C13,C14);
double_shift(D13,D14);
double_shift(C14,C15);
double_shift(D14,D15);
single_shift(C15,C16);
single_shift(D15,D16);
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);
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);
permutation_64(input_bin,L0,R0);
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);
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]);
}
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';
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);
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;
}
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++;
}
*(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;
}
}
for(i=0;i<8;i++)
{
for(j=0;j<7;j++)
{
temp=PC1[i][j]-1;
*(key_PC1+k)=*(key_bin+temp);
k++;
}
}
}
for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=PC2[i][j]-1;
*(K+m)=*(CD+temp);
m++;
}
}
}
xor(K1,ER0,F1);
/*printf("\nF1 : ");
for(i=0;i<48;i++)
printf("%c",F1[i]);*/
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;
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);
}
{
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);
}
*(L+i)=*(R+i);
}
for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=E_bit[i][j]-1;
*(ER+m)=*(R+temp);
m++;
}
}
}
*(R1+m)='1';
m++;
}
}
}
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
temp=sb_permutation[i][j]-1;
*(f+m)=*(SB1+temp);
m++;
}
}
}
OUTPUT:
#include <stdio.h>
#include <string.h>
int
main (void)
{
BLOWFISH_CTX ctx;
int n;
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
while (plaintext_len)
{
message_left = message_right = 0UL;
/* 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);
ciphertext_string = &ciphertext_buffer[0];
while(ciphertext_len)
{
message_left = message_right = 0UL;
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
#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++){
OUTPUT:
Cipher Text
:sY6vkQrWRg0fvRzbqSAYxepeBIXg4AySj7Xh3x4vDv8TBTkNiTfca7wW/dxiMMJl
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”);
display(decrypt);
}
}
OUTPUT:
ENTER PLAIN TEXT :RC4 PROGRAM
ENCRYPTED: ??-??±?µFJ|
#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;
}
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(){
int p,q,i;
int pt,ct;
int e,d,et,n,temp;
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;
ct = en_de(pt,e,n);
Cipher text : 26
plain text : 20
#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");
}
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.
#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;
.
.
.
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;
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.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
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);
}
static DigestArray h;
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);
{
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 );
}
}
abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];
abcd[1] = f;
}
}
for (p=0; p<4; p++)
h[p] += abcd[p];
os += 64;
}
return h;
}
OUTPUT:
---------------------------------------------------
MD5 ENCRYPTION ALGORITHM IN C \n\n");
----------------------------------------------------
0x8c09f08e0000f08e0000f08e0000f08e
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).
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.
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.
Procedure:
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
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.
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.
Procedure:
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
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)
| | | | | | 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)
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
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.
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....
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.
Procedure:
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
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)
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.)
Number of Leaves : 27
Number of Leaves : 27
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)?
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.
Procedure:
5) Go to OPEN file and browse the file that is already stored in the system “credit-g.arff”.
6) Go to Classify tab.
8) Select J48
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.
| | | | | 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)
Number of Leaves : 47
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
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.
5) Go to OPEN file and browse the file that is already stored in the system “credit-.arff”.
7) Go to Classify tab.
9) Select “PART” .
OUTPUT:-
In weka, rules.PART is one of the classifier which converts the decision trees into
“IF-THEN-ELSE” rules.
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
With respect to the time, the oneR classifier has higher ranking and J48 is in 2nd place and
PART gets 3rd place.
Q.) Design Hospital management system data warehouse using all schemes.
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.
MEDICINE( medicine-brand-name,drug-name,medicine-brand-name,Address)
SUPPLIER( supplier-name,medicine-brand-name,address)
Star Scheme:
Star scheme is a simplest among all schemes.it consists of fact table and dimension tables.
Snowflake is a same as star scheme but in snowflake scheme dimension tables are normalized in
to additional tables.
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.
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.
Clementine has a visual programming or data flow interface, which simplifies the
data mining process. By using Clementine process the data mining processes.
3.Deploy
4.Visualize