Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Artichoke Cipher Assignment Background In the week 6 lab we encrypted a word by

ID: 3680128 • Letter: A

Question

Artichoke Cipher Assignment

Background

In the week 6 lab we encrypted a word by shifting each letter a fixed number of positions later in the alphabet. Letters that would have shifted beyond the end of the alphabet wrapped back about to the front of it. This approach is known as a Caesar Cipher, after Julius Caesar, who used it to encrypt important communications.

Artichoke Cipher

Much later President Thomas Jefferson, in preparing Lewis and Clark for their expedition, developed what is called the Artichoke Cipher. Like the Caesar Cipher, it works by shifting characters a distance from their original position. But in this scheme the shift is not fixed. The characters of the original message are shifted by the character position of each of the characters in a keyword in turn, repeating the word as many times as necessary. Jefferson used "artichoke" as his keyword. In this assignment you will implement an artichoke cipher, allowing the user to choose the word to be used as the secret keyword (it need not be the word "artichoke").

The Alphabet

In this assignment we will assume that all messages are created using only the characters in the following string:

You can use this line in your program to define the character sequence. Note the escaped characters near the end, they are the single and double quotes. Characters are shifted according to their position in this sequence.

Example

In the sample input /output below, the keyword is artichoke and the first line of text to be encrypted is We are using Thomas Jefferson's Artichoke CIPHER system. Imagine the code word repeated above the text. Each character in the text is shifted by the character position in alphabet of the character above it.

gives

Decoding

To decode a message, use the reverse of the character sequence (that is, create a reversed version of the the string alphabet). Write some program code that will do this, don't just create a second constant. Then decoding works just like encoding, except for using this string instead of the original one.

Requirements

Your program should work as follows:

First ask the user for the name of the file to be encrypted.

You can assume that the input file will contain multiple lines of text.

The characters in these lines will only be from the character sequence alphabet.

Read whole lines of text in at a time using nextLine().

Then ask whether the file should be encoded or decoded.

Lastly ask for the word to be used as the key in the encryption process.

Your program should then process the input file, encrypting all the characters using the artichoke cipher approach.

The result should look just like the input file, except that the letters have been encrypted or decrypted as appropriate.

Example Input and Output

Suppose the file message.txt contains

Here is a sample user interaction (with user input in blue).

If the file had contained the encrypted text shown above and decryption was chosen, you should have gotten the original message back.

Suggestion

First get this program working just using the Caesar Cipher, as in the lab. Then work on the artichoke part. Here is some sample output when the Caesar Cipher approach is used.

What to Turn In

A printout of your Java code. Upload in Moodle a zip of your whole BlueJ project folder as well so that I can test it. Don't just upload a .java file.

Explanation / Answer

package com.sanfoundry.setandstring;

import java.util.Scanner;

public class Cipher

{

    public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

    public static String encrypt(String plainText, int shiftKey)

    {

        plainText = plainText.toLowerCase();

        String cipherText = "";

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

        {

            int charPosition = ALPHABET.indexOf(plainText.charAt(i));

            int keyVal = (shiftKey + charPosition) % 26;

            char replaceVal = ALPHABET.charAt(keyVal);

            cipherText += replaceVal;

        }

        return cipherText;

    }

    public static String decrypt(String cipherText, int shiftKey)

    {

        cipherText = cipherText.toLowerCase();

        String plainText = "";

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

        {

            int charPosition = ALPHABET.indexOf(cipherText.charAt(i));

            int keyVal = (charPosition - shiftKey) % 26;

            if (keyVal < 0)

            {

                keyVal = ALPHABET.length() + keyVal;

            }

            char replaceVal = ALPHABET.charAt(keyVal);

            plainText += replaceVal;

        }

        return plainText;

    }

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the String for Encryption: ");

        String message = new String();

        message = sc.next();

        System.out.println(encrypt(message, 3));

        System.out.println(decrypt(encrypt(message, 3), 3));

        sc.close();

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote