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
an organization that maintains a gateway to the Internet and rents access to customers on a per-use of subscription basis\ and W
shtirl [24]
1. An organisation that maintains a gateway to the internet and rent access to customers on a per use of subscription basis is called INTERNET SERVICE PROVIDER [ISP]. Internet service providers are of various forms, it can be commercially or privately owned, it can also be owned by a community. The internet access provided by ISP can be inform of cable, DSL or dial up. ISP provide other services such as website building and virtual hosting.
2. Internet protocol [IP] refers to a set of rules that guide the format of data sent over the internet, it is the method by which data is sent over the internet from one computer to another computer. Each computer that is linked to the internet has a unique IP address by which it is identified and distinguished from all other computers on the internet.
8 0
2 years ago
Select the statements that identify benefits of programming design. Choose all that apply. It ensures that programmers working o
scoundrel [369]

Answer:

The answer is "Option i, iii, and iv"

Explanation:

The software design is a sequence of code, which is used to aggregate, calculating the efficiency of a common issue in software development within the same specific scenario.

  • It's also not a finished specification, that can be immediately translated into the origin of machine language code.
  • These testing aims to detect a list of steps on a machine, which is often used to solve a certain problem and optimize the performance of a task which is just as complicated as a corporate structure.
6 0
2 years ago
A _____ character is a space, a tab, or a new line.
babunello [35]
Whitespace

--------------------------------------------
5 0
3 years ago
Jackson is teaching a class the concept of the binary number system. Which term will he use to refer to the leftmost bit of a bi
Fofino [41]
Most significant digit. That's the same that it's called for any number system.
5 0
3 years ago
Read 2 more answers
(MCQ question). Which of the following files stores information about a local Google Drive installation such as User email ID, L
Harlamova29_29 [7]

Answer:

Sync_config.db

Explanation:

With the help of this file we can find the following information of our google drive.

  1. Google Drive version  
  2. local sync root path  
  3. User email address
8 0
3 years ago
Other questions:
  • Background Susan finished work on system architecture issues, and her system design specification was approved. Now she is ready
    15·1 answer
  • 3. This shows you the different things that all the tools can do, as you click on the tools
    10·1 answer
  • Which of the following can indicate what careers will be enjoyable ?
    5·1 answer
  • Which toolbar is located next to the office button and contain the commonly used commands​
    15·1 answer
  • Pseudocode is an improvement over the IPO chart because it lays out the a0 sequence of steps for a particular program.
    6·1 answer
  • Virtualization:
    8·1 answer
  • Please help me answer this<br><br>number 1..
    9·1 answer
  • (Exhibit: Production Possibilities Curves 2) Assume that a nation is operating on production possibilities curve CD. Economic gr
    11·1 answer
  • . How to insert Section Break in Microsoft word 2016 ?
    14·1 answer
  • Describe accessibility in Windows 7 ​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!