Class Spy

java.lang.Object
  extended by Spy

public class Spy
extends Object

The Spy class contains methods to encrypt and decrypt messages, as well as information about the secret code names of Spies that are already operating.

This class should have ONE instance variable (you must not add any other instance variables):

This class should also have ONE static variable (you must not add any other static variables):


Constructor Summary
Spy(int n)
          Create a new Spy with code number n; Update the record of all existing code numbers according to the description above; If the specified code number already exists in the record, print a warning message to the user (hint: use System.out.println); You can assume that the user's input will be a 4-digit integer.
 
Method Summary
static String charToBinary(char c)
          Return the binary representation of character c; You must do this by first converting the character into an integer ASCII value, and then converting that integer into an 8-bit binary value; Note that the return type must be a String of length 8; This means that the ASCII value of the given character cannot be more than 255 -- if the character's ASCII value is greater than 255, then return the empty String ("").
static String decrypt(String encrypted, int key)
          Return the decrypted version of the encrypted String, using the given key; You should assume that the String was encrypted using Spy's encrypt method; If the encrypted String is not valid (i.e., it is not composed of 8-bit binary Strings), then return "cannot decrypt".
static String encrypt(String msg, int key)
          Return the encrypted version of the msg, using the given key, according to this encryption algorithm.
static int exclusiveOr(int i, int j)
          Return the result of applying the "exclusive-or" (XOR) operator to integers i and j; You can assume that the input integers will be either 1 or 0, representing true and false, respectively; This means that this method must return either 1 or 0, depending on the result of "i XOR j".
static String getAllCodeNumbers()
          Return the code numbers of all existing Spies.
 int getCodeNumber()
          Return the secret code number of this Spy.
static boolean removeCodeNumber(int c)
          Remove the specified code number c from the record of all existing code numbers; Return true once this is done; if the specified code number does not exist in the record, return false.
static String stretch(String binKey, int length)
          Return a new key that is the result of stretching the given binKey to be AT LEAST as long as the given length; The given binKey must be a String of bits!

The algorithm for this "stretching" is as follows: divide the binary key into two even halves (you can assume that the key has an even number of bits), then XOR the corresponding bits of each half to produce a new String of bits, and append this new String of bits to the end of the key; If the resulting key is still not as long as the given length, then repeat the process until the key is at least as long as the given length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Spy

public Spy(int n)
Create a new Spy with code number n; Update the record of all existing code numbers according to the description above; If the specified code number already exists in the record, print a warning message to the user (hint: use System.out.println); You can assume that the user's input will be a 4-digit integer.

Method Detail

getCodeNumber

public int getCodeNumber()
Return the secret code number of this Spy.


getAllCodeNumbers

public static String getAllCodeNumbers()
Return the code numbers of all existing Spies.


removeCodeNumber

public static boolean removeCodeNumber(int c)
Remove the specified code number c from the record of all existing code numbers; Return true once this is done; if the specified code number does not exist in the record, return false.


charToBinary

public static String charToBinary(char c)
Return the binary representation of character c; You must do this by first converting the character into an integer ASCII value, and then converting that integer into an 8-bit binary value; Note that the return type must be a String of length 8; This means that the ASCII value of the given character cannot be more than 255 -- if the character's ASCII value is greater than 255, then return the empty String ("").


exclusiveOr

public static int exclusiveOr(int i,
                              int j)
Return the result of applying the "exclusive-or" (XOR) operator to integers i and j; You can assume that the input integers will be either 1 or 0, representing true and false, respectively; This means that this method must return either 1 or 0, depending on the result of "i XOR j".


stretch

public static String stretch(String binKey,
                             int length)
Return a new key that is the result of stretching the given binKey to be AT LEAST as long as the given length; The given binKey must be a String of bits!

The algorithm for this "stretching" is as follows: divide the binary key into two even halves (you can assume that the key has an even number of bits), then XOR the corresponding bits of each half to produce a new String of bits, and append this new String of bits to the end of the key; If the resulting key is still not as long as the given length, then repeat the process until the key is at least as long as the given length.


encrypt

public static String encrypt(String msg,
                             int key)
Return the encrypted version of the msg, using the given key, according to this encryption algorithm.


decrypt

public static String decrypt(String encrypted,
                             int key)
Return the decrypted version of the encrypted String, using the given key; You should assume that the String was encrypted using Spy's encrypt method; If the encrypted String is not valid (i.e., it is not composed of 8-bit binary Strings), then return "cannot decrypt".