Tuesday, December 27, 2016

Accept name and display name with good morning message.

C program to accept name and display the name with good morning message.

#include<stdio.h>
int main()
{
  int time;
  char name[20];

  printf("Enter your name:");
  gets(name);

  printf("Enter time:");
  scanf("%d", &time);

  if(time < 12)
  {
     printf("Good Morning...%s", name);
  }
  return 0;
}

Output of the program:

Enter your name:Manish
Enter time:10
Good Morning...Manish


* * * * *

Monday, December 26, 2016

Simple Rail Fence - Encryption using C program.

Rail fence Simple - Encryption implementation using C program.

#include<stdio.h>
int main()
{
  char str[20]="HelloStudent", str1[10]="", str2[10]="";
  int i, cnt1=0, cnt2=0;
  printf("Rail Fence - Encryption\n\n");
  printf("Plain Text: HelloStudent\n\n");

  for(i=0; i<strlen(str); i++)
  {
    if( i%2 == 0)
    {
      str1[cnt1++]=str[i];
    }
    else
  str2[cnt2++]=str[i];
  }
  printf("Cipher Text: %s%s",str1,str2);
  return 0;
}

Output of the program:

Rail Fence - Encryption

Plain Text: HelloStudent

Cipher Text: HlotdnelSuet


* * * * *
* * * * *

Security - Playfair Key Matrix Generation



Playfair Key Matrix Generation: 

1. Read a keyword from User
2. Validate a keyword as per specification.
3. Generate and Display Playfair Key Matrix

#include<stdio.h>
int main()
{
  char arr[5][5], key[10], temp[26]="";
  int i,j,flag=0, cnt=0, key_check[26]={};
  printf("Enter your Keyword:");
  scanf("%s",key);
  //Logic to check given keyword....MONARCHY
  for(i=0; i<strlen(key); i++)
  {
key_check[key[i]%65]++;
  }
  for(i=0; i<26; i++)
  {
if(key_check[i] > 1)
flag=1;
//printf("%c=%d \n",i+65, key_check[i]);
  } 
  if(flag==1)
  {
printf("Enter proper keyword...\n");
    exit(0);
  }
  else
printf("Keyword %s is ok.\n", key);

  //Logic to fill Key Matrix using keyword..
  strcpy(arr,key);
//logic to initialize remaining cell of key matrix
  for(i=0; i<26; i++)
  {
if(key_check[i]==0)
{
temp[cnt++]=(char)key_check[i]+65+i;
//printf("%c",key_check[i]+65+i);
}
  }
  temp[cnt]='\0';
  strcat(arr,temp);
  //Logic to print Key Matrix
  printf("Your Playfair Key Matrix:\n");
  for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
printf("\n");
  }
  return 0;
}

Output of the program:

Enter your Keyword:MONARCHY
Keyword MONARCHY is ok.
Your Playfair Key Matrix:
M O N A R
C H Y B D
E F G I J
K L P Q S
T U V W X


Security - Rail Fence (complex) Decryption using C program.

This C program demonstrate how to decrypt given cipher text using Rail fence (complex) technique.

Assumptions:

1. 5 X 5 matrix is used.
2. Key = 21345

Sample Output expected:

RAIL FENCE DECRYPTION

Cipher text: BGLQVAFKPUCHMRWDINSXEJOTY

Key Used = 2 1 3 4 5

Plain text: ABCDEFGHIJKLMNOPQRSTUVWXY


* * * * *

* * * * *
/*
Security - Decryption using Rail fence (complex) technique.

Assumptions:
1. 5 column matrix is used.
2. Key = 21345
*/
#include<stdio.h>
int main()
{
  char str[5][5], plaintext[26];   
  char ciphertext[26]="BGLQVAFKPUCHMRWDINSXEJOTY";
  int i,j,k,key[5]={2,1,3,4,5}, cnt=0, len;

  len = strlen(ciphertext);
  printf("RAIL FENCE DECRYPTION\n\n");
  printf("Input Data: BGLQVAFKPUCHMRWDINSXEJOTY\n\n");
  printf("Key Used = 2 1 3 4 5 \n\n");

  //Writing Cipher text in str[][] matrix.
  for(i=0; i<len/5; i++)
  {
k=key[i]-1;
for(j=0; j<5; j++)
str[j][k]=ciphertext[cnt++];
  }
  printf("Ciphertext arranged in matrix for key:21345\n\n");
  for(i=0; i<len/5; i++)
  {
for(j=0; j<5; j++)
printf("%c ",str[i][j]);
printf("\n");
  }
  printf("\nPlain text: ");
  for(i=0; i<len/5; i++)
  {
for(j=0; j<5; j++)
printf("%c",str[i][j]);
  }
  return 0;
}

//Data can be taken from cipher.txt data file.
//Plain text can be stored into plain.txt.
//You can make this program more dynamic by using file management.
/*
Output

RAIL FENCE DECRYPTION

Input Data: BGLQVAFKPUCHMRWDINSXEJOTY

Key Used = 2 1 3 4 5

Ciphertext arranged in matrix for key:21345

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

Plain text: ABCDEFGHIJKLMNOPQRSTUVWXY
*/

Security - Rail Fence (complex) Encryption using C program.

This C program demonstrate how to encrypt data using Rail fence (complex) technique.

Assumptions:

1. 5 X 5 matrix is used.
2. Key = 21345

Sample Output:

RAIL FENCE ENCRYPTION

Input Data: 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

Input data arranged in 5 X 5 matrix:
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

Key Used = 2 1 3 4 5

Ciphertext: B G L Q V A F K P U C H M R W D I N S X E J O T Y

* * * * *


* * * * *
/*
Definition: Write a C program to Encrypt given data using Rail fence (complex) technique.

Assumptions:

1. 5 X 5 matrix is used.
2. Key = 21345

*/

#include<stdio.h>
int main()
{
 char str[5][5];
 int i,j,k, key[5]={2,1,3,4,5}, cnt=65;

 printf("RAIL FENCE ENCRYPTION\n\n");
 printf("Input Data: 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\n\n");

 //Input data A to Y is stored in str[][] matrix.
 printf("Input data arranged in 5 X 5 matrix:\n");
 for(i=0; i<5; i++)
 {
  for(j=0; j<5; j++)
str[i][j]=cnt++;
 }

 //To print full matrix..
 for(i=0; i<5; i++)
 {
  for(j=0; j<5; j++)
printf("%c ",str[i][j]);
printf(" \n");
 }
 printf(" \n");
 printf("Key Used = 2 1 3 4 5 \n\n");
 printf("Ciphertext: ");

 for(j=0; j<5; j++)
 {
k=key[j]-1;
for(i=0; i<5; i++)
printf("%c ",str[i][k]);
 }
 return 0;
}

//Data can be taken from input.txt data file.
//Ciphertext can be stored into output.txt using C file management.

Output of the program

RAIL FENCE ENCRYPTION

Input Data: 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

Input data arranged in 5 X 5 matrix:
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

Key Used = 2 1 3 4 5

Ciphertext: B G L Q V A F K P U C H M R W D I N S X E J O T Y

* * * * *

Sunday, December 25, 2016

Networking - Simulation of Framing Concept.

Networking: Simulation of Framing Concept using C language.

Assumptions:

1. Header contains following parameters:
    FrameNumber, SourceIP, DestinationIP,FiveCharacters of Data
2. SourceIP=192.168.1.1
3. DestinationIP=192.168.1.2
4. Input data is taken from input.txt file.
5. Generated Frames will be stored in output.txt file.

#include <stdio.h>
int main()  
{
  FILE *fp1, *fp2, *fopen(); 
  int i;
  char c,cnt=49; 
  fp1 = fopen("input.txt","r");  
  // open for reading
  fp2 = fopen("output.txt","w") ; 
  //open for writing

  if ( fp1 == NULL )
 
printf("Cannot open input.txt." ); 
exit(1);    
 
  else if ( fp2 == NULL ) 
 
printf("Cannot open output.txt."); 
exit(1);    
 
  else 
  {
  c = getc(fp1) ;  
  while ( c != EOF) 
 
putc(cnt++,fp2);
fputs(",192.168.1.1,192.168.1.2,",fp2);
//Logic to track 5 characters
for(i=0;i<5;i++) 
{
putc( c,  fp2); //Write to Output.txt 
c =  getc( fp1 ) ;
//putc(10,fp2);
}
putc(10,fp2);
  }
  printf("Frames generated in Output.txt file.");
  fclose(fp1); //close files
  fclose(fp2); 
  }
  return 0; 
}

Output of the program:

Frames generated in Output.txt file.

Sample content in input.txt file is as under:

Hello students, how are you?

After execution of program content of output.txt file:

1,192.168.1.1,192.168.1.2,Hello
2,192.168.1.1,192.168.1.2, stud
3,192.168.1.1,192.168.1.2,ents,
4,192.168.1.1,192.168.1.2, how 
5,192.168.1.1,192.168.1.2,are y
6,192.168.1.1,192.168.1.2,ou?ÿÿ

* * * * *




Saturday, December 24, 2016

Networking - UDP Header implementation

This C program generates UDP Header with following assumptions:

1. Sample data in input.txt file: ABC
2. Source Port=1100, Destination Port=1101
3. Total length=50
4. You may use your logic for Checksum calculation.


#include <stdio.h>
void main()  
{
FILE *fp1, *fp2, *fopen(); 
int src_port=1100, dest_port=1101, total_len=50,checksum=0;
char c;

fp1 = fopen( "input.txt",  "r" );       /* open for reading */ 
fp2 = fopen( "UDPHeader.txt", "w" ) ; /* open for writing */

if ( fp1 == NULL )     /* check file exist/not  */ 
{
printf("Cannot open input.txt for reading." ); 
exit(1);     /* Exit program */ 

else if ( fp2 == NULL ) 
{
printf("Cannot open UDPHeader.txt for writing."); 
exit(1);     /* Exit program */ 

else 
{
c = getc(fp1) ;
while ( c != EOF) 
{
checksum = checksum + c;
c =  getc( fp1 ) ;

fprintf(fp2,"%d,%d,%d,",src_port,dest_port,total_len);
fprintf(fp2,"%d",checksum);
printf("UDP Header created successfully in UDPHeader.txt file.");
fclose(fp1); /*close files */
fclose(fp2); 


}

Output of the program

UDP Header created successfully in UDPHeader.txt file.

Data after program execution in UDPHeader.txt file:

1100,1101,50,198
* * * * *



Saturday, December 17, 2016

Computer Networking Practical - Fragmentation Implementation

Computer Networking Practical of Fragmentation

/* Implementation of fragmentation concept using C program.
Assume MTU size is 5 characters. 
All fragments will be displayed on screen.
Content of in.txt file is: "Hello student. How are you?"
*/

#include<stdio.h>
#include<string.h>
int main()
{
  char ch;
  int i=0,cnt=0;
  FILE *fp;

  fp=fopen("in.txt","r");
  ch=fgetc(fp);

/*Assuming SourceIP=192.168.1.1 and DestinationIP=10.1.1.1*/

  printf("%d,192.168.1.1, 10.1.1.1,",cnt++);
  while(ch != EOF)
  {
  printf("%c", ch);
  i++;
  if(i%5==0)
  {
printf("\n");
printf("%d,192.168.1.1, 10.1.1.1,",cnt++);
  }
  ch = fgetc(fp);
  }
  printf("\nTotal fragments created=%d",cnt);
  return 0;
}

/*Instead of printing fragments on screen, we can also write fragments into a file.*/

Output of the program

0,192.168.1.1, 10.1.1.1,Hello
1,192.168.1.1, 10.1.1.1, stud
2,192.168.1.1, 10.1.1.1,ent.
3,192.168.1.1, 10.1.1.1,How a
4,192.168.1.1, 10.1.1.1,re yo
5,192.168.1.1, 10.1.1.1,u?
Total fragments created=6
* * * * *

Thursday, December 15, 2016

Find the Ball Game using C language.

Use Up, Down, Right and Left arrow keys to move your bat on screen to search the Ball. Press X to exit from the game.


Fig. Find the Ball Game

* * * * *


* * * * *

//Find the Ball Game using C language.

#include <stdio.h>
#include <windows.h>

void display_border();

COORD c = {0, 0};
void setxy (int x, int y)
{
 c.X = x; c.Y = y; // Set X and Y coordinates
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main() 
int batx=15,baty=13, ballx=25, bally=7;
int x=25, y=10, ch1, ch2, b;
setxy(14,1);
printf("...Find The Ball...") ;
display_border();
setxy(batx, baty);
printf("===");
setxy(ballx, bally);
printf("%c",2);
ch1 = getch(); 
ch2 = 0;

/*When accepting arrow key, function must be called twice; first call returns 0/0xE0; second call returns actual key code.*/
if (ch1 == 0xE0) 
{
  while(ch2=getch())
  {
if(ch2 == 'X') exit(0);
if(ch2 == 75)//Left
{
  if(batx>11) //Restrict bat on left side wall
  {
setxy(--batx,baty);
printf("===");  
setxy(batx+3,baty);//Space to clear bat route
printf(" ");
  }
}
  if(ch2 == 77) //Right
  {
if(batx<37)//Restrict bat on right side wall
{
  setxy(++batx,baty);
  printf("===");
  setxy(batx-1,baty);//Space to clear bat route
  printf(" ");
}
  }
  if(ch2 == 72) //Up
  {
if(baty>6)
{
  setxy(batx,--baty);
  printf("===");
  setxy(batx,baty+1);
  printf("   ");
}
  }
  if(ch2 == 80) //Down
  {
if(baty<14)
{
  setxy(batx,++baty);
  printf("===");
  setxy(batx,baty-1);
  printf("   ");
}
  }
  if(batx==ballx-1 && baty==bally)
  {
setxy(ballx-9,bally+4);
printf("...Congratulations...");
setxy(ballx-9,bally+5);
printf("  ...Ball Found..");
setxy(ballx-9,bally+6);
printf("   ...Game Over..");
getch();
exit(0);
  }
}
getch();
return 0; 
}

void display_border()
{
int i, j;
//Top border line...
setxy(10,5); 
for(j=0; j<30; j++)
printf("%c", 223);
//Bottom border line...
setxy(10,15);
for(j=0; j<=30; j++)
printf("%c", 223);
//Left and Right border line...
for(j=0; j<10; j++)
{
setxy(10,5+j);
printf("%c",219);
setxy(40,5+j);
printf("%c",219);
}
printf("\n");
}
//Code is design and developed by cprogrampracticals.blogspot.in

//Give reference to cprogrampracticals.blogspot.in while using this code.

* * * * *