218k views
0 votes
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.

User Nick Mowen
by
5.8k points

1 Answer

1 vote

Answer:

See explaination

Step-by-step 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;

}

}

User Dr Joe
by
5.7k points