Write two methods encrypt(message,key) and decrypt(message,key) to encrypt and decrypt messages with a key.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 21PE
icon
Related questions
Question

please write in JAVA

Question 4
Write two methods
encrypt(message,key) and
decrypt(message,key)
to encrypt and decrypt messages with a key.
You are going to use ASCII values of each character. For example, the character 'A' is encoded using the
number 65 and 'B' is encoded using the number 66. Given a key, you will use the following method to
each letter of the message:
if (OriginalChar + Key > 126) then
EncryptedChar = 32 + ((OriginalChar + Key) - 127)
else
EncryptedChar = (OriginalChar + Key)
For example, if the enemy uses Key = 10 then the message "Hey" would initially be represented as:
Character
ASCII code
H
72
e
101
y
121
"Hey" would be converted to "Ro$:
Encrypted H = (72 + 10) = 82 = R in ASCII
Encrypted e = (101 + 10) = 111 = o in ASCII
Encrypted y = 32 + ((121 + 10) - 127) = 36 = $ in ASCII
After conversion, the converted message will be circularly shifted left 2 characters. For example, the
message “Hey" will be encrypted as "$Ro" after circular shift operation. The methods encrypt and
decrypt will return the following result:
encrypt("Hey", 10) → “$Ro"
decrypt("SRo", 10) > "Неy"
Suppose that key =17, encrypt and decrypt the following message texts. Print encrypted and decrypted
messages.
msg1 = "Lost time is never found again."
msg2 = "Use it or lose it."
Transcribed Image Text:Question 4 Write two methods encrypt(message,key) and decrypt(message,key) to encrypt and decrypt messages with a key. You are going to use ASCII values of each character. For example, the character 'A' is encoded using the number 65 and 'B' is encoded using the number 66. Given a key, you will use the following method to each letter of the message: if (OriginalChar + Key > 126) then EncryptedChar = 32 + ((OriginalChar + Key) - 127) else EncryptedChar = (OriginalChar + Key) For example, if the enemy uses Key = 10 then the message "Hey" would initially be represented as: Character ASCII code H 72 e 101 y 121 "Hey" would be converted to "Ro$: Encrypted H = (72 + 10) = 82 = R in ASCII Encrypted e = (101 + 10) = 111 = o in ASCII Encrypted y = 32 + ((121 + 10) - 127) = 36 = $ in ASCII After conversion, the converted message will be circularly shifted left 2 characters. For example, the message “Hey" will be encrypted as "$Ro" after circular shift operation. The methods encrypt and decrypt will return the following result: encrypt("Hey", 10) → “$Ro" decrypt("SRo", 10) > "Неy" Suppose that key =17, encrypt and decrypt the following message texts. Print encrypted and decrypted messages. msg1 = "Lost time is never found again." msg2 = "Use it or lose it."
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Public key encryption
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning