Tuesday, February 28, 2017

Count Server Socket

Definition: Program to demonstrate use of Page views count on server.
Count server program will increment page view counter for every client request.

Count Server Socket Program

import java.io.*;
import java.net.*;

public class CountServer {

    public static void main(String[] args) throws IOException {

        ServerSocket ss = new ServerSocket(1234);
        Socket cs = null;
        System.out.println("Waiting for connection.....");
        int count=0;
        while(true){
            cs = ss.accept();          
            count++;
            System.out.println("Total visitors:"+count);
            cs.close();
        }
        //ss.close();
    }
}

Output on Server screen

Waiting for connection.....
Total visitors:1

//Validations and exception handling to be added.

Count Client Socket Program

/* This program will just create socket with server. Note: try-catch clauses are not added to keep logic simple…*/

import java.io.*;
import java.net.*;

public class CountClient
{
   public static void main(String[] args) throws IOException
   {
         Socket s1 = new Socket("localhost", 1234);
         System.out.println("Hello");
         s1.close();
   }
}

Output on Client screen

Hello

Monday, February 27, 2017

Chat Server Socket

Following two socket programs demonstrate basic Chat application. You need to add additional functionalities and exception handling according to your needs.

Chat Server Socket Program

import java.io.*;
import java.net.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        ServerSocket ss = new ServerSocket(1234);
        Socket cs = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Waiting for connection.....");
        cs = ss.accept();      
        BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        PrintWriter out = new PrintWriter(cs.getOutputStream(), true);
        String inputLine, serverInput;
        while(true){
            inputLine = in.readLine();
            System.out.println("Client: " + inputLine);
            System.out.print("Server:");
            serverInput = br.readLine();
            out.println(serverInput);
        }
        out.close();
        in.close();
        cs.close();
        ss.close();
    }
}
//You may add exit condition in while loop of server socket.

Output on server screen:

Waiting for connection.....
Client: hello
Server:hello client
Client: how are you?
Server:I am fine...what about you?


Chat Client Socket Program

import java.io.*;
import java.net.*;

public class ChatClient {
    public static void main(String[] args) throws IOException
   {
         Socket s1 = new Socket("localhost", 1234);
         DataInputStream is = new DataInputStream(s1.getInputStream());
        // String request = "Hello";
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
         System.out.print("Client:");
         String request = br.readLine();
         while(!(request.contentEquals("bye"))){
            out.println(request);
            String reply = is.readLine();
            System.out.println("Server:" + reply);
            System.out.print("Client:");
            request = br.readLine();
         }
         out.close();                   
         is.close();
         s1.close();
   }
}

Output on Client Screen:

Client:hello
Server:hello client
Client:how are you?
Server:I am fine...what about you?
Client:bye

Echo Client Server Socket

Following two programs are written in JAVA. You have to run Server program first, and then run client program. Server program will reply the message sent by client.


Simple Echo Server Program

import java.io.*;
import java.net.*;

public class SimpleEchoServer1 {
 public static void main(String[] args) throws IOException    {

    ServerSocket ss = new ServerSocket(123);
    Socket cs = null;     
    System.out.println ("Waiting for connection.....");
   
    cs = ss.accept();     
    System.out.println ("Waiting for input.....");

    BufferedReader in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
    PrintWriter out = new PrintWriter(cs.getOutputStream(),true);

    String inputLine = in.readLine();
    out.println(inputLine);
    System.out.println ("Reply sent...");

    out.close();    
    in.close();    
    cs.close();    
    ss.close();
   }
}

Output on server:

Waiting for connection.....
Waiting for input.....
Reply sent...


Simple Echo Client Program

import java.io.*;
import java.net.*;

public class SimpleEchoClient1 {
   public static void main(String[] args) throws IOException   {
         Socket s1 = new Socket("localhost", 123);
         DataInputStream is = new DataInputStream(s1.getInputStream());
         System.out.print("Enter your string:");
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String request = br.readLine();
    
         PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
         out.println(request);       
         String reply = is.readLine();
         System.out.println("Echo from Server:" + reply);
         
         out.close();           
      is.close();         
      s1.close();
   } 
}

Output on Client Screen:

Enter your string:hello
Echo from Server:hello



Saturday, February 25, 2017

RSA Decryption

#include<stdio.h>
int main()
{
long int n=187, d=23, m, c=11;
int i;
//Assuming input - Cipher text c=11.

printf("Sample Data:d=23, n=187, c=11\n");

m = 1;

for(i=0; i<d; i++){
m = m * c%n;
}
m = m%n;

printf("Plain Text of %i = %i",c,m);
return 0;
}

Output of program:

Sample Data:d=23, n=187, c=11
Plain Text of 11 = 88

RSA Encryption

Sample values taken for execution of RSA encryption are as under:

      e=7, d=23, n=187 and m=88

#include<stdio.h>
int main()
{
  long int e=7,n=187,d=23, m=88, c;
  int i;
  //you may read e,n,d and m from user.

  printf("Sample Data:e=7, d=23, n=187, m=88\n");

  c=1;
  for(i=0; i<e; i++)
{
    c = c*m%n;
  }
  c = c%n;
  printf("Cipher Text of %i = %i \n",m, c);

  return 0;
}

Output of program:

Sample Data:e=7, d=23, n=187, m=88
Cipher Text of 88 = 11



Monday, February 20, 2017

Random between 0 to 100.

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i;
   time_t my_sys_time;
   //To set random number generator as per system time 
   srand((unsigned) time(&my_sys_time));

   //To print 10 random numbers from 0 to 100
   for( i = 0 ; i < 10 ; i++ ) 
   {
      printf("%d ", rand()%100);
   }
   return(0);
}
Output of program
16 45 14 21 60 18 65 3 41 34

Thursday, February 16, 2017

Greatest Common Divisor(GCD) of given 2 numbers

Write a C program to find Greatest Common Divisor (GCD) of given 2 numbers.
GCD is also known as Greatest Common Factor (GCF)
GCD is also known as Highest Common Factor (HCF)

#include <stdio.h>
int main()
{
  int num1, num2, i, j, temp, gcd;

  printf("Enter number 1:");
  scanf("%d", &i);

  printf("Enter number 2:");
  scanf("%d", &j);

  num1 = i;
  num2 = j;

  while (num2 != 0)
  {
    temp = num2;
    num2 = num1 % num2;
    num1 = temp;
  }

  gcd = num1;
  printf("Greatest Common Divisor(GCD) of %d and %d is %d.\n", i,j,gcd);
  return 0;
}

Output of program

Enter number 1:20
Enter number 2:30
Greatest Common Divisor(GCD) of 20 and 30 is 10.


Friday, February 10, 2017

Key generation in Simplified DES


Simplified DES - Key Generation Simulation Program using C Programming


DES means Data Encryption Standard. DES is one of the top cryptographic software security algorithm used for providing security in many information systems. This c programming tutorial will help you to generate secure password (encryption key).

Assumptions for this program: 
  • 10 bits input size
  • Perform Left Shift - 1 (LS-1) on both the halfs
  • Display Key k1 as final output.

#include<stdio.h>
int main()
{
int i, cnt=0, p8[8]={6,7,8,9,1,2,3,4};
int p10[10]={6,7,8,9,10,1,2,3,4,5};

char input[11], k1[10], k2[10], temp[11];
char LS1[5], LS2[5];
//k1, k2 are for storing interim keys
//p8 and p10 are for storing permutation key

//Read 10 bits from user...
printf("Enter 10 bits input:");
scanf("%s",input);
input[10]='\0';

//Applying p10...
for(i=0; i<10; i++)
{
cnt = p10[i];
temp[i] = input[cnt-1];
}
temp[i]='\0';
printf("\nYour p10 key is    :");
for(i=0; i<10; i++)
{ printf("%d,",p10[i]); }

printf("\nBits after p10     :");
puts(temp);
//Performing LS-1 on first half of temp
for(i=0; i<5; i++)
{
if(i==4)
temp[i]=temp[0];
else
temp[i]=temp[i+1];
}
//Performing LS-1 on second half of temp
for(i=5; i<10; i++)
{
if(i==9)
temp[i]=temp[5];
else
temp[i]=temp[i+1];
}
printf("Output after LS-1  :");
puts(temp);

printf("\nYour p8 key is     :");
for(i=0; i<8; i++)
{ printf("%d,",p8[i]); }

//Applying p8...
for(i=0; i<8; i++)
{
cnt = p8[i];
k1[i] = temp[cnt-1];
}
printf("\nYour key k1 is     :");
puts(k1);
//This program can be extended to generate k2 as per DES algorithm.
}

Output of program

Enter 10 bits input:1100011100

Your p10 key is    :6,7,8,9,10,1,2,3,4,5,
Bits after p10     :1110011000
Output after LS-1  :1100110001

Your p8 key is     :6,7,8,9,1,2,3,4,
Your key k1 is     :10001100



You may also like to view following Computer Security Programs:


Wednesday, February 8, 2017

Simplified DES - Initial Permutation function

Simulation of Simplified DES - Initial Permutation function.
Note: Validations, Exceptions are to be added by programmer.


#include<stdio.h>
int main()
{
int IPkey[8]={1,6,7,8,5,2,3,4};
int i, cnt;
char input[9], output[9];

printf("Enter your 8 bits input:");
scanf("%s",input);
input[8]='\0';
printf("Your input is:%s\n", input);
printf("IP key used : ");
for(i=0; i<8; i++) {
printf("%d",IPkey[i]);
}
printf("\n");
for(i=0; i<8; i++)
{
cnt=IPkey[i];
output[i]=input[cnt-1];
}
output[8]='\0';

printf("Your output is %s", output);
return 0;
}

Output of the program

Enter your 8 bits input:10011101
Your input is:10011101
IP key used : 16785234
Your output is 11011001

Sunday, February 5, 2017

Playfair Decryption


Playfair Decryption implementation 


Assumptions: 
  • Assume key matrix is given to us. 
  • Read cipher text (2 characters) from user. 
  • This program demonstrate four rules of the Playfair decryption algorithm. 
  • This program will process only 2 characters input. You may extend to process n characters by repeating given logic.

#include<stdio.h>
int main()
{

char arr[5][5]={"MONAR","CHYBD","EFGIK","LPQST","UVWXZ"};
char ct[10];

int i, j, r1=0, r2=0, c1=0, c2=0;
printf("Plaifair Keymatrix\n=================\n");
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
printf("\n");
}

printf("Enter your cipher text:");
scanf("%s",ct);
printf("Your cipher text is %s\n", ct);

for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(arr[i][j] == ct[0])
{
r1=i; c1=j;
}
if(arr[i][j] == ct[1])
{
r2=i; c2=j;
}
}
}
if(r1==r2) //Rule2-when both characters in same row
{
if(c2==0) //for char in last column
printf("Plaintext = %c%c \n", arr[r1][c1-1], arr[r2][4]);
else
printf("Plaintext = %c%c \n", arr[r1][c1-1], arr[r2][c2-1]);
}
if(c1==c2)//Rule3- when both characters in same column
{
if(r2==0) //for char in last row
printf("Plaintext = %c%c \n", arr[r1-1][c1], arr[4][c2]); 
else
printf("Plaintext = %c%c \n", arr[r1-1][c1], arr[r2-1][c2]); 
}
//Rule4 when characters are not in a same row and column
if(r1 != r2 && c1 != c2) 
{
printf("Plaintext = %c%c \n", arr[r1][c2], arr[r2][c1]); 
}
return 0;
}

Output of the program

Plaifair Keymatrix
=================
M O N A R
C H Y B D
E F G I K
L P Q S T
U V W X Z
Enter your cipher text:NA
Your cipher text is NA

Plaintext = ON

Click here to check Playfair Encryption Program

Playfair Encryption




Playfair Encryption implementation 


Playfair is one of the popular cryptographic software security algorithms. This technique encrypts pairs of letters at a time and generates more secure encrypted text compare to  the simple substitution cipher like Caesar.


Assumptions:
  • Assume key matrix is given to us. 
  • Read plain text(2 characters) from user.
  • This program demonstrate four rules of the Playfair encryption algorithm.
  • This program will process only 2 characters input. 
  • You may extend to process n characters by repeating given logic.
  • Add suitable exception for completing this program.

#include<stdio.h>
int main(){

  char arr[5][5]={"MONAR","CHYBD","EFGIK","LPQST","UVWXZ"};
  char pt[10];

  int i, j, r1=0, r2=0, c1=0, c2=0;
  printf("Playfair Keymatrix\n==================\n");
  for(i=0; i<5; i++)
  {
  for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
  printf("\n");
  }

  printf("Enter your plain text:");
  scanf("%s",pt);
  printf("Your plain text = %s", pt);

  for(i=0; i<5; i++)
  {
  for(j=0; j<5; j++)
  {
    if(arr[i][j] == pt[0])
{
r1=i; c1=j;
}
if(arr[i][j] == pt[1])
{
r2=i; c2=j;
}
  }
  }
  if(r1==r2) //when both characters in same row
  {
  if(c2==4) //for char in last column
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][0]);
  else
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][c2+1]);
  }
  if(c1==c2)//when both characters in same column
  {
  if(r2==4) //for char in last row
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[0][c2]); 
  else
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[r2+1][c2]); 
  }
  //when characters are not in a same row and column
  if(r1 != r2 && c1 != c2) 
  {
  printf("\nCiphertext = %c%c \n", arr[r1][c2], arr[r2][c1]); 
  }
  return 0;
}

Output of the program

Playfair Keymatrix
==================
M O N A R
C H Y B D
E F G I K
L P Q S T
U V W X Z
Enter your plain text:IN
Your plain text = IN
Ciphertext = GA

Click here to check Playfair Decryption Program