Monday, October 13, 2014

Encrypt AND Decrypt (Assign Value)


public class CaesarCrypt {
   private final String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-";
  
  
   public String encrypt(String plainText)
   {
       
         String cipherText="";
               
         for(int i=0; i < plainText.length(); i++)
         {
          
            if (plainText.charAt(i) == 'a')
            {
               cipherText += "1";
            }
            if (plainText.charAt(i) == 'b')
            {
               cipherText += "2";
            }
            if (plainText.charAt(i) == 'c')
            {
               cipherText += "3";
            }
            if (plainText.charAt(i) == 'd')
            {
               cipherText += "4";
            }
         }
         return cipherText;
   }
  
   public String decrypt(String cipherText)
   {
      
         String plainText="";    
        
         for(int i=0;i<cipherText.length();i++)
         {
         
            if (cipherText.charAt(i) == '1')
            {
               plainText += "a";
            }
            if (cipherText.charAt(i) == '2')
            {
               plainText += "b";
            }
            if (cipherText.charAt(i) == '3')
            {
               plainText += "c";
            }
            if (cipherText.charAt(i) == '4')
            {
               plainText += "d";
            }
         }
        
         return plainText;
   }
}

No comments:

Post a Comment