Question

In: Computer Science

Write a program in java that can perform encryption/decryption. In the following let the alphabet A...

Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.

Solutions

Expert Solution

package Security;

import java.util.Scanner;

// Defines a class for encryption and decryption

public class EncDec

{

// Creates a character array

char letters[] = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd',

'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i',

'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n',

'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's',

'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x',

'Y', 'y', 'Z', 'z',' ', '.', '\''};

// Method to encrypt data and returns it

String encryption(String ori)

{

// To store encrypted data

String encrypted = "";

// Loops till end of the string

for(int c = 0; c < ori.length(); c++)

{

// Flag value 0 for not available character

// for encoding

int f = 0;

// Extracts a character

char ch = ori.charAt(c);

// Loops till end of the letters array

for(int d = 0; d < letters.length; d++)

{

// Checks if current extracted character

// is same as letters array d index position

// character

if(ch == letters[d])

{

// Adds the index position to encrypt

encrypted += d;

// Adds -1 as split character

encrypted += -1;

// Sets the flag to 1 for available

f = 1;

// Come out of the loop

break;

}// End of if condition

}// End of for loop

// Checks if flag is 0 then add the original

// character

if(f == 0)

encrypted += ch;

}// End of outer for loop

// Returns the encrypted data

return encrypted;

}// End of method

// Method to decrypt data and returns it

String decryption(String enc)

{

String decrypted = "";

// Split data with -1

String splitString[] = enc.split("\\-1");

// To store the array position

int pos = 0;

// Loops till end of the split array

for(int c = 0; c < splitString.length; c++)

{

// Extract a character

char ch = splitString[c].charAt(0);

// Check if the character is a digit

if(Character.isDigit(ch))

{

// Convert the split data to integer

pos = Integer.parseInt(splitString[c]);

// Add the letter at pos index position

decrypted += letters[pos];

}// End of if condition

// Otherwise add the split data

else

decrypted += splitString[c];

}// End of for loop

// Returns the decrypt data

return decrypted;

}// End of method

// main method definition

public static void main(String ss[])

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Class EncDec object created

EncDec ed = new EncDec();

// Accepts the string to encrypt

System.out.print("\n Enter a string to encrypt: ");

String original = sc.nextLine();

// Calls the method to encrypt and stores the result

String encryptedString = ed.encryption(original);

// Displays the original data

System.out.print("\n Original string: " + original);

// Displays the encrypted data

System.out.print("\n Encrypted string: " + encryptedString);

// Accepts the string to decrypt

System.out.print("\n Enter a string to decrypt: ");

encryptedString = sc.nextLine();

// Calls the method to decrypt and stores the result

String decryptedString = ed.decryption(encryptedString);

// Displays the decrypted data

System.out.print("\n Decrypted string: " + decryptedString);

}// End of main method

}// End of class

Sample Output:


Enter a string to encrypt: This is demo.

Original string: This is demo.
Encrypted string: 38-115-117-137-152-117-137-152-17-19-125-129-153-1
Enter a string to decrypt: 38-115-117-137-152-117-137-152-17-19-125-129-153-1

Decrypted string: This is demo.


Related Solutions

Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch >...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } mean??? I understand that it has something to do with ASCII characters and makes sure that if the encryption/decryption character is more than "z", then it would loop back to "a" instead of outputting a charcter like "{" . I...
The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm....
The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm. Implement these two algorithms in their own function. Now write a testing function that demonstrates your algorithms work for all interesting cases!
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT