jeudi 11 mars 2021

please fix my code this is about encryption

I am tasked with writing a program the will generate a random alphabet. The user is then prompted to enter a plaintext message. The program must then perform a substitution cipher to generate an encrypted message using your random cipher alphabet. When I enter a message, the encrypted message that is returned is "null". I'm not sure what is wrong with my code.

import java.util.Scanner;

public class noob2
{
    public  noob2(String message)
    {
    }

    private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private String myNewAlphabet;
    private String myEncryptedMessage;

    public String generateRandomAlphabet()
    {
        int random;
        String myNewAlphabet = "";

        char[] alphabet = { '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' };

        for (int i=0; i <26; i++) {
            // keep generating random index until an unused alphabet is found
            do {
                random = (int)(Math.random() * 26);
            } while (myNewAlphabet.indexOf(alphabet[random]) >= 0);

            myNewAlphabet += alphabet[random];
        }

        //temporary code just to display original an new random alphabet
        System.out.print("   Alphabet: ");
        for (int x =0; x <26; x++)
        {
            System.out.print(alphabet[x]);
        }

        System.out.println();
        System.out.println("Cipher Alphabet: " + myNewAlphabet);
        // this section will be deleted
        return myNewAlphabet;
    }

    public String substitutionCipher(String message)
    {
        String encryptedMessage = "";

        for(int i = 0; i < message.length(); i++)
        {
            int positionOfCharacterInMessage = ALPHABET.indexOf(message.charAt(i));

// this accounts for spaces or other symbols that may be in the message to be encrypted

            if (positionOfCharacterInMessage == -1)
            {
                encryptedMessage += message.charAt(i);
                continue;
            }

            encryptedMessage += myNewAlphabet.charAt(positionOfCharacterInMessage);

        }

        return myEncryptedMessage;
    }

    //getter method to return Encrypted message
    public String getEncryptedMessage()
    {
        return myEncryptedMessage;
    }
}

//*************************************************************
import java.util.Scanner;

public class RandomCipherTester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a plaintext message for processing");
        Scanner sc = new Scanner(System.in);
        String plaintext = sc.nextLine();
        noob2 x = new noob2(plaintext);
        x.generateRandomAlphabet();
        System.out.println("The encrypted message is " + x.getEncryptedMessage());
    }
}



Aucun commentaire:

Enregistrer un commentaire