Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Even though I used the replace all func. Its giving me special characters.
Below is my code.

What I have tried:

Java
package oops;

import java.util.Arrays;
import java.util.Map;

import java.util.Scanner;  

public class Main {

	private static final String Bforce = null;
	private static final String BMessage = null;  

	public static void main(String[] args) {

		System.out.println("----Vigenere Cipher Encryptor----\n");
		Scanner in = new Scanner(System.in); 



		//	Allow the user to choose if they want to encrypt or decrypt or brute force a message
		System.out.println(" 1 to encrypt a message  ");
		System.out.println(" 2 to decrypt a message  ");
		System.out.println(" 3 to brute force  ");
		int input = in.nextInt();

		// ENCRYPT MESSAGE 
		if (input == 1) {
			System.out.print("Enter the password: ");
			String key = in.next();
			// key number
			System.out.print("Enter the key number (1-26) : ");
			int number = in.nextInt();   

			if (number >= 1 && number <= 26) { 

			}

			else { 


			}


			// message
			System.out.print("Enter the message : ");

			String EMessage = in.next();
			String encryptMessage = encrypt(EMessage, key, number);
			 System.out.println("The encrypted message is: " + encryptMessage);
			for (int x = 0; x <= number; x++) {
			replaceAll("[-+.^:,]","");  
			// encrypt the message
   //		String encryptMessage = encrypt(EMessage, key);
		//	System.out.println("The encrypted message is: " + encryptMessage);
			

		} 
		}

		// DECRYPT MESSAGE option 2
		else if (input == 2) {
			System.out.print("Enter the password: ");
			String key = in.next(); 
			// key number
			System.out.print("Enter the key number (1-26) : ");

			// NUM 1- 26  OR ELSE IT GIVES AN ERROR
			int number = in.nextInt();   

			if (number >= 1 && number <= 26) { 

			}

			else { 


			}



			// prompt for message
			System.out.print("Enter the message: ");
			String dMessage = in.next(); 
			String decryptMessage = decrypt(dMessage, key, number); 
		//	System.out.println("The decrypted message is: " + decryptMessage);
			for (int x = 0; x <= number; x++) {
			//replaceAll("[-+.^:,?_-`*/()]","");   
				// decrypt the message
			////	String DecryptMessage = decrypt(DMessage, key);
		System.out.println("The decrypted message is: " + decryptMessage);

				// brute force option 3
			}
		}
		
		
		else if  (input == 3) {
			System.out.print("Enter the password: ");
			String password = in.next(); 
			// key number
			System.out.print("Enter the key number (1-26) : ");

			// 1-26 OR ELSE ERROR
			int number = in.nextInt();   

			if (number >= 1 && number <= 26) { 

			}

			else { 


			}

			// message  for user
			System.out.print("Enter the message: ");
			String BMessage = in.next(); 

			for (int x = 0; x <= number; x++) {
				replaceAll("[-+.^:,]","");  
				// decrypt w brute force
				String BruteMessage = Bforce(BMessage, password);
				System.out.println("The decrypted message is: " + BruteMessage);
			}
		}
		else {
			System.out.println("Wrong Input!");
		}
		in.close();
	}




















	private static void replaceAll(String string, String string2) {
		

	}





	//	Encryption 1
	//	Encryption Logic: 

	public static String encrypt(String Message, String Key, int number) {
		
		String eMessage = "";
		Message = Message.toUpperCase();
		Key = Key.toUpperCase(); 
		
		for (int i = 0, j = 0; i < Message.length(); i++) {
			// Get the character from the message
			char letter = Message.charAt(i);
			// Get the ASCII value of the character
			int ascii = (int) letter;
			// Get the value of the character in the password
			int passVal = (int) (Key.charAt(j)) - 65;
			// Calculate the amount of the shift
			int shiftPos = ascii + passVal - number;
			
			// Check if the position is > 90 (that is, past the letter 'Z')
			if (shiftPos > 90) {
				// Go back  to the letter 'A'
				eMessage += (char) (shiftPos % 90 + 64);
			}
			else {
				// Take the character at the shift position
				eMessage += (char) (ascii + passVal - number);
			}
		//	System.out.println(EMessage);
			j = ++j % Key.length();
		}
		return eMessage;
	}

	//	Decryption 2
	//	Decryption Logic: 
	public static String decrypt(String Message, String Key, int number) {
		String dMessage = ""; 
		Message = Message.toLowerCase();
		Key = Key.toLowerCase();     
		
			for (int i = 0, j = 0; i < Message.length(); i++) {
				char letter = Message.charAt(i); 
				int num = (int) letter; 
				int pass = (int) (Key.charAt(j)) - 'a';

				int shiftPos = num + pass - number; 
				if (shiftPos > 90) {
					// Go back  to the letter 'A'
					dMessage += (char) (shiftPos % 90 + 64);
				}
				else {			

				
					// Take the character at the shift position
					dMessage += (char) (num + pass - number);
				}
			//	System.out.println(EMessage);
				j = ++j % Key.length();
			}


	

		  	
		
		
					return dMessage;
			
			
			}
	

	// BRUTE FORCE  3     


	public static <Bmessage> String Bforce(String Message, String key) {
		String BMessage = "";
		Message = Message.toLowerCase(); 
		for (int i = 0, j = 0; i < Message.length(); i++) {
			char letter = Message.charAt(i);
			BMessage += (char)((letter - key.charAt(j) + 26) % 26 + 65);
			j = ++j % key.length();
		}


		return BMessage;


	}
}
Posted
Updated 27-Apr-20 14:21pm
v2
Comments
Patrice T 27-Apr-20 17:39pm    
Show example input and output.
Give context.

1 solution

Quote:
In java how do I replace all special characters?

I fear you have to fill this part of code:
Java
private static void replaceAll(String string, String string2) {


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900