/** * 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." );
}
}
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.