Class SubstitutionMessage

Object
OpenMessage
SubstitutionMessage
All Implemented Interfaces:
EncryptedMessage, Message

public class SubstitutionMessage extends OpenMessage implements EncryptedMessage
  • Field Details

    • messageKey

      protected Key messageKey
      Note that all you need to declare here is messageKey, as to, from, subject, and body are handled in OpenMessage, which SubstitutionMessage inherits fom.
    • ALPHABET

      private static final String ALPHABET
      See Also:
  • Constructor Details

  • Method Details

    • encrypt

      public String encrypt(String message, Key key)
      Encrypts the String passed into it using a Substitution Cipher. Any characters that aren't in the ALPHABET variable will not be encrypted, they will remain the original character.

      Example output:

                 Key key = new Key("cheshirecat", "2pHZWoYd8cTbJ0n1fmeyOs6Buk4VX5i3hvQKzD)GgUPF9xjISRLwl.ANaM(rE7tq?C", "S");
                 Key key2 = new Key("alice", "AMivN0z3qwVcTH4W9xsEGjhJ2tlDp(5uLoFRg8r.fXOQP6Ym?Ie)dky7UKC1bZSanB", "S");
                 SubstitutionMessage message1 = new SubstitutionMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!", key);
                 SubstitutionMessage message2 = new SubstitutionMessage("Alice", "Hatter", "Celebration", "Happy Un-Birthday!!!", key2);
                 System.out.println(message1.encrypt(message1.getBody(), key));
                 System.out.println(message2.encrypt(message2.getBody(), key2));
                 
      • First line prints ZgG'x Vi D4xi 3gF gjF IiFL Q)UgFx4Gx 54xi!
      • Second line prints 3lXXe G.-MFQ6o(le!!!
      Specified by:
      encrypt in interface EncryptedMessage
      Parameters:
      message - String to encrypt
      key - Key used to encrypt the message
      Returns:
      String containing encrypted message
    • decrypt

      public String decrypt(String message, Key key)
      Decrypts the String passed in.

      Example output:

                 Key key = new Key("cheshirecat", "2pHZWoYd8cTbJ0n1fmeyOs6Buk4VX5i3hvQKzD)GgUPF9xjISRLwl.ANaM(rE7tq?C", "S");
                 Key key2 = new Key("alice", "AMivN0z3qwVcTH4W9xsEGjhJ2tlDp(5uLoFRg8r.fXOQP6Ym?Ie)dky7UKC1bZSanB", "S");
                 SubstitutionMessage message1 = new SubstitutionMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!", key);
                 SubstitutionMessage message2 = new SubstitutionMessage("Alice", "Hatter", "Celebration", "Happy Un-Birthday!!!", key2);
                 String encryptedMessage1 = "ZgG'x Vi D4xi 3gF gjF IiFL Q)UgFx4Gx 54xi!";
                 String encryptedMessage2 = "3lXXe G.-MFQ6o(le!!!";
                 System.out.println(message1.decrypt(encryptedMessage1, key));
                 System.out.println(message2.decrypt(encryptedMessage2, key2));
                 
      • First line prints Don't be late for our very important date!
      • Second line prints Happy Un-Birthday!!!
      Specified by:
      decrypt in interface EncryptedMessage
      Parameters:
      message - String to decrypt
      key - Key used to decrypt the message
      Returns:
      String containing decrypted message