In: Computer Science
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.
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.