4 Replies - 1976 Views - Last Post: 14 March 2010 - 09:20 AM Rate Topic: -----

#1 nikoll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-November 09

Vigenere cipher - help please!

Posted 14 March 2010 - 05:32 AM

Hi guys!

I need to implement Vigenere cipher, but I am facing one problem, which I don't know how to solve. I have this code:

// Vigenere encryptionMessage
		  private void encryptMessageVigenere(String msg, int k) {
		    	String result = "";
		    	chipherTextArea.setText("");
		    	for (int i = 0; i < msg.length(); i++)
		    		result += encryptChar(msg.charAt(i), k);
		    	chipherTextArea.setText(result);
		    }
		  
		  // Vigenere encryptionChar
		    private char encryptCharVigenere(char c, int k) {
		    	if (Character.isLetter(c))
		    		return (char) ('A' + (c - 'A' + k) % 26);
		    	else
		    		return c;
		    }

// here I have an button for encryption and there goes:
String plain = plainTextArea.getText();			       	
String keyword = keyTextArea.getText();
int i = 0, j = 0;
int key = 0;
			        	
for (i = 0; i <= plain.length(); i++){
			        			        	     int current = plain.charAt(i);
   int currentKey = keyword.charAt(i); // I constantly get error String  index out of range
			        			
	key = current + currentKey; 
	currentKey++;
			        	
}
   String message = "";
   message = plain.toUpperCase();
   encryptMessageVigenere(message, key);



I think this message occurs, because of for loop, and because it cannot do addition right. Please, tell how to do it right ? Any help is welcome and thanks in advance!

Edited by Dogstopper: With 8 posts, you should know how to post between code tags: :code:

This post has been edited by Dogstopper: 14 March 2010 - 09:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Vigenere cipher - help please!

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Vigenere cipher - help please!

Posted 14 March 2010 - 08:19 AM

Change for (i = 0; i <= plain.length(); i++){ to for (i = 0; i < plain.length(); i++){. Let's say for example plain had a length of two. Those two characters are accessed at index 0 and 1. But plain.length() returns 2. So you must use less than in the for loop declaration, otherwise you will be trying to access plain.charAt(2), which would actually be the third character in the string (0,1,2). Strange, I know, but there you go.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,536
  • Joined: 27-December 08

Re: Vigenere cipher - help please!

Posted 14 March 2010 - 08:41 AM

Please, :code:
Also, can you specifically describe the errors you are encountering? Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#4 nikoll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-November 09

Re: Vigenere cipher - help please!

Posted 14 March 2010 - 09:10 AM

View Postxor-logic, on 14 March 2010 - 07:19 AM, said:

Change for (i = 0; i <= plain.length(); i++){ to for (i = 0; i < plain.length(); i++){. Let's say for example plain had a length of two. Those two characters are accessed at index 0 and 1. But plain.length() returns 2. So you must use less than in the for loop declaration, otherwise you will be trying to access plain.charAt(2), which would actually be the third character in the string (0,1,2). Strange, I know, but there you go.


Thank You! No w that error is handled. But still I have one more question, this code work some encryption, but not the real one. I actually wanted to encrypt with Vigenere cipher. It work like: In English alphabet there is 26 letter, each one has a number from 0 to 26. Then it should sum number of first letter in plain text (message which we want to encrypt) and first number of key (String) and that goes for the length of message, if the key is shorter it starts over, when all length had already sumerized. But I am not sure where from it gets for example 203 number for k. It would be from great help if someone can give me assistance, what to change and where is the problem. Thank You!
Was This Post Helpful? 0
  • +
  • -

#5 nikoll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-November 09

Re: Vigenere cipher - help please!

Posted 14 March 2010 - 09:20 AM

View Postmacosxnerd101, on 14 March 2010 - 07:41 AM, said:

Please, :code:
Also, can you specifically describe the errors you are encountering? Thanks for helping us help you! :)


Upps, sorry! Will do it in next posts.

Will take this in to account! ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1