4 Replies - 1860 Views - Last Post: 13 April 2014 - 12:31 AM Rate Topic: -----

#1 Thornton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 14

Vigenere Cipher

Posted 12 April 2014 - 10:43 PM

Hello all,

I'm trying to complete my first assignment for a programming unit.
We have been tasked to create a Vigenere Cipher. At this stage however, I am attempting
to get the program to encrypt a phrase using a single letter key (Caesar cipher), before
progressing to the final stage, implementing a functioning Vigenere Cipher. The code below is able to
encrypt a word, capatalise it, remove white space and shift it a certain amount of places,
given the provided letter. My problem is that because the single letter is a String, not a char,
the phrase is being shifted incorrectly. For example, when the program is given the Inputs:
“This is a test” with the single letter key A, the program shifts all the characters by 1.
The output however should read THISISATEST as A is supposed to be 0.
I hope I have explained this clearly and would greatly appreciate any advice.

import java.util.Scanner;
public class VigenereCipher {

    public static void main(String[] args)
    {
        
	char c;

        String encrypted = "";
     	
        
     	Scanner  sc = new Scanner(System.in);
        
    	System.out.print(" Please enter Your word      = ");
      	String str=sc.nextLine();
        

		System.out.print("Keyphrase			= ");
  		String key=sc.nextLine();

  		
    	
    	for (int i = 0;  i < str.length();  i++ )
        {
    	
		if(Character.isLetter(c = str.charAt(i)))
		{
		c += key.length() % 26;
		
		encrypted += c; 
		}
			
		}	
		System.out.println(encrypted.toUpperCase());

	}


}  




Is This A Good Question/Topic? 0
  • +

Replies To: Vigenere Cipher

#2 infernorthor  Icon User is offline

  • D.I.C Lover

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

Re: Vigenere Cipher

Posted 12 April 2014 - 10:52 PM

 c += key.length() % 26;


by doing this you giving the char 0 to 25 which weird symbols and '\0' so it won't show up.
Then you are adding it to encrypted which will also be screwed up.
Was This Post Helpful? 0
  • +
  • -

#3 Thornton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 14

Re: Vigenere Cipher

Posted 12 April 2014 - 11:15 PM

View Postinfernorthor, on 12 April 2014 - 10:52 PM, said:

 c += key.length() % 26;


by doing this you giving the char 0 to 25 which weird symbols and '\0' so it won't show up.
Then you are adding it to encrypted which will also be screwed up.


I'm attempting to shift c with wrap around using modulo 26, as in the number of letters in the alphabet. The output isn't screwed up, it just shifts the phrase by 1 every time, as that is what the key length is.
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: Vigenere Cipher

Posted 12 April 2014 - 11:54 PM

Sorry you're it won't be complete screwed up. I misread it. But it certainly won't do what you intend.

key.length() will be the same
so it will just move the initial char right the length of the key.
If the word was 3 long. It would shift 3 to the right. But if you are near the end of the alphabet it will go out of bounds.
Try your code with z

there is a lot of stuff you need to fix in order to get this working
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10731
  • View blog
  • Posts: 18,362
  • Joined: 19-March 11

Re: Vigenere Cipher

Posted 13 April 2014 - 12:31 AM

Remember that the value of 'A' is not zero, it's some other number whose value you don't want to have to care about. Life will be a lot easier if you normalize your chars to the range 0..25 before you start working on them. Assuming you're doing all this in uppercase, you'd do this by subtracting 'A'. Since a char is an int, this will wind up mapping the character A to the value zero, and B to 1, and so forth. Now you can do your cipher arithmetic, making sure your values remain in the range 0..25, and when you're done, denormalize back to the uppercase alphabet by adding back the 'A'.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1