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