Sunday, 12 October 2014

Program to encrypt the given text message Ceaser Cipher

Caesar cipher encryption an decryption in java


public interface EncryptData {
    public void encrypt();
    public void decrypt();
    }



public class SpaceEliminator {
    String SplitStr="";
    String SplitResult="";
   
    SpaceEliminator(String x)
    {
    SplitStr=x;
   
   
    }
    public String toString()
    {
        SplitResult=SplitStr.replaceAll("\\s+","");
        return SplitResult;
    }
 }





public class CeaserCipher extends SpaceEliminator implements EncryptData {

    String EncyptedData = "";
    String DecyptedData = "";
    String InputString;
    CeaserCipher(String x) {
        super(x);
       
    }
    public void getString(String data)
    {
    InputString=data;   
    }
    public void encrypt()
    {
       
        char[] alpha={'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'};
        char[] CharStr=InputString.toCharArray();
        for(int i=0;i<CharStr.length;i++){
            for(int j=0;j<alpha.length;j++){
                if(CharStr[i]==alpha[j]){
                    if(j==23)
                        EncyptedData +=alpha[0];
                            else if(j==24)
                            EncyptedData +=alpha[1];
                            else if(j==25)
                                EncyptedData +=alpha[2];
                            else
                                EncyptedData +=alpha[j+3];
                        }
            }
        }
        System.out.println("Encypted String "+EncyptedData);
               
       
    }
    public void decrypt()
    {
   
       
        char[] alpha={'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'};
        char[] CharStr=EncyptedData.toCharArray();
        for(int i=0;i<CharStr.length;i++){
            for(int j=0;j<alpha.length;j++){
                if(CharStr[i]==alpha[j]){
                    if(j==0)
                        DecyptedData +=alpha[23];
                            else if(j==1)
                            DecyptedData +=alpha[24];
                            else if(j==2)
                                DecyptedData +=alpha[25];
                            else
                                DecyptedData +=alpha[j-3];
                        }
            }
    }
        System.out.println("Decypted String "+DecyptedData);
    }
   
}








No comments:

Post a Comment