1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vladimir [108]
2 years ago
15

Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure by modern standards. The first

approach is called the Caesar Cipher, and is a simple "substitution cipher" where characters in a message are replaced by a substitute character. The second approach, due to Giovan Battista Bellaso (b 1505, d 1581), uses a key word, where each character in the word specifies the offset for the corresponding character in the message, with the key word wrapping around as needed.
Computers and Technology
1 answer:
xeze [42]2 years ago
6 0

Answer:

See explaination

Explanation:

//CryptoManager.java

public class CryptoManager {

static int LOWER_BOUND=32;

static int UPPER_BOUND=95;

/*This method determines if a string is within the allowable bounds of ASCII

codes according to the LOWER_BOUND and UPPER_BOUND characters. The parameter

plainText is the string to be encrypted. The method returns true if all

characters are within the allowable bounds, false if any character is outside.*/

public static boolean stringInBounds (String plainText)

{

boolean flag=true;

//determines if a string is within the allowable bounds of ASCII

//codes according to the LOWER_BOUND and UPPER_BOUND characters.

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

{

if(!((int)plainText.charAt(i)>=LOWER_BOUND && (int)plainText.charAt(i)<=UPPER_BOUND))

{ //false if any character is outside the bounds

flag=false;

break;

}

}

//returns true if all characters are within the allowable bounds

return flag;

}

/*This method encrypts a string according to the Caesar Cipher. The integer key

specifies an offset and each character in plainText is replaced by the character

the specified distance away from it. The parameter plainText is an uppercase

string to be encrypted. The parameter key is an integer that specifies the

offset of each character. The method returns the encrypted string.*/

public static String encryptCaesar(String plainText, int key)

{

//Wrap around the key, if it is greater than the UPPER_BOUND

key=Wrap_around(key);

//encrypted text

String res="";

//encryption

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

{

res+=Character.toString((char) ((int)plainText.charAt(i)+key));

}

//return result

return res;

}

/* This method decrypts a string according to the Caesar Cipher. The integer

key specifies an offset and each character in encryptedText is replaced by

the character "offset" characters before it. This is the inverse of the

encryptCaesar method. The parameter encryptedText is the encrypted string

to be decrypted, and key is the integer used to encrypt the original text.

The method returns the original plain text string.*/

public static String decryptCaesar(String encryptedText, int key){

//Wrap around the key, if it is greater than the UPPER_BOUND

key=Wrap_around(key);

//decrypted text

String org="";

//encryption

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

{

org+=Character.toString((char) ((int)encryptedText.charAt(i)-key));

}

//return result

return org;

}

public static int Wrap_around(int key)

{

while(key>UPPER_BOUND)

{

key-=(UPPER_BOUND-LOWER_BOUND);

}

return key;

}

/* This method encrypts a string according to the Bellaso Cipher. Each character

in plainText is offset according to the ASCII value of the corresponding

character in bellasoStr, which is repeated to correspond to the length of

plaintext. The method returns the encrypted string.*/

public static String encryptBellaso(String plainText, String bellasoStr)

{

//encrypted text

String res="";

//Adjust length of bellasoStr to plainText

while(bellasoStr.length()<plainText.length())

{

bellasoStr+=bellasoStr.substring(0,(plainText.length()-bellasoStr.length()));

}

//encryption

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

{

char c=(char)Wrap_around((int)plainText.charAt(i)+(int)bellasoStr.charAt(i) );

res+=Character.toString(c);

}

//return result

return res;

}

/*

This method decrypts a string according to the Bellaso Cipher. Each character

in encryptedText is replaced by the character corresponding to the character in

bellasoStr, which is repeated to correspond to the length of plainText. This is

the inverse of the encryptBellaso method. The parameter encryptedText is the

encrypted string to be decrypted, and bellasoStr is the string used to encrypt

the original text. The method returns the original plain text string.*/

public static String decryptBellaso(String encryptedText, String bellasoStr)

{

//decrypted text

String res="";

//Adjust length of bellasoStr to plainText

while(bellasoStr.length()<encryptedText.length())

{

bellasoStr+=bellasoStr.substring(0,(encryptedText.length()-bellasoStr.length()));

}

//decryption

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

{

char c=(char)Wrap_around((int)encryptedText.charAt(i)-(int)bellasoStr.charAt(i) );

res+=Character.toString(c);

}

//return result

return res;

}

}

You might be interested in
Emma opened a new document a Microsoft word and typed her assignment. She is now planning to save the documents for the first ti
ycow [4]

Answer:

E.  F12

Explanation:

3 0
3 years ago
__ is/are the amount of blank space between lines of text in a paragraph
Natali [406]
Indents? Double Space? one of those
5 0
3 years ago
Read 2 more answers
The requester of sensitive information should not receive access just because of his or her clearance, position, or rank. The re
JulsSmile [24]

Answer:

Need to know

Explanation:

Need to know is a policy or predetermined criterion by which the requester of an information is granted access to the requested information only when a valid need is establish and not just because of his or her clearance, position, or rank.  

7 0
3 years ago
How can you stay safe on the Internet? Check all that apply.
Harlamova29_29 [7]

Answer:

all of the above

Explanation:

7 0
2 years ago
Read 2 more answers
Application software (apps) includes all the programs that allow you to perform specific tasks or applications on a computer. In
bulgar [2K]

Answer:

Microsoft Excel / Spreadsheet software.

5 0
3 years ago
Other questions:
  • Desktop, laptop, and tablet computers, and mobile devices are classified as _______.
    7·1 answer
  • Your computer is configured to obtain an ipv4 address and dns server address automatically. what utility will help you to find t
    7·1 answer
  • Please help?!
    11·2 answers
  • What are the advantages of repeating a header row? check all that apply
    14·1 answer
  • An investor is considering in which of two start-up companies to invest. The investor has faith in the industrial organization (
    9·2 answers
  • Consider the most important item among all the categories of information stored on your personal computer. As it applies to that
    8·1 answer
  • TWO MINUTES LEFT<br> ________ __________ is the physical arrangement of devices on a network.
    9·1 answer
  • Science Help
    11·1 answer
  • When your grounded and the only thing you can get on is a computer........................oh welllllllllllllllllllllllllllllllll
    7·1 answer
  • Question 1 (3 points)
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!