Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code for a (basic) encryption approach is attached and should be completed to show how Strings are encrypted and decrypted. The code also showcases

The code for a (basic) encryption approach is attached and should be completed to show how Strings are encrypted and decrypted. The code also showcases how easy it is to (sneakily) intercept code and store information (in this case, by the original programmer). Add a component to your code that saves the developed SimpleEnigma object to a file.

JAVA CODE:

//Crypto.java

public interface Crypto {

/**

* @param unencryptedString The raw string to be encryped.

* @return The encrypted string.

*/

public String encrypt(String unencryptedString);

/**

* @param encryptedString An encrypted string.

* @return A decrypted string.

*/

public String decrypt(String encryptedString);

}

//CyptoError.java

/**

* A custom error class example.

*

*

*/

public class CryptoError extends Exception {

private static final long serialVersionUID = 1L;

public CryptoError(String message) {

super(message);

}

}

//Main.java

public class Main {

/**

* @param args (not used)

*/

public static void main(String[] args) {

SimpleEnigma enigma = new SimpleEnigma();

String enc = enigma.encrypt("Demo Text");

System.out.println("Secret message : " + enc);

String dec = enigma.decrypt(enc);

System.out.println("Decoded message: " + dec);

System.out.println("Not-so secret messages:");

System.out.println(enigma);

}

}

//SimpleEnigma.java

import java.io.Serializable;

import java.util.ArrayList;

public class SimpleEnigma implements Crypto, Serializable {

private static final long serialVersionUID = 1L;

private ArrayList backDoorMessages;

public SimpleEnigma() {

backDoorMessages = new ArrayList();

}

/**

* @param unencrypted

* @return

* @throws CryptoError

*/

private char crypted(char unencrypted) throws CryptoError {

if ((unencrypted - 10) < 18)

throw new CryptoError("Incoming char ASCII code must be >= 28!");

return (char) (unencrypted - 10);

}

/**

* @param encrypted

* @return

* @throws CryptoError

*/

private char decrypted(char encrypted) throws CryptoError {

if ((encrypted + 10) < 28)

throw new CryptoError("Ambiguous incoming ASCII code.");

return (char) (encrypted + 10);

}

/**

* @param unencryptedString

* @return

* @throws CryptoError

*/

@Override

public String encrypt(String unencryptedString) {

String encodedString = "";

try {

for (int i = 0; i < unencryptedString.length(); i++) {

encodedString += crypted(unencryptedString.charAt(i));

}

} catch (CryptoError e) {

e.printStackTrace();

} finally {

backDoorMessages.add(unencryptedString);

}

return encodedString;

}

/**

* @param unencryptedString

* @return

* @throws CryptoError

*/

@Override

public String decrypt(String encryptedString) {

// TODO You fill in the blanks

return null;

}

@Override

public String toString() {

String retString = "";

for (String mesg : backDoorMessages)

retString += mesg + " ";

return retString;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

Students also viewed these Databases questions

Question

I receive the training I need to do my job well.

Answered: 1 week ago