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
What are the basic data types supported in C programing language?
Murljashka [212]

Answer:

The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long.

6 0
2 years ago
What is the importance of using the proper markup language?
Klio2033 [76]

the answer is D. without the right tags the content wont be accurately indexed

3 0
3 years ago
Read 2 more answers
In this lab, you use the flowchart and pseudocode found in the figures below to add code to a partially created C++ program. Whe
never [62]

Answer:

The equivalent program in C++:

#include<iostream>

#include <sstream>

using namespace std;

int main(){

   string Score, Rank;

   cout<<"Enter student score and class rank: ";

   cin>>Score>>Rank;

   int testScore = 0, classRank = 0;

   stringstream sstream(Score);

   sstream>>testScore;

   

   stringstream tream(Rank);

   tream>>classRank;

   

   if (testScore >= 90){

       if(classRank >=25){cout<<"Accept";}

       else{cout<<"Reject";}

   }

   else if(testScore >= 80){

       if(classRank >=50){cout<<"Accept";}

       else{cout<<"Reject";}

   }

   else if(testScore >= 70){

       if(classRank >=75){cout<<"Accept";}

       else{cout<<"Reject";}

   }

   else{cout<<"Reject";}

   return 0;

}

Explanation:

This declares Score and Rank as string variables

   string Score, Rank;

This prompts the user for score and class rank

   cout<<"Enter student score and class rank: ";

This gets the user input

   cin>>Score>>Rank;

This declarees testScore and classRank as integer; and also initializes them to 0

   int testScore = 0, classRank = 0;

The following converts string Score to integer testScore

<em>    stringstream sstream(Score);</em>

<em>    sstream>>testScore;</em>

The following converts string Rank to integer classRank

   stringstream tream(Rank);

   tream>>classRank;

The following conditions implement the conditions as given in the question.    

If testScore >= 90

<em>    if (testScore >= 90){</em>

If classRank >=25

<em>        if(classRank >=25){cout<<"Accept";}</em>

If otherwise

<em>        else{cout<<"Reject";}</em>

<em>    } ---</em>

If testScore >= 80

<em>    else if(testScore >= 80){</em>

If classRank >=50

<em>        if(classRank >=50){cout<<"Accept";}</em>

If otherwise

<em>        else{cout<<"Reject";}</em>

<em>    }</em>

If testScore >= 70

<em>    else if(testScore >= 70){</em>

If classRank >=75

<em>        if(classRank >=75){cout<<"Accept";}</em>

If otherwise

<em>        else{cout<<"Reject";}</em>

<em>    }</em>

For testScore less than 70

<em>    else{cout<<"Reject";}</em>

<em />

3 0
2 years ago
4 brainly
Allushta [10]
Web designers like create websites for businesses
6 0
2 years ago
Use conversion tool to convert the binary numbers to decimal.
Neko [114]
It’s real my easy you can use a calculator or an online converter. Or division

(111001)₂ = (1 × 2⁵) + (1 × 2⁴) + (1 × 2³) + (0 × 2²) + (0 × 2¹) + (1 × 2⁰) = (57)₁₀

(1100000)₂ = (1 × 2⁶) + (1 × 2⁵) + (0 × 2⁴) + (0 × 2³) + (0 × 2²) + (0 × 2¹) + (0 × 2⁰) = (96)₁₀

(1010101)₂ = (1 × 2⁶) + (0 × 2⁵) + (1 × 2⁴) + (0 × 2³) + (1 × 2²) + (0 × 2¹) + (1 × 2⁰) = (85)₁₀

(1001000)₂ = (1 × 2⁶) + (0 × 2⁵) + (0 × 2⁴) + (1 × 2³) + (0 × 2²) + (0 × 2¹) + (0 × 2⁰) = (72)₁₀
7 0
2 years ago
Read 2 more answers
Other questions:
  • A restaurant is a workplace for someone whose career specialty is in
    14·2 answers
  • When a cache block has been modified since being read from main memory?
    10·2 answers
  • What is the order in which windows systems receiving and process multiple gpos?
    7·1 answer
  • Can someone help me with this one
    9·2 answers
  • Vendors who use open source as part of their product offerings can expect to bring new products to the market faster because: Gr
    11·1 answer
  • What is the main difference between a search engine and a web browser?
    6·2 answers
  • To use   ( 2 complement ) answer it (101101)2 – (1100)2 =   (                    )2
    12·1 answer
  • Saujani describes that women are highly underrepresented in STEM careersShe attributes this to women needing more confidence. Wh
    13·1 answer
  • Add me on Fortn!te <br> TsarBacon (imma girl)
    11·1 answer
  • Phân tích cạnh tranh của cocacola và pepsi
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!