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...
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
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).
Please perform encryption and decryption given the following values of an RSA public key cryptosystem; p=17,...
Please perform encryption and decryption given the following values of an RSA public key cryptosystem; p=17, q=31, e=7 and M=2
Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the...
Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the following information: parameters p=11,q=5, e=7 Present all the information that you will need to encrypt and decrypt only the first letter from text
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT