Click here to Skip to main content
15,868,121 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have finished doing encryption, and it is working but I dont know how to do decryption. I have tried doing ut but the output is not right.
Can someone correct my code.


Below is the code of what I have done

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 {			

				
					dMessage += (char) (num - pass + number);
				}
				dMessage += (char)((num - pass - number) % 26 + 65);
				j = ++j % Key.length();
				dMessage =  dMessage.replaceAll("[^a-zA-Z0-9]", "");
			}  
	

		  	
		
		
					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.replaceAll("[", "");



	}
}
Posted
Updated 27-Apr-20 15:06pm
v4

1 solution

As far as I can see, removing non letter or digits is a bad idea because you can't restore original message. What is removed include spaces.
Problem in your code, it keeps digits, but your algorithm is make to handle only letters, so any message with digits will not be decrypted.
An approach while encrypting/decrypting is to check on fly if next char is within range of allowed chars, and skip encryption for them.
Quote:
I have tried doing ut but the output is not right.

To decrypt, you have to do exactly what was done to encrypt in reverse order.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
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