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
-------------------------------------------------------------------------------------------------------------------