Security is an important feature of information systems. Often, text is encrypte
ID: 655701 • Letter: S
Question
Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located.
For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly encrypted into another, which could be the same character. (If you like, you can design your program so that no character is encrypted into itself). To represent this concept, we can have an array of characters for the original alphabet, and another array of characters for the encrypted alphabet.
To encrypt a word, each letter in the word is replaced by the corresponding letter in the encrypted alphabet. To decrypt a word, the letters in the encrypted are replaced by the corresponding letter in the original.
If we have 26 different characters in the original alphabet, then we have 26 in the decrypted. Furthermore, the encrypted alphabet should be randomly generated (REALLY having the hard time here).
In your main method, you should prompt the user for a sentence. Your program should encrypt the sentence, output the encrypted sentence, then decrypt it, and out the decrypted.
This is what I have:
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class securityClass
{
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static final char[] Ualpha =
{'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
};
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];
Random regenerator = new Random();
public securityClass(){
int randNum = regenerator.nextInt(ALPHASIZE);
for(int i=0; i {
//makes sure that it won't assign a letter to itself or to one that has already been assigned
do {
randNum = regenerator.nextInt(26);
}
while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));
encryptU[i] = Ualpha[randNum];
encryptL[i] = Lalpha[randNum];
decrypt[i] = randNum;
}
}
public String encrypt(String secret)
{
System.out.println(Arrays.toString(encryptU));
int position = 0;
char[] mess = secret.toCharArray();
for(int i = 0 ; i {
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Ualpha[j]) {
position = j;
}
mess[i] = encryptU[position];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Lalpha[j]) {
position = j;
}
mess[i] = encryptL[position];
}
}
}
return new String(mess);
}
public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
for(int i = 0 ; i {
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j if(mess[i]==encryptU[j]){
mess[i] = Ualpha[j];
}
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j if(mess[i]==encryptL[j]){
mess[i] = Lalpha[j];
}
}
}
}
return new String(mess);
}
}
I WOULD LOVE ANY HELP YOU COULD GIVE ME, I ESPECIALLY NEED A MAIN METHOD FOR THIS PROGRAM.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package encryptdecrypt;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class EncryptDecrypt {
public static void main(String[] args) {
String secret;
securityClass sc=new securityClass();
System.out.println("Enter the text to be encrypted:");
Scanner scan=new Scanner(System.in);
secret=scan.nextLine();
System.out.println("Before Encryption:" +secret);
String code1=sc.encrypt(secret);
System.out.println("After Encryption:" +code1);
String code2=sc.decrypt(code1);
System.out.println("After Decryption:" +code2);
}
}
class securityClass
{
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static final char[] Ualpha =
{'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
};
Random regenerator = new Random();
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];
public securityClass(){
int randNum;
int hh=0;
for(int i=0;i<26;i++){
do
{
randNum = regenerator.nextInt(ALPHASIZE);
for(int k=0;k<26;k++){
if((encryptU[i]==Ualpha[randNum])||(encryptL[i] == Lalpha[randNum])){ hh=1;break;}
}
}while(randNum%26==0 && hh==1);
encryptU[i] = Ualpha[randNum];
encryptL[i] = Lalpha[randNum];
decrypt[i] = randNum;
}
}
public String encrypt(String secret)
{
System.out.println(Arrays.toString(encryptU));
char[] mess = secret.toCharArray();
int position = 0;
for(int i=0; i<secret.length();i++){
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Ualpha[j])
mess[i] = encryptU[position];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j < encryptL.length; j++) {
if(mess[i]==Lalpha[j])
mess[i] = encryptL[position];
}
}
}
return new String(mess);
}
public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
int position = 0;
for(int i=0; i<secret.length();i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j<encryptU.length;j++){
if(mess[i]==encryptU[j])
mess[i] = Ualpha[j];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j<encryptL.length;j++){
if(mess[i]==encryptL[j])
mess[i] = Lalpha[j];
}
}
}
return new String(mess);
}
}