Monday, 15 August 2016

A Java Program to perform encryption and decryption using Monoalphabetic Cipher

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

public class MonoAlphabeticSubstitutionCipher
{
 public char p[] = {'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'};
 public char ch[] = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
 public String doEncryption(String s)
 {
                char c[]=new char[(s.length())];
                for(int i=0;i<s.length();i++)
                {
                                for(int j=0;j<26;j++)
                                {
                                                if(p[j]==s.charAt(i))  
                                                {
                                                                c[i]=ch[j];
                                                                break;   
                                                }
                                }
 }
 return(new String(c));
 }
 public String doDecryption(String s)
 {
                char p1[]=new char[(s.length())];
                 for(int i=0;i<s.length();i++)
                {
                for(int j=0;j<26;j++)
                {
                                if(ch[j]==s.charAt(i))  
                                {
                                                p1[i]=p[j];
                                                break;   
                                }
                }
  }
   return(new String(p1));
 }




                public static void main(String args[])
                {
                                String msg="helloworld";
                                String emsg,nmsg;

                                MonoAlphabeticSubstitutionCipher c1=new MonoAlphabeticSubstitutionCipher();
                                emsg= c1.doEncryption(msg);
                                System.out.println("\n\n Normal Text : " + msg);

                                System.out.println("\n\n Encrypted Text : " + emsg);

                                nmsg= c1.doDecryption(emsg);

                                System.out.println("\n\n main Decrypted Text : " + nmsg);
                }

}

/*End of the Program */
copy the above code in your desired editor , 
-> compile it with javac 
-> then run it with java

Below it the expected out put in a screen take.
Note: you will be prompted to enter your text, remember to use it your other applications before sending the information to the network for security preseasons.


Tuesday, 9 August 2016

A Java Program to perform encryption and decryption using Ceaser Cipher.

import java.util.Scanner;

public class myCaeserCipher {
public char p[] = {'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 key = 3;

         public String doEncryption (String s){
          String s1 = new String(s);
          s1 = "";
          int t;
          for (int i=0; i<s.length();i++){
          for(int j=0; j<26;j++){
          if(s.charAt(i)==' '){
          s1 = s1+" ";
          break;
          }
          if(p[j]==s.charAt(i)){
          t=(j-key+26)%26;
          s1=s1+p[t];
          break;
          }
          }
          }
          return(s1);
         }

         public String doDecrypt(String s){
          String s1 = new String(s);
          s1="";
          int t;
          for(int i=0; i<s.length();i++){
          for(int j=0;j<26; j++)
                  {
          if(s.charAt(i)==' '){
          s1=s1+" ";
          break;
          }
          if(p[j]==s.charAt(i)){
          t=(j+key+26)%26;
          s1=s1+p[t];
          break;
          }
          }
          }
          return(s1);
          }        
     
         public static void main (String args[]){
            Scanner sc = new Scanner(System.in);
            System.out.println("Ente a string: \n");

          String msg = sc.nextLine();
          String emsg,nmsg;

          myCaeserCipher c1 = new myCaeserCipher();
          emsg = c1.doEncryption(msg);
          System.out.println("\nafter encryption: "+ emsg);

          nmsg = c1.doDecrypt(emsg);
          System.out.println("\nafter decryption : " + nmsg);

         }

}
 /* The End Of Our Program Code */
Copy the above code and paste it in your editor, 
-> save the file as myCaeserCipher.java, 
->compile it using javac myCaeserCipher.java
->then run it using java myCaeserCipher

if all done well, the following out put is expected
-------------------------------------------------------------------------------------------------------------------

Tuesday, 12 July 2016

SHELL UNIX LINUX PROGRAM TO IMPLEMENT SIGNAL

#include <stdio.h>     
#include <unistd.h>    
#include <signal.h>    
void catch_int(int sig_num)
{
    signal(SIGINT, catch_int);
    printf("Don't do that\n");
    fflush(stdout);
}

int main(int argc, char* argv[])
{
    /* set the INT (Ctrl-C) signal handler to 'catch_int' */
    signal(SIGINT, catch_int);

    /* now, lets get into an infinite loop of doing nothing. */
    for ( ;; )
        pause();
}


Output:

[csuser@localhost ~]$ vi sig.c
[csuser@localhost ~]$ gcc sig.c
[csuser@localhost ~]$ ./a.out
^CDon't do that
^Z
[1]+  Stopped                 ./a.out

SHELL UNIX-LINUX PROGRAM TO CONVERT LOWER CASE TO UPPER CASE

#include <stdio.h>
#include <ctype.h>

void main()
{
    char sentence[100];
    int count, ch, i;

    printf("Enter a sentence \n");
    for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
    {
        ;
    }
    sentence[i] = '\0';
    /*  shows the number of chars accepted in a sentence */
    count = i;
    printf("The given sentence is   : %s", sentence);
    printf("\n Case changed sentence is: ");
    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
        putchar(ch);
    }





Output:

[csuser@localhost ~]$ gcc l.c
[csuser@localhost ~]$ ./a.out
Enter a sentence
kishan
The given sentence is   : kishan
 Case changed sentence is: KISHAN

SHELL UNIX-LINUX IMPLEMENTATION OF COPY COMMAND

#include<stdio.h>
int main(){
  FILE *p,*q;
  char file1[20],file2[20];
  char ch;
  printf("\nEnter the source file name to be copied:");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL){
      printf("cannot open %s",file1);
      exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL){
      printf("cannot open %s",file2);
      exit(0);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 return 0;
}



Output:

[csuser@localhost ~]$ vi c.c
[csuser@localhost ~]$ gcc c.c
[csuser@localhost ~]$ ./a.out

Enter the source file name to be copied: file1

Enter the destination file name: file2

COMPLETED

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT PIPES USING GREP

echo "Enter file name:"
read fname
echo "Enter expression to search:"
read exp
echo "Output of grep is :"
grep $exp $fname
echo "Total lines containing $exp in $fname is"
grep $exp $fname | wc –l

output:

[csuser@localhost ~]$ vi gp
[csuser@localhost ~]$ chmod 777 gp
[csuser@localhost ~]$ ./gp
Enter file name:
k
Enter expression to search:
s

Output of grep is :
gdsgsg
sdfgsdgsdg
dfgsdgsg
gsdgsg
Total lines containing s in k is
4

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT CASE STRUCTURE

echo "1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

echo "Please enter your choice:"

read choice

echo "Enter the first value:"

read a

echo "Enter the second value:"

read b

case $choice in
1) sum=`expr $a + $b`
echo "Sum of $a and $b is " $sum
;;

2) diff=`expr $a - $b`
echo "Difference of $a and $b is " $diff
;;

3) prod=`expr $a \* $b`
echo "Product of $a and $b is " $prod
;;

4) div=`expr $a / $b`
echo "Divions of $a and $b is " $div
;;

*) echo "Invalid option selected!"

Esac


program to implement case structure

Output:

[csuser@localhost ~]$ vi ma
[csuser@localhost ~]$ chmod 777 ma
[csuser@localhost ~]$ ./ma
1. Addition
2. Subtraction
3. Multiplication
4. Division
Please enter your choice:
1
Enter the first value:
2
Enter the second value:
3
Sum of 2 and 3 is  5

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT IF THEN ELSE

echo "Enter marks in subject1"
read m1
echo "Enter marks in subject2"
read m2
echo "Enter marks in subject3"
read m3
sum=`expr $m1 + $m2 + $m3`
echo "Sum is = " $sum
percentage=`expr $sum / 3`
if test $percentage -ge 70
then
echo "You have got a distinction!"
elif test $percentage -ge 60
then
echo "You have got a first class!"
elif test $percentage -ge 40
then
echo "You are qualified!"
else
echo "You have failed!"
fi
program to implement if then else



Out put:

[csuser@localhost ~]$ vi ra
[csuser@localhost ~]$ chmod 777 ra        (ra is file name)
[csuser@localhost ~]$ ./ra
Enter marks in subject1
23
Enter marks in subject2
34
Enter marks in subject3
45
Sum is =  102
You have failed!

SHELL UNIX-LINUX PROGRAM TO WISH DEPENDING ON THE TIME OF DAY

hr=`date | cut -c 12-13`
if [ $hr -lt 12 ]
then
echo "Good Morning"
else
if [ $hr -ge 12 -a $hr -lt 15 ]
then
echo "Good Afternoon"
else
if [ $hr -ge 15 -a $hr -lt 24 ]
then
echo "Good Evening"
fi
fi
fi

Out put:
[csuser@localhost ~]$ vi tt
[csuser@localhost ~]$ chmod 777 tt                   (tt is a file name)
[csuser@localhost ~]$ ./tt
good morning

Monday, 4 July 2016

PROGRAM TO IMPLEMENT TYPE OF A FILE

echo "Enter file name:"
read fname
if test -f $fname
then
echo "The given name $fname is a file!"
elif test -d $fname
then
echo "The given name $fname is a directory!"
else
  echo “Then name $fname doesn’t exist”
fi
output: 
[csuser@localhost ~]$ vi x
[csuser@localhost ~]$ chmod 777 x
[csuser@localhost ~]$ ./x
Enter file name:
a                                                   ->   (a is a file name)
Then name a doesn.t exist.

java program to find the area of a circle

//ComputeArea.java:Compute the area of a circle

public class ComputeArea {
public static void main(String[] args){

 double PI = 3.14159;
double radius = 20;

//Compute area

double area = radius * radius * PI;

//Display result

System.out.println("The area for the circle of radius," + radius  + "is,"   + area);

}
}