Question

JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

JAVA PROJECT

Part 1 - Normalize Text

The first thing we will do is normalize the input message so that it’s easier to work with.

Write a method called normalizeText which does the following:

Removes all the spaces from your text

Remove any punctuation (. , : ; ’ ” ! ? ( ) )

Turn all lower-case letters into upper-case letters

Return the result.

The call

normalizeText(“This is some \“really\” great. (Text)!?”)

should return

“THISISSOMEREALLYGREATTEXT”
 

Part 2 - Obfuscation

We’ll be turning out text into Obish, a language game designed to let people who know the rules talk to each other privately (sort of like Pig Latin but less well-known). The rules for Obish are simple: you simply insert the syllable “ob” in front of every vowel sound. So “mister ackerman” becomes “mobistober obackobermoban”. With a little practice you can learn to speak this with your friends when you need to hide a conversation from outsiders. This part isn’t really encryption, but it does make your message a little harder for people who aren’t in the know to understand.

Write a method called obify that takes a String parameter (the message to be obfuscated) and returns a string in which every vowel (A, E, I, O, U, Y) is preceded by the letters “OB” (be sure to use capital letters).

If we call obify on “THISISSOMEREALLYGREATTEXT”, it should return

“THOBISOBISSOBOMOBEROBEOBALLOBYGROBEOBATTOBEXT”

Note: In spoken Obish, you normally wouldn’t put an “ob” in front of the double vowel sound “EA” in “GREAT”, but we want to keep our program simple so we’ll insert “OB” in front of both vowels.

Part 3 - Unobfuscation

Write a method called unobify that takes a string in Obish and returns the string in the original language. So if you call:

String plainText = unobify(“OBI LOBIKOBE CHOBEOBESOBE”);

then plainText would store “I LIKE CHEESE”.

Part 4 - Caesar Cipher

Next we’ll be writing a Caesar Cipher. The Caesar cipher is just about the simplest encryption algorithm out there. A Caesar encription "shifts" each individual character forward by a certain number or "key". Each letter in the alphabet is shifted to the letter in the alphabet that is "key" places past the original letter. With a shift value of +1, the string “ILIKEZOOS” would be rendered as “JMJLFAPPT.”

Write a method called caesarify that takes two parameters. The first argument is a string you want to encrypt, and the second is an integer that contains the shift value or "key". The function should return a string, which is the input string encrypted with the Caesar cypher using the shift value passed in its second argument. You may assume that the input string is normalized.

Note that the alphabet “wraps around”, so with a shift value of +1 the “Z” in ZOOS became an A.

You can also have negative shift values, which cause the alphabet to previous letters. With a -1 shift, the string “ILIKEAPPLES” would turn into “HKHJDZOOKDR.”

We will provide you with a function called shiftAlphabet. This function takes one argument, an integer to specify the shift value, and returns a string, which is the uppercase alphabet shifted by the shift value. So if you call shiftAlphabet(2), you will get back the following string: “CDEFGHIJKLMNOPQRSTUVWXYZAB”

Here is the implementation for shiftAlphabet, which you can just paste into your java file:

public static String shiftAlphabet(int shift) {
    int start = 0;
    if (shift < 0) {
        start = (int) 'Z' + shift + 1;
    } else {
        start = 'A' + shift;
    }
    String result = "";
    char currChar = (char) start;
    for(; currChar <= 'Z'; ++currChar) {
        result = result + currChar;
    }
    if(result.length() < 26) {
        for(currChar = 'A'; result.length() < 26; ++currChar) {
            result = result + currChar;
        }
    }
    return result;
}
 

Part 5 - Codegroups

Traditionally, encrypted messages are broken into equal-length chunks, separated by spaces and called “code groups.”

Write a method called groupify which takes two parameters. The first parameter is the string that you want to break into groups. The second argument is the number of letters per group. The function will return a string, which consists of the input string broken into groups with the number of letters specified by the second argument. If there aren’t enough letters in the input string to fill out all the groups, you should “pad” the final group with x’s. So groupify(“HITHERE”, 2) would return “HI TH ER Ex”.

You may assume that the input string is normalized.

Note that we use lower-case ‘x’ here because it is not a member of the (upper-case) alphabet we’re working with. If we used upper-case ‘X’ here we would not be able to distinguish between an X that was part of the code and a padding X.

Part 6 - Putting it all together

Write a function called encryptString which takes three parameters: a string to be encrypted, an integer shift value, and a code group size. Your method should return a string which is its cyphertext equivalent. Your function should do the following:

Call normalizeText on the input string.

Call obify to obfuscate the normalized text.

Call caesarify to encrypt the obfuscated text.

Call groupify to break the cyphertext into groups of size letters.

Return the result

Part 7 - Hacker Problem - Decrypt

Write a method called ungroupify which takes one parameter, a string containing space-separated groups, and returns the string without any spaces. So if you call ungroupify(“THI SIS ARE ALL YGR EAT SEN TEN CEx”) you will return “THISISAREALLYGREATSENTENCE”

Now write a function called decryptString which takes three parameters: a string to be decrypted and the integer shift value used to encrypt the string, and returns a string which contains the (normalized) plaintext. You can assume the string was encrypted by a call to encryptString().

So if you were to call

String cyphertext = encryptString(“Who will win the election?”,  5, 3);
String plaintext = decryptString(cyphertext, 5);

… then you’ll get back the normalized input string “WHOWILLWINTHEELECTION”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class StringOperations{

                public static String normalizeText(String s){

                                return s.replaceAll("[^a-zA-Z]+", "").toUpperCase();

                }

                public static String unobify(String s){

                               

                                int index;

                                index = s.indexOf("OBA");

                                while(index>=0){

                                                s = s.substring(0,index)+"A"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBA");

                                }

                                index = s.indexOf("OBE");

                                while(index>=0){

                                                s = s.substring(0,index)+"E"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBE");

                                }

                                index = s.indexOf("OBI");

                                while(index>=0){

                                                s = s.substring(0,index)+"I"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBI");

                                }

                                index = s.indexOf("OBO");

                                while(index>=0){

                                                s = s.substring(0,index)+"O"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBO");

                                }

                                index = s.indexOf("OBU");

                                while(index>=0){

                                                s = s.substring(0,index)+"U"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBU");

                                }

                                index = s.indexOf("OBY");

                                while(index>=0){

                                                s = s.substring(0,index)+"Y"+s.substring(index+3,s.length());

                                                index = s.indexOf("OBY");

                                }

                                return s;

                }

               

                public static String obify(String s){

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

                                                //adding OB infront of vowels

                                                if((s.charAt(i) == 'A') || (s.charAt(i) == 'E') || (s.charAt(i) == 'I') ||(s.charAt(i) == 'O') ||(s.charAt(i) == 'U')||(s.charAt(i) == 'Y')) {

                                                                s = s.substring(0, i) + "OB" + s.substring(i, s.length());

                                                                i=i+2;

                                                }

                                }

                                return s;

                }

               

                public static String shiftAlphabet(int shift) {

                                int start = 0;

                                if (shift < 0) {

                                                start = (int) 'Z' + shift + 1;

                                } else {

                                                start = 'A' + shift;

                                }

                                String result = "";

                                char currChar = (char) start;

                                for(; currChar <= 'Z'; ++currChar) {

                                                result = result + currChar;

                                }

                                if(result.length() < 26) {

                                                for(currChar = 'A'; result.length() < 26; ++currChar) {

                                                                result = result + currChar;

                                                }

                                }

                                return result;

                }

               

                public static String caesarify(String s, int key){

                               

                                String cipher = "";

                                String alphabet = shiftAlphabet(0); //normal alphabet

                                String rotatedAlphabet = shiftAlphabet(key); //rotated alphabet

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

                                                //find respective char of normal alphabet in rotated alphabet using index

                                                cipher = cipher + rotatedAlphabet.charAt(alphabet.indexOf(s.charAt(i)));

                                }

                                return cipher;

                }

               

                public static String groupify(String s, int key){

                                int pad = s.length()%key;

                                //padding x's to string

                                while(pad>0){

                                                s = s + 'x';

                                                pad--;

                                }

                               

                                //adding space

                                for(int i=key;i<s.length();i=i+key){

                                                s = s.substring(0,i)+" "+s.substring(i,s.length());

                                                i++;

                                }

                                return s;

                }

               

                public static String encryptString(String s, int key1, int key2){

                                s = normalizeText(s);

                                s = obify(s);

                                s = caesarify(s,key1);

                                s = groupify(s,key2);

                                return s;

                }

               

                public static String ungroupify(String s){

                                s = s.replaceAll("\\s+","");

                                s = s.replaceAll("x","");

                                return s;

                }

                public static String decryptString(String s, int key){

                                s = ungroupify(s);

                                s = caesarify(s,-1*key); //decryption of caesar cipher is just rotating string reverse

                                s = unobify(s);

                                return s;

                }

                               

                public static void main(String[] args){

                               

                                String ns = normalizeText("This is some \"really\" great. (Text)!?");

                                System.out.println("Normalized text: "+ns);

                                String obf = obify(ns);

                                System.out.println("Obified Text: "+obf);

                                String uobf = unobify(obf);

                                System.out.println("UnObified Text: "+uobf);

                                String cr = caesarify("ILIKEAPPLES",-1);

                                System.out.println("Encryption of \"ILIKEAPPLES\": "+cr);

                               

                                String gr = groupify("HITHERE",2);

                                System.out.println("Groupified Text: "+gr);

                               

                                String ugr = ungroupify(gr);

                                System.out.println("UnGroupified Text: "+ugr);

                               

                                System.out.println("\n-----------Encryption and Decryption for \"Who will win the election?\"----------\n");

                                String cyphertext = encryptString("Who will win the election?", 5, 3);

                                System.out.println("Encrypted Text: "+cyphertext);

                                String plaintext = decryptString(cyphertext, 5);

                                System.out.println("Decrypted Text: "+plaintext);

                }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

  • Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...

    Using the website Repl.it Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The program should be able to shift a specific number of places based on the user's input number. For example Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. Ciphertext: QEBNRFZH YOLTK CLU GRJMP...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

  • Security is an important feature of information systems. Often, text is encrypted before being sent, and...

    Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located. For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT