Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * This lab requires you to use Java\'s String methods, together * with the `

ID: 645703 • Letter: #

Question

/**
* This lab requires you to use Java's String methods, together
* with the ``remainder'' function, written a%b, which returns
* the remainder of a divided by b. In addition,
* the methods, encipher(String plainText) and
* decipher( String cipherText ) must use either the
* StringBuilder or the StringBuffer class in
* order to construct their return values, which are encrypted or decrypted
* Strings, respectively.
*
* @author UMD CS Department.
*
*/
public class Cipher {
   /*
   * Do NOT change this alphabet!
   */
   private final static String alphabet =
               "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   /*
   * This constant may be useful in the various computations that
   * you will be required to implement in this class.
   */
   private final static int alphaLength = alphabet.length();
   /*
   * The "key" is set by the client during the construction of a Cipher
   * object. The key is used to determine the "displacement" or the number of characters
   * to the right (if positive) or to the left (if negative) that the alphabet will be
   * shifted.
   */
   private int key;
  
   public Cipher( int key ) {
       throw new UnsupportedOperationException( "You must implement this method." );
   }
   /**
   * This method (function) takes plain Text (Strings) to cipher Text (Strings)
   * by reconstructing the plainText, character by character, into cipher Text.
   * It does this by using the "key" to transform each character.
   *
   * Note: this method should use the StringBuilder (or StringBuffer) class
   * to hold the return value. Teaching assistants will be scanning your
   * implementation to ensure that your code uses the String Builder
   * or String Buffer class.
   *
   * @param plainText
   * @return
   */
   public String encipher( String plainText ) {
       throw new UnsupportedOperationException( "You must implement this method." );
   }
  
   /**
   * This method (function) takes cipher Text (Strings) to plain Text (Strings)
   * by reconstructing the cipherText, character by character, into the plain Text.
   * It does this by using the key, along with some modular arithmetic, to transform
   * each character in the cipher Text.
   *
   * Note: your method should use the StringBuilder (or StringBuffer) class
   * to hold the return value. Teaching assistants will be scaning your
   * implementation to ensure that your code uses the StringBuilder or
   * StringBuffer class appropriately.
   *
   * @param cipherText
   * @return
   */
   public String decipher( String cipherText ) {
       throw new UnsupportedOperationException( "You must implement this method." );
   }
   public static void main(String[] args) {
       throw new UnsupportedOperationException( "You must implement this method." );
   }


}

CONTENTS Contents 1 Class Index 1.1 Class List . 2 Class Documentation 2.1 Cipher Class Reference 2.1.1 Detailed Description 2.1.2 . Member Function Documentation Index 3 1 Class Index 1.1 Class List Here are the classes, structs, unions and interfaces with brief descriptions: Cipher 2 Class Documentation 2.1 Cipher Class Reference Public Member Functions Cipher (int key) . String encipher (String plain Text) . String decipher (String cipherText) 2.1.1 Detailed Description This lab requires you to use Java's String methods, together with the "remainder" function, written ab, which returns the remainder of a divided by b. In addition, the methods, encipher (String plainText) and decipher ( String cipherText ) must use either the StringBuilder or the StringBufferclass in order to construct their return values, which are encrypted or decrypted Strings, respectively Author UMD CS Department 2.1.2 Member Function Documentation 2.1.2.1 String Cipher.decipher(String cipherText) [inline] This method (function) takes cipher Text (Strings) to plain Text (Strings) by reconstructing the cipherText, character by character, into the plain Text. It does this by using the key, along with some modular arithmetic, to transform each Generated on Sun Mar 22 2015 20:45:11 for Ciphers Lab by Doxygen

Explanation / Answer

public class Cipher {
private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

   private int key;

   public Cipher(int key){

       this.key = key;
   }
  
  
   public String encrypt(String plaintext)
{

String ciphertext="";
for(int i=0;i   {
int position = ALPHABET.indexOf(plaintext.charAt(i));
int keyVal = (key+position)%62;
char replace = this.ALPHABET.charAt(keyVal);
ciphertext += replace;
}
return ciphertext;
}
public String decrypt(String ciphertext)
{

String plaintext="";
for(int i=0;i   {
int position = this.ALPHABET.indexOf(ciphertext.charAt(i));
int keyVal = (position-key)%62;
if(keyVal<0)
{
keyVal = this.ALPHABET.length() + keyVal;
}
char replace = this.ALPHABET.charAt(keyVal);
plaintext += replace;
}
return plaintext;
}

public static void main(String args[])
{
String plaintext = "datamatics";
  
  
Cipher c = new Cipher(10);
  
String ciphertext = c.encrypt(plaintext);
System.out.println("Plain Text :" + plaintext);
System.out.println("Cipher Text :" + ciphertext);
  
String SamplePlainText = c.decrypt(ciphertext);
System.out.println("Your Plain Text :" + SamplePlainText);
}
}