Write a method caesarencode that takes a string message

Assignment Help Computer Network Security
Reference no: EM132098902

Please do not omit coding structure and please write a main method for it. Also,

Question: Cipher

Caesar's cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message so that only those authorized will be able to read it. Caesar's cipher conceals a message by replacing each letter in the original message (the plaintext), by a letter corresponding to a certain number of letters to the right on the alphabet.

Of course, the message can be retrieved by replacing each letter in the encoded message (the ciphertext) with the letter corresponding to the same number of position to the left on the alphabet.

To achieve this, the cipher has a key that needs to be kept private. Only those with the key can encode and decode a message. Such a key determines the shift that needs to be performed on each letter. For example, here is how a string containing the entire alphabet will be encrypted using a key equal to 3:

Original: abcdefghijklmnopqrstuvwxyz
Encrypted: defghijklmnopqrstuvwxyzabc

Vigenere's cipher is a slightly more complex encryption scheme, also used to transform a message. The key of this cipher consists of a word and the cipher works by applying multiple Caesar ciphers based on the letters of the keyword. Each letter can be associated with a number corresponding to its position in the English alphabet (counting from 0). For instance, the letter `a' is associated to 0, `c' to 2, and `z' to 25. Therefore, the keyword of the cipher will provide as many integers as letters in the word and these integers will be used to implement different Caesar ciphers.

Let's see how: suppose the message to encrypt is "elephants" and the keyword is "rats". The first thing to do is to repeat the keyword until its length matches the one of the message.

Message: e l e p h a n t s
Keyword: r a t s r a t s r

Now, each letter of "ratsratsr" is associated to both a letter in the message and an integer. We can encrypt each letter of the message using a Caesar cipher where the key corresponds to the integer associated to it through the keyword. In this case `r' corresponds to 17, so the first letter of the message which is an `e' will be encrypted using a `v', the second letter `l' as an `l' since `a' is associated to 0, and so on. The entire message will be encrypted as "vlxhyaglj".

The goal of this exercise is to write several methods in order to create a program that encodes and decodes messages using Caesar's and Vigenere's ciphers. For the purpose of this exercise we will only consider messages written using lower case letters and blank spaces. All the code for this question must be placed in a file named Cipher.java.

a. Encoding a character

Let's start by writing a simple method called charRightShift which takes a character and an integer n as inputs, and returns a character. The method should verify that the integer is a number between 0 and 25 (both included). If that's not the case, the method should print out an error message and return the character with ASCII value 0.

Note that ASCII value 0 is not '0', but is the char that maps to the value 0! Otherwise, if the character received as input is a lower case letter of the English alphabet, the method will return the letter of the alphabet which is n positions to the right on the alphabet. If the character received as input is not a lower case letter of the English alphabet, then the method returns the character itself with no modification.

For example:

charRightShift(`g', 2 ) returns `i',
charRightShift(`#', 2 ) returns `#', and
charRightShift(`h', 32 ) returns the character with ASCII 0 and prints an error message.

b. Decoding a character

Write a method charLeftShift which practically reverses what the previous method does. This method also takes a character and an integer n as inputs, and returns a character. The method should verify that the integer is a number between 0 and 25 (both included).

If that's not the case it should print out an error message and return the character with ASCII value 0. Note that ASCII value 0 is not '0', but is the char that maps to the value 0!

Otherwise, if the character received as input is a lower case letter of the English alphabet, the method will return the letter of the alphabet which is n positions to the left on the alphabet. If the character received as input is not a lower case letter of the English alphabet, then the method returns the character itself with no modification.

For example:

charLeftShift(`i', 2 ) returns `g',
charLeftShift(`#', 2 ) returns `#', and
charLeftShift(`h', 32 ) returns the character with ASCII 0 and prints an error message.

Note: The two methods above are very similar. This suggests that you write one common method charShift which contains the shifting logic and can shift both left and right. Then charRightShift can simply call charShift with a positive n, and charLeftShift can call charShift with a negative version of n.

c. Caesar's cipher - Encoding

Write a method caesarEncode that takes a String message and an int key as inputs and returns the string obtained by encrypting message using the Caesar's cipher with key equal to key. To create the encrypted string you need to replace each letter in message, by the letter corresponding to key letters to the right on the alphabet. You should call and use charRightShift appropriately in order to get full points.

The input key must be an integer from 0 to 25 (included). Your method should print out an error message and return the empty string if that's not the case.

For the purpose of this exercise you can assume that the strings to encrypt will only contain letters from the English alphabet in lower case and blank spaces. Blank spaces don't get modified by the encryption.

For example, caesarEncode(``cats and dogs'', 5) should return ``hfyx fsi itlx''.

d. Caesar's cipher - Decoding

Write a method caesarDecode that takes a String message and an int key as inputs and retunrs the string obtained by decrypting message using the Caesar's cipher with key equal to key.

To decrypt the string you need to replace each letter in message, by the letter corresponding to key letters to the left on the alphabet. To get full points, you should call and use the method charLeftShift appropriately.

As for caesarEncode, the key must be a number between 0 and 25. Your method should print an error message and return an empty string if that's not the case. More over, you can expect strings to contain only lower case letters from the English alphabet and blank spaces which will not be modi ed by the decryption (as they were not modified by the encryption).

For example, caesarDecode(``hfyx fsi itlx'', 5) should return ``cats and dogs''.

e. From String to keys

Write a method called obtainKeys which takes a String as input and returns an array of integers. The size of the array will be equal to the length of the String. The elements of the array correspond to the position (counting from 0) of each character in the String as a letter of the English alphabet.

For instance obtainKeys(``hello'') returns [7, 4, 11, 11, 14].

For the purpose of this exercise you can assume that the input String to this method will only contain lower case letters of the English alphabet.

f. Vigenere's cipher - Encoding

Write a method vigenereEncode that takes a String message and a String keyword as inputs and returns the string obtained by encrypting message using the Vigenere's cipher with key equal to keyword.

Remember that this cipher rst associates each letter of the keyword to a letter of the message. Then it shifts (to the right) each letter of the message by the number of positions determined by the corresponding letter in the keyword. Use the methods obtainKeys and charRightShift appropriately in order to implement the encryption.

The input keyword must contain only characters from the lower case English alphabet. Your method should print out an error message and return the empty string if that's not the case.

For the purpose of this exercise you can assume that the strings to encrypt will only contain letters from the English alphabet in lower case and blank spaces. Blank spaces don't get modi ed by the encryption.

For example, vigenereEncode(``elephants and hippos'', ``rats'') should return ``vlxhyagljtfu aagphk''.

g. Vigenere's cipher - Decoding

Finally, write a method vigenereDecode that takes a String message and a String keyword as inputs and returns the string obtained by decrypting the message using the Vigenere's cipher with key equal to keyword. Remember that this cipher rst associates each letter of the keyword to a letter of the message.

Then it shifts (to the left) each letter of the message by the number of positions determined by the corresponding letter in the keyword. Use the methods obtainKeys and charLeftShift appropriately in order to implement the decryption.

Again, the input keyword must contain only characters from the lower case English alphabet. Your method should print out an error message and return the empty string if that's not the case.

For the purpose of this exercise you can assume that the strings to decrypt will only contain letters from the English alphabet in lower case and blank spaces.

For example, vigenereDecode(``vlxhyaglj tfu aagphk'', ``rats'') should return ``elephants and hippos''.

Reference no: EM132098902

Questions Cloud

Create a gui application that allows the user to enter data : The remainder goes to the movie company. Create a GUI application that allows the user to enter the following data .
Discuss about the snmp tools : In order to ensure quality of service, what are the measured metrics employed in trunk test system and traffic measurement system.
What is the sequence number in the corresponding : Compare the GBN, SR, and TCP protocols as approaches toward pipelined error recovery for reliable data transfer.
What is apriorialgorithm : What is Association Rule? Discuss with example? What is Apriorialgorithm,discuss its advantages and disadvantages?
Write a method caesarencode that takes a string message : Let's start by writing a simple method called charRightShift which takes a character and an integer n as inputs, and returns a character.
Why have we moved to python : Why have we moved to Python? Feature-by-feature, how powerful is BASIC vs. Python?
Create a gui application that allows the user to calculate : A retail company must file a monthly sales tax report listing the total sales for the month, and the amount of state and county sales tax collected.
Write an oop program to store integers into a linked list : Write an OOP program to store integers into a linked list. Add code so that the linked list sorted.
Calculate and return the tip on a restaurant bill : Write a C# function called calcTip to calculate and return the tip on a restaurant bill.

Reviews

Write a Review

Computer Network Security Questions & Answers

  An overview of wireless lan security - term paper

Computer Science or Information Technology deals with Wireless LAN Security. Wireless LAN Security is gaining importance in the recent times. This report talks about how vulnerable are wireless LAN networks without any security measures and also talk..

  Computer networks and security against hackers

This case study about a company named Magna International, a Canada based global supplier of automotive components, modules and systems. Along with the company analysis have been made in this assignment.

  New attack models

The Internet evolution is and is very fast and the Internet exposes the connected computers to attacks and the subsequent losses are in rise.

  Islamic Calligraphy

Islamic calligraphy or Arabic calligraphy is a primary form of art for Islamic visual expression and creativity.

  A comprehensive study about web-based email implementation

Conduct a comprehensive study about web-based email implementation in gmail. Optionally, you may use sniffer like wireshark or your choice to analyze the communication traffic.

  Retention policy and litigation hold notices

The purpose of this project is to provide you with an opportunity to create a document retention policy. You will also learn how to serve a litigation hold notice for an educational institute.

  Tools to enhance password protection

A report on Tools to enhance Password Protection.

  Analyse security procedures

Analyse security procedures

  Write a report on denial of service

Write a report on DENIAL OF SERVICE (DoS).

  Phising email

Phising email It is multipart, what are the two parts? The HTML part, is it inviting the recepient to click somewhere? What is the email proporting to do when the link is clicked?

  Express the shannon-hartley capacity theorem

Express the Shannon-Hartley capacity theorem in terms of where is the Energy/bit and is the psd of white noise.

  Modern symmetric encryption schemes

Pseudo-random generators, pseudo-random functions and pseudo-random permutations

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd