You are on page 1of 24

List of experiments ECS-651

1. WAP in c for CRC (Cyclic Redundancy Check). 2. C program for the Implementation Of RSA Algorithm . 3. C program for the Implementation Of Bit Stuffing 4. WAP in C language for Hamming code. 5. WAP in C for Checksum. 6. Write a Program for the Implementation Of Byte Stuffing . 7. Use the some DOS Commands in the Computer Networking . 8. WAP for calculate the LRC (Longitudinal Redundancy Check). 9. WAP in C for Encryption and Decryption the Message.

Program No.1
/* WAP for calculate the CRC (Cyclic Redundancy Check)*/ #include< stdlib.h> #include< conio.h> #include< stdio.h> void main() { int i,j,n,g,a,arr[20],gen[20],b[20],q[20],s; clrscr(); printf("Transmitter side:"); printf("\nEnter no. of data bits:"); scanf("%d",&n); printf("Enter data:"); for(i=0;i< n;i++) scanf("%d",&arr[i]); printf("Enter size of generator:"); scanf("%d",&g); do{ printf("Enter generator:"); for(j=0;j< g;j++) scanf("%d",&gen[j]); } while(gen[0]!=1); printf("\n\tThe generator matrix:"); for(j=0;j< g;j++) printf("%d",gen[j]); a=n+(g-1); printf("\n\tThe appended matrix is:"); for(i=0;i< j;++i) arr[n+i]=0; for(i=0;i< a;++i) printf("%d",arr[i]); for(i=0;i< n;++i) q[i]= arr[i]; for(i=0;i< n;++i) { if(arr[i]==0)

{ for(j=i;j< g+i;++j) arr[j] = arr[j]^0; } else { arr[i] = arr[i]^gen[0]; arr[i+1]=arr[i+1]^gen[1]; arr[i+2]=arr[i+2]^gen[2]; arr[i+3]=arr[i+3]^gen[3]; } } printf("\n\tThe CRC is :"); for(i=n;i < a;++i) printf("%d",arr[i]); s=n+a; for(i=n;i< s;i++) q[i]=arr[i]; printf("\n"); for(i=0;i< a;i++) printf("%d",q[i]); getch(); } /* Output: Transmitter side: Enter no. of data bits:8 Enter data:1 0 1 0 0 0 0 1 Enter size of generator:4 Enter generator:1 0 0 1 The generator matrix:1001 The appended matrix is:10100001000 The CRC is :111 10100001111 */

Program No. 2 /* C program for the Implementation Of RSA Algorithm */ #include< stdio.h> #include< conio.h> int phi,M,n,e,d,C,FLAG; int check() { int i; for(i=3;e%i==0 && phi%i==0;i+2) { FLAG = 1; return; } FLAG = 0; } void encrypt() { int i; C = 1; for(i=0;i< e;i++) C=C*M%n; C = C%n; printf("\n\tEncrypted keyword : %d",C); } void decrypt() { int i; M = 1; for(i=0;i< d;i++) M=M*C%n; M = M%n; printf("\n\tDecrypted keyword : %d",M); } void main() { int p,q,s; clrscr(); printf("Enter Two Relatively Prime Numbers\t: "); scanf("%d%d",&p,&q); n = p*q; phi=(p-1)*(q-1); printf("\n\tF(n)\t= %d",phi);

do { printf("\n\nEnter e\t: "); scanf("%d",&e); check(); }while(FLAG==1); d = 1; do { s = (d*e)%phi; d++; }while(s!=1); d = d-1; printf("\n\tPublic Key\t: {%d,%d}",e,n); printf("\n\tPrivate Key\t: {%d,%d}",d,n); printf("\n\nEnter The Plain Text\t: "); scanf("%d",&M); encrypt(); printf("\n\nEnter the Cipher text\t: "); scanf("%d",&C); decrypt(); getch(); } /*************** OUTPUT ***************** Enter Two Relatively Prime Numbers : 7 17 F(n) = 96 Enter e : 5 Public Key : {5,119} Private Key : {77,119} Enter The Plain Text : 19 Encrypted keyword : 66 Enter the Cipher text : 66 Decrypted keyword : 19 */

Program No. 3 /* C program for the Implementation Of Bit Stuffing */ #include<stdio.h> #include<conio.h> main() { int n=0,i,size; char ch,b[100]; FILE *ed; FILE *es; printf("enter the bit information size\n"); scanf("%d",&size); printf("enter the information in bits\n"); for(i=0;i<size;i++) { scanf("%c",&b[i]); } ed=fopen("info.txt","w+"); i=0; for(i=0;i<size;i++) { fputc(b[i],ed); } fclose(ed); ed=fopen("info.txt","r+"); if(ed==NULL) printf("error\n"); es=fopen("infoo.txt","w+"); ch=getc(ed); while(ch!=EOF) { fputc(ch,es); if(ch=='1') n++; else n=0; if(n==5) { fputc('0',es); n=0; } ch=getc(ed);

} printf("\n original data"); fclose(ed); fclose(es); ed=fopen("info.txt","rb+"); es=fopen("infoo.txt","rb+"); ch=getc(ed); while(ch!=EOF) { printf("%c",ch); ch=getc(ed); } printf("\n data after stuffing"); ch=getc(es); while(ch!=EOF) { printf("%c",ch); ch=getc(es); } fclose(ed); fclose(es); }</size;i++) </size;i++) Output: enter the bit information size: 15 enter the information in bits: 101111101111110 data after stuffing: 10111110011111010

Program No. 4 WAP in C language for Hamming code. #include<stdio.h> #include<math.h> #include<string.h> #define MAXINT 65536 #define SIZE 100 int arr[SIZE][SIZE]; // a global array , to contain table information int ele=0,ind=0; // indices to be used in above array void summ(int i) { /* The method to initialize contents of global array by power of 2's sum for each entry */ int j=0; //index no if(isPow2(i)) { /*input no i is of power of 2's form like 1,2,4,8,16...*/ arr[ele][ind]=calcPow(i); /*calculate what's the power of 2 , in the no, & store to global array*/ ind++; } else { /*input no i is NOT of power of 2's form */ j=findLowerOf2(i); /* try to find just last no , which is of form of power of 2 */ arr[ele][ind]=calcPow(j); /* again calculate the power*/ ind++; i=i-j; /* differnce in the no & the last no which is of form of power of 2 */ /*now call method recursively for the new no (i=i-j) */ summ(i); } } int isPow2(int i)

{ /*if input no is power of two retrun 1 , else 0*/ if(MAXINT % i==0) return 1; //true return 0; //false } int calcPow(int i) { /*Thism ethod returns , what is power of 2 , in a no. which is of form 2 to the power p */ /* return p , from input of format 2^p */ int count=-1; /* validate */ if(!isPow2(i)) printf("flow error... "); else while(i>0) { i/=2; count++; } return count; } int findLowerOf2(int i) { /*a function to calculate the no , JUST below i , which is power of 2 */ int count=-1; if(isPow2(i)) return pow(2,i); else while(i>0) { i/=2; count++; } return pow(2,count); } void callSumm(int i) {

/* A method to call summ() method , with assertion that all global parameters are incremented at each call to summ() */ ind=0; summ(i); arr[ele][ind++]=-1; ele++; } void dieError() { /* If failure , exit the program*/ exit(1); } int howManyTimes(int val,int a[]) { /* a method to check that how many times no val is occuring in array a[] */ int i,count=0; for(i=0;a[i]!=-1;i++) if(a[i]==val) count++; return count; } void checkInput(int argc,char str[]) { int i=0; if (argc<2) { printf("usage: filename.o 'The code string' "); printf(" ex. a.out 110110 "); dieError(); } for(i=0;i<strlen(str);i++) if(!(str[i]=='0' || str[i]=='1')) { printf("Please enter a binary string only..... "); dieError(); }

} int calr(int m) { /*Method to calculate checksum bits for given m , databits */ int r=0; for(r=0;r<=m;r++) if(m <= pow(2,r)-1-r) return r; } int isEven(int i) { return i%2==0; } int main(int argc,char *argv[]) { /* Declaretions ...*/ /* flag & index variables*/ int i,j,k=0,flag,temp; /* The output codeword container */ char coded[SIZE]; int len; //total length of coded word int m; //code bits int r; //check bits /* to associate & contain equations of checkbits */ int count[SIZE][SIZE]; /* validate input */ checkInput(argc,argv[1]); /*calculate no of check bits required n thus total length */ m=strlen(argv[1]); r=calr(m); len=m+r; /* Fill the global container , according to the size info of m,r & len */ for(i=1;i<=len;i++) callSumm(i); for(j=0,k=0;j<r;j++)

{ for(i=0,k=0;i<len;i++) if(howManyTimes(j,arr[i])) count[j][k++]=i+1; count[j][k]=-1; } /*Fill the code word....,except check bits*/ for(i=0,j=0;j<len;j++) { if(!isPow2(j+1)) coded[j]=argv[1][i++]; else coded[j]='x'; //initialize checkbits by character x } /* Now ********** Frame all equations & solve them & fill entries in coded table accordingly */ for(i=0;i<r;i++) { for(flag=0,j=1;count[i][j]!=-1;j++) { temp=count[i][j]-1; if (coded[temp]=='1') flag+=1; else if (coded[temp]=='0') flag+=0; } temp=count[i][0]-1; if (isEven(flag)) coded[temp]='0'; else coded[temp]='1'; } printf("The Hamming coded word for your data is "); for(i=0;i<len;i++) printf("%c",coded[i]); printf(" ");

} Program No.5 WAP in C for Checksum.


#include<stdio.h> #include<math.h> int sender(int b[10],int k) { int checksum,sum=0,i; printf("\n****SENDER****\n"); for(i=0;i<k;i++) sum+=b[i]; printf("SUM IS: %d",sum); checksum=~sum; printf("\nSENDER's CHECKSUM IS:%d",checksum); return checksum; } int receiver(int c[10],int k,int scheck) { int checksum,sum=0,i; printf("\n\n****RECEIVER****\n"); for(i=0;i<k;i++) sum+=c[i]; printf(" RECEIVER SUM IS:%d",sum); sum=sum+scheck; checksum=~sum; printf("\nRECEIVER's CHECKSUM IS:%d",checksum); return checksum; } main() { int a[10],i,m,scheck,rcheck; printf("\nENTER SIZE OF THE STRING:"); scanf("%d",&m); printf("\nENTER THE ELEMENTS OF THE ARRAY:"); for(i=0;i<m;i++) scanf("%d",&a[i]); scheck=sender(a,m); rcheck=receiver(a,m,scheck); if(rcheck==0) printf("\n\nNO ERROR IN TRANSMISSION\n\n"); else printf("\n\nERROR DETECTED"); }

Out Put

Program No.6 /* C program for the Implementation Of Byte Stuffing */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char da[10]="NULL",data[10]="NULL",sy='@',re[10]="NULL"; int i,n,k=0,ct=0,len,w,s=0; clrscr(); printf("\nHeader is inserted...('@')"); printf("\nEnter string:::"); gets(da); strcpy(data,da); len=strlen(da); data[0]=sy; for(i=0;i<=len;i++) { data[i+1]=da[i]; } data[len+1]=sy; printf("\nFooter is inserted...(@)"); printf("\nYour data::"); puts(data); len=strlen(data); printf("The data length %d",len--); for(i=0;i<len;i++) { if(data[i]==sy) { s++; } } printf("\nThe no of symbol is %d",s); for(i=0;i<=len;i++) { if(data[i]==sy && i!=0 && i!=len) ct++; if(ct==1 && i!=len && i!=0)

{ re[i+k]=data[i]; k++; re[i+k]=sy; ct=0; } else re[i+k]=data[i]; } printf("\n\n\nByte stuffing::"); puts(re); len=strlen(re); printf("\n\nLength=%d",len); getch(); } Output: enter the bit information size: 9 enter the information in bits: HelloRamp data after stuffing: Hello&&Ramp

Program No.7 Use the some DOS Commands in the Computer Networking .

DOS Commands
Common DOS Commands 1. ping command ping xxx.xxx.xxx.xxx (ip address) ping yahoo.com (web address) Switches - You maybe use the switches together ping xxx.xxx.xx.xx -t Allows a continuous ping. To stop the ping press Cntrl+C. -l size Allows you to send a specific amount of data. The Ping command is a network tool used to determine if you are able to send a set amount of information between your computer on the internet(or local network) to another computer without losing packets along the way. By doing this you can determine if you're online or if a website is down. It also calculates round trip time and Time to Live. This can let you know if your NIC card is working properly and if you're able to reach your router or modem. It can also be used as a network trouble shooting tool. 2. ipconfig command ipconfig Used to view your current IP values of your TCP/IP settings. ipconfig /all To display all your IP information for all adapters. ipconfig /release To release your current IP information. ipconfig /renew Used to renew your IP information. ipconfig /displaydns This shows your current DNS Resolver Logs. ipconfig /flushdns This clears your current DNS Resolver Logs. You would run an ipconfig if you need to find your IP address, default gateway or subnet mask. These numbers can be very helpful when trouble shooting your network connection. if you have changed your settings but they are not taking place you may try a release and renew. if you're having problems resolving to a website you may try clearing your DNS Resolver Logs.

3. tracert tracert yahoo.com With Trace route you can check the path your packets take across the internet from you to your destination. Along the way you can determine the time from hop to hop. You can identify server problems and latency with this tool. 4. nslookup nslookup yahoo.com nslookup xxx.xxx.xxx.xxx nslookup is used to resolve and IP to hostname or vice versa. Hostname to IP. It can be a way to find out if your DNS is properly working or if the site is having problems. You can obtain an IP from a site and try to visit the IP directly, bypassing the Domain Name Servers that would usually resolve the Domain name to IP name. 5. netstat netstat netstat can be used to view your active TCP/IP connections. You can determine what ports are being used, what programs are using your ports and what kind of TCP and UDP connections are present. Switches netstat -a Displays all active TCP connections. And TCP / UDP ports. netstat -e Displays ethernet statistics. netstat -b Displays all active programs that are listening. 6. netsh netsh winsock reset winsock reset netsh int ip reset resetlog.txt TCP/IP reset netsh is used to reset or rebuild the Windows TCP/IP IP Stack. This can be done if you have a problem with DNS. Say you are able to browse to a sites IP address but not to it's Domain Name. You can find the sites IP by using nslookup mentioned above. 7. arp arp -a This command can be used to find out the MAC address of the device you are connecting to. With this information, you can find out what type of router you are connected to by using this website.

To get to the DOS Command Prompt To get to the DOS Command prompt you simply go to your Windows Start menu, Then go to Run. When the little box pops up you type in cmd. Once the Black Command Prompt pops up you can type any of these commands in and have some fun! Another way to get the Run box is to hit your Windows Key + R at the same time.

Program No. 8
/* WAP for calculate the LRC (Longitudinal Redundancy Check)*/ #include<stdio.h> #include<conio.h> void main() { unsigned char LRC = 0x00; unsigned char CMNDarray[]= {0x01, 0x01, 0x00, 0x07, 0xC0}; int index, count; count = 5; clrscr(); for (index = 0; index<count; index++) { LRC = CMNDarray[index]+LRC; // printf(" LRC temp is %X ",LRC); // printf(" index is %d ",index); // printf(" count is %d ",count); }

LRC = 0xFF-LRC; // 1's complement LRC = LRC+1; // 2's complements

printf(" LRC Calculated is %X ",LRC); } #include<stdio.h> #include<conio.h> void main() { unsigned char LRC = 0x00; unsigned char CMNDarray[]= {0x01, 0x01, 0x00, 0x07, 0xC0}; int index, count; count = 5; clrscr(); for (index = 0; index<count; index++) { LRC = CMNDarray[index]+LRC; // printf(" LRC temp is %X ",LRC); // printf(" index is %d ",index); // printf(" count is %d ",count);

LRC = 0xFF-LRC; // 1's complement LRC = LRC+1; // 2's complements

printf(" LRC Calculated is %X ",LRC);

Output:

Example 1: Single byte: AA Since it's a single byte command, there are no other numbers to be added, so just we have to find its 2's complement. 1's complement of AA = FF AA = 55.<br> 2's complent of AA = 55 + 1 = 56. So the LRC is 56. Example 2: Two byte command. 7E, A6. Step 1. 7E + A6 = 124.<br> Step 2. After discarding the carry = 24.<br> Step 3. 1's complement of 24 = FF-24 = DB<br> Step 4. 2's complement of 24 = DB + 1 = DC. So LRC of 7E,A6 is DC. Example 3: 01, 01, 00, 07, C0 Step 1. 01+01 = 02<br> Step 2. 02+00 = 02<br> Step 3. 02+07 = 09<br> Step 4. 09+C0 = C9<br> Step 5. 1's complement of C9 = FF-C9 = 36<br> Step 6. 2's complement of C9 = 36+1 = 37. So the LRC of the above mentioned bytes is 37.

Program No. 9 WAP in C for Encryption and Decryption the Message. #include<stdio.h> void main() { FILE *fp,*fp1; int choi; char name[20],temp[20]={"Temp.txt"},c; clrscr(); printf("Press 1 to Encrypt: Press 2 to Decrypt"); printf("Enter your Choice:"); scanf("%d",&choi); switch(choi) { case 1: printf("Enter the filename to Encrypt:"); scanf("%s",name); fp=fopen(name,"r+"); if(fp==NULL) { printf("The file %s can't be open",name); getch(); exit(); } fp1=fopen(temp,"w+"); if(fp1==NULL) { printf("The file Temp can't be open"); getch(); exit(); } c=fgetc(fp); while(c!=EOF) { fputc((c+name[0]),fp1);printf("%c",c+name[0]);getch(); c=fgetc(fp); } fclose(fp);

fclose(fp1); remove(name); rename(temp,name); printf("The file is Encrypted:"); getch(); break; case 2: printf(" Enter the Filename to Decrypt:"); scanf("%s",name); fp=fopen(name,"r+"); fp1=fopen(temp,"w+"); c=fgetc(fp); while(c!=EOF) { fputc(c-name[0],fp1); c=fgetc(fp); } fclose(fp); fclose(fp1); remove(name); rename(temp,name); printf("The file is decrypted:"); getch(); } }

You might also like