Thursday, 4 September 2014

A program for encrypting and decrypting data

Objective:

The program is used to Encrypt Information in Alpha-Numeric Form using suitable Frequency +ve or -ve which can then be used again to decode the Encrypted Code.
Computer encryption is based on the science of cryptography, which has been used as long as humans have wanted to keep information secret. Before the digital age, the biggest users of cryptography were governments, particularly for military purposes.
An Encrypted Document.
The Greeks were also the first to use ciphers, specific codes that involve substitutions or transpositions of letters and numbers. Here's an example of a typical cipher, with a grid of letters and their corresponding numbers:The Greek historian Plutarch wrote, for example, about Spartan generals who sent and received sensitive messages using a scytale, a thin cylinder made out of wood. The general would wrap a piece of parchment around the scytale and write his message along its length. When someone removed the paper from the cylinder, the writing appeared to be a jumble of nonsense. But if the other general receiving the parchment had a scytale of similar size, he could wrap the paper around it and easily read the intended message. 








1
1A
2FGHI/JK
3LMNOP
4QRSTU
5VWXYZ


If a Spartan general wished to send the message I AM SPARTA to another general, he would write this series of numbers:
42 11 23 34 53 11 24 44 11
As long as both generals had the correct cipher, they could decode any message the other sent. To make the message more difficult to decipher, they could arrange the letters inside the grid in any combination.
Most forms of cryptography in use these days rely on computers, simply because a human-based code is too easy for a computer to crack. Ciphers are also better known today as algorithms, which are the guides for encryption -- they provide a way in which to craft a message and give a certain range of possible combinations. Akey, on the other hand, helps a person or computer figure out the one possibility on a given occasion.
Computer encryption systems generally belong in one of two categories:
  • Symmetric-key encryption
  • Public-key encryption
BlueJ Program Screenshot:



Java Program Source Code:

/**
  * The Encryptor_Decoder Program is used to hide the secret Message by Algorithm Frequency.
  * © SHANTANU KHAN 
  * @ shantanukhan1995@gmail.com
  * @website 0code.blogspot.com 
  * Program Type : BlueJ Program - Java
  */

  import java.io.*;
  public class Encryptor_Decoder
  {
   static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   static String Str="",Enc="",Dec="";
   int algoEnc,algoDec;
   char C[]=new char[26],S[]=new char[26],N[]=new char[10];
  
   void inputEnc()throws Exception
   {
      System.out.print("ENTER THE CODE TO ENCRYPT : ");
      Str=br.readLine();
      System.out.print("ENTER THE ALGORITHM FREQUENCY TO ENCRYPT : ");
      algoEnc=Integer.parseInt(br.readLine());
   }
  
   void inputDec()throws Exception
   {
      System.out.print("ENTER THE CODE TO DECRYPT : ");
      Str=br.readLine();
      System.out.print("ENTER THE ALGORITHM FREQUENCY TO DECRYPT : ");
      algoDec=Integer.parseInt(br.readLine());
   }

   void encrypt()
   {
      int algoN=algoEnc,c='A',s='a',n='0';
      for(int i=0;i<26;i++)
      { C[i]=(char)c++; S[i]=(char)s++; }
         for(int j=0;j<10;j++)
            N[j]=(char)n++;
         algoEnc%=26; algoN%=10;
         for(int i=0;i<Str.length();i++)
         {
            char ext;
            int tmp=Str.charAt(i);
            if(tmp>=65&&tmp<=90) //CAPITAL A-Z
            { if(algoEnc>=0) // POSITIVE ALGORITHM
              { int pos=tmp-65;
                if(pos+algoEnc>25) Enc+=C[-1+(algoEnc-(25-pos))];
                else Enc+=C[pos+algoEnc];
              }
              else // NEGATIVE ALGORITHM
              { int pos=tmp-65;
                if(pos+algoEnc<0) Enc+=C[26+(pos+algoEnc)];
                else Enc+=C[pos+algoEnc];
              }
            }
            else if(tmp>=97&&tmp<=122) //SMALL A-Z
            { if(algoEnc>=0) // POSITIVE ALGORITHM
              { int pos=tmp-97;
                if(pos+algoEnc>25) Enc+=S[-1+(algoEnc-(25-pos))];
                else Enc+=S[pos+algoEnc];
              }
              else // NEGATIVE ALGORITHM
              { int pos=tmp-97;
                if(pos+algoEnc<0) Enc+=S[26+(pos+algoEnc)];
                else Enc+=S[pos+algoEnc];
              }
            }
            else if(tmp>=48&&tmp<=57) //NUMBERS 0-9
            { if(algoN>=0) // POSITIVE ALGORITHM
              { int pos=tmp-48; 
                if(pos+algoN>9) Enc+=N[-1+(algoN-(9-pos))];
                else Enc+=N[pos+algoN];
              }
              else // NEGATIVE ALGORITHM
              { int pos=tmp-48; 
                if(pos+algoN<0) Enc+=N[10+(pos+algoN)];
                else Enc+=N[pos+algoN];
              }
            }
            else Enc+=(char)tmp;
         }
    }

    void decrypt()
    {
      int algoN=algoDec,c='A',s='a',n='0';
      for(int i=0;i<26;i++)
      { C[i]=(char)c++; S[i]=(char)s++; }
      for(int j=0;j<10;j++)
         N[j]=(char)n++;
      algoDec%=26; algoN%=10;
      for(int i=0;i<Str.length();i++)
      {
         char ext;
         int tmp=Str.charAt(i);
         if(tmp>=65&&tmp<=90) //CAPITAL A-Z
         { if(algoDec>=0) // POSITIVE ALGORITHM
           { int pos=tmp-65;
             if(pos-algoDec<0) Dec+=C[26-(algoDec-pos)];
             else Dec+=C[pos-algoDec];
           }
           else // NEGATIVE ALGORITHM
           { int pos=tmp-65;
             if(pos-algoDec>25) Dec+=C[-1+((pos-algoDec)-25)];
             else Dec+=C[pos-algoDec];
           }
         }
         else if(tmp>=97&&tmp<=122) //SMALL A-Z
         { if(algoDec>=0) // POSITIVE ALGORITHM
           { int pos=tmp-97;
             if(pos-algoDec<0) Dec+=S[26-(algoDec-pos)];
             else Dec+=S[pos-algoDec];
           }
           else // NEGATIVE ALGORITHM
           { int pos=tmp-97;
             if(pos-algoDec>25) Dec+=S[-1+((pos-algoDec)-25)];
             else Dec+=S[pos-algoDec];
           }
         }
         else if(tmp>=48&&tmp<=57) //NUMBERS 0-9
         { if(algoN>=0) // POSITIVE ALGORITHM
           { int pos=tmp-48; 
             if(pos-algoN<0) Dec+=N[10-(algoN-pos)];
             else Dec+=N[pos-algoN];
           }
           else // NEGATIVE ALGORITHM
           { int pos=tmp-48; 
             if(pos-algoN>9) Dec+=N[-1+((pos-algoN)-9)];
             else Dec+=N[pos-algoN];
           }
         }
         else Dec+=(char)tmp;
      }
   }

   public static void main(String args[])throws Exception
   {
      Encryptor_Decoder obj=new Encryptor_Decoder();
      int choice,pflag=0,iflag=0;
      String d;
      do
      {
        do
        {
          System.out.print("\nSELECT THE CODE OF OPTION TO PERFORM\n1: 
                             ENCRYPT CODE  2: DECRYPT CODE 3: EXIT");
          System.out.print("\nENTER YOUR CHOICE : ");
          choice=Integer.parseInt(br.readLine());
          if(choice>=1&&choice<=3) iflag=1;
          else System.out.println("INVALID CHOICE TRY AGAIN\n");
        }while(iflag==0);
        switch(choice)
        {
          case 1:
          obj.inputEnc();
          obj.encrypt();
          System.out.print("ENCRYPTED CODE : "+Enc);
          break;
          case 2:
          obj.inputDec();
          obj.decrypt();
          System.out.print("DECRYPTED CODE : "+Dec);
          break;
          case 3:
          pflag=1;
          break;
        }
        if(pflag==0)
        {
          System.out.print("\nCONTINUE EXECUTION ? (Y) : ");
          d=br.readLine();
          if(d.equalsIgnoreCase("y")) pflag=0;
          else pflag=1;
        }
      }while(pflag!=1);
      System.out.println("\nPROGRAM SUCCESSFULLY TERMINATED :
       CLOSE TERMINAL WINDOW TO EXIT");
   }
 }

0 comments :

Post a Comment