5 Replies - 3338 Views - Last Post: 13 April 2014 - 04:56 PM Rate Topic: -----

#1 Haltie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-April 14

Java Vigenere Cipher

Posted 13 April 2014 - 02:54 AM

Hey guys, I previously got help with my Vigenere Cipher a few days ago, and have received a few instructions and suggestions. I currently have a rather decent program, that successfully runs without error and (sort-of) produces the output I want. My code is:

/**
 * @(#)VigenereCipher.java
 *
 *
 * @author
 * @version 1.00 2014/4/11
 */

import java.util.Scanner;
public class VigenereCipher {

	public static void main(String[]args)
	{
		Scanner sc;
		String word, key;
		sc = new Scanner(System.in);
		int i, k, l, j;
		
		

		System.out.println("Vignère Cipher Encryptor/Decryptor");
		System.out.println("(1) Encrypt");
		System.out.println("(2) Decrypt");
		System.out.print("Choice:");
		i=sc.nextInt();
		sc.nextLine();

   	 		if(i==1)
    		{
    		System.out.print("Enter the text you wish to encrypt: ");
    		word = sc.nextLine();
    		word = word.toUpperCase();
			word = word.replaceAll("\\W", "");
			System.out.println(word);
		
			System.out.print("Please enter your key: ");
			key = sc.nextLine();
		
		
		 	for (j = 0; j < word.length(); j++)
            	{
                	char encrypted = word.charAt(j);
               		for (k = 0; k < key.length(); k++)
                	{
                    	Character encryptedkey = key.charAt(k);
                    	l = -10 + encryptedkey.getNumericValue(encryptedkey);
                    	System.out.print((char) (encrypted + l));
                	}
                
            	}
		
    		}
    		else
    		{
    		System.out.println("Enter the text you wish to decrypt");
    		word = sc.nextLine();
    		word = word.toUpperCase();
			word = word.replaceAll("\\W", "");
			System.out.println(word);
		
			System.out.print("Please enter your key: ");
			key = sc.nextLine();
		
		
		 		for (j = 0; j < word.length(); j++)
            	{
                	char encrypted = word.charAt(j);
               		for (k = 0; k < key.length(); k++)
                	{
                    	Character encryptedkey = key.charAt(k);
                    	l = -10 + encryptedkey.getNumericValue(encryptedkey);
                    	System.out.print((char) (encrypted + l));
                	}
                
            	}

    		}
            			

	}
}	


Now my issue is that I need to know how to:
1.) Limit user prompts to only 1 & 2 for the choice.
2.) Remove symbols and punctuation from my encrypted and decrpyted output so I only have text/chars.

If anyone can help with some insight, that'd be fantastic, thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Vigenere Cipher

#2 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Java Vigenere Cipher

Posted 13 April 2014 - 04:22 AM

To limit your input, use a simple while loop:
i = 0;
while(i < 1 || i > 2){
   // prompt for input
   i = //read in input
}
It is important to initialise i to 0 before the loop, so that you can enter it.

This post has been edited by mike73: 13 April 2014 - 04:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 Haltie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-April 14

Re: Java Vigenere Cipher

Posted 13 April 2014 - 04:14 PM

View Postmike73, on 13 April 2014 - 04:22 AM, said:

To limit your input, use a simple while loop:
i = 0;
while(i < 1 || i > 2){
   // prompt for input
   i = //read in input
}
It is important to initialise i to 0 before the loop, so that you can enter it.


Thanks for that Mike, I knew it was something simple that I couldn't quite think of.
Would you happen to know how to fix my other issue, by any chance?
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Java Vigenere Cipher

Posted 13 April 2014 - 04:24 PM

I would suggest you make methods. I notice you repeat the same algorithms. -- bad programming.

String Encrypt( String word, String key ){}
char Cipher( char word, char key ){}
String Decrypt( String word, String key){}


Was This Post Helpful? 0
  • +
  • -

#5 Haltie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-April 14

Re: Java Vigenere Cipher

Posted 13 April 2014 - 04:43 PM

View Postinfernorthor, on 13 April 2014 - 04:24 PM, said:

I would suggest you make methods. I notice you repeat the same algorithms. -- bad programming.

String Encrypt( String word, String key ){}
char Cipher( char word, char key ){}
String Decrypt( String word, String key){}



I understand that I should make methods, but unfortunately this is an assignment where I have been instructed not to make any. Methods would certainly simplify the matter, though. As for repeating the same algorithms, I agree, but I'm running on half a year of Computer Science classes and the internet. Everything I did was a little 'dirty' and brute-force. Sorry!
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Java Vigenere Cipher

Posted 13 April 2014 - 04:56 PM

In that case I still would think you should use methods, the reason is you can test just the method to get it working. You can easily copy the code in the method into the final when you are done.

You've made a lot of mistakes, not programming errors, but in the algorithm. I want you to figure it out mostly on your own, though.

As in the last post and still now, I don't think either understand the cipher, or how char represent numbers. use Integer.valueof('A') to investigate the values. If 'A' is 65

then int x = 5 + 'A'; // x will be 70
work on this first:
static char Ciper(char word, char key){
}



that function should give you all the possible answers on this chart: http://en.wikipedia....are_shading.svg
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1