Class SubstitutionMessage
Object
OpenMessage
SubstitutionMessage
- All Implemented Interfaces:
EncryptedMessage,Message
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprotected KeyNote that all you need to declare here is messageKey, as to, from, subject, and body are handled in OpenMessage, which SubstitutionMessage inherits fom.Fields inherited from class OpenMessage
body, from, subject, to -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class OpenMessage
getBody, getFrom, getSubject, getTo, search, searchFrom, searchSubject, searchTo, toString
-
Field Details
-
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
- See Also:
-
-
Constructor Details
-
SubstitutionMessage
-
-
Method Details
-
encrypt
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:
encryptin interfaceEncryptedMessage- Parameters:
message- String to encryptkey- Key used to encrypt the message- Returns:
- String containing encrypted message
-
decrypt
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:
decryptin interfaceEncryptedMessage- Parameters:
message- String to decryptkey- Key used to decrypt the message- Returns:
- String containing decrypted message
-