473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Vigenere decryption StringIndexOutOfBoundsException

Hi all,

I am having a problem with a vigenere decryption method I am writing.
I have tried to solve this but really can't figure out why this is happening.
I get a java.lang.StringIndexOutOfBoundsException and while I know what this means, I don't know why it occurs in this instance.
The method takes a ciphertext and key string as an argument, and first loops through all letters in the key and converts them to the number they are in the alphabet (e.g. a=0,b=1,etc..). Then it loops though all the letters in the ciphertext and shifts them by the amount given in the key.

Expand|Select|Wrap|Line Numbers
  1. public String VigenereDecrypt(String Ciphertext, String Key){
  2.     int KeyLength = Key.length(); 
  3.     int[] ShiftAmounts = new int[KeyLength];
  4.     String Plaintext = "";
  5.     for(int a=0;a<KeyLength;a++){
  6.         ShiftAmounts[a] = Alphabet.indexOf(Key.substring(a,1));
  7.     }
  8. for(int i=0;i<Ciphertext.length();i++){
  9.     int ShiftAmount = i % KeyLength;
  10.     String Sub = Ciphertext.substring(i,1);
  11.     String ShiftedSub = Shift(Sub,ShiftAmount);
  12.     Plaintext += ShiftedSub;
  13. }
  14. return Plaintext;
  15. }
  16.  
The exact error I am getting is:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)

and it occurs at line six of the block of code shown here.
Sep 9 '07 #1
3 2233
JosAH
11,448 Expert 8TB
That substring method invocation is wrong; pay close attention to that second
parameter value and compare it with String's API documentation.

kind regards,

Jos
Sep 9 '07 #2
I'm sorry for posting something like this on this forum, I didn't realise that my error was this trivial. Thank you for helping me solve this and for not just telling me the answer.
Sep 9 '07 #3
JosAH
11,448 Expert 8TB
I'm sorry for posting something like this on this forum, I didn't realise that my error was this trivial. Thank you for helping me solve this and for not just telling me the answer.
You're welcome of course. Trivial mistakes are the nastiest mistakes to find
because, they're so, well, trivial. You don't suspect yourself to make those
trivial mistakes anymore. I suffer from the same syndrome now and then ;-)

kind regards,

Jos
Sep 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paul Bergin | last post by:
Hi, I am connecting to a mysql 4.1 db with mysql connectorJ 3.0.7 and when I run a meta.getColumns(null, null, tableName, "%") on one particular table the following exception is thrown. ...
7
by: Piotr Turkowski | last post by:
Hi! Here you can get some notes about Vigenere Cipher: http://raphael.math.uic.edu/~jeremy/crypt/vignere.html Here's whole code of my program, function stats() is in polish, so you can omit...
9
by: Piotr Turkowski | last post by:
Hi, The code is here: http://free.ud.pl/~piotr/data/vigenere.zip Its my program for decrypting and encrypting text. Shot polish course: szyfrowanie (szyfruj) - encrypting (text i want to code...
1
by: al.raiyes | last post by:
hi all i hope that anybody can help me to find a source program in C++ for Vigenere Table and i will be thankfull for you yours Al.Raiyes
1
by: BigLuzer | last post by:
hi i am using the following setup: - .net 1.1 - 2 load-balanced iis servers - DPAPI machine store. - C# - i encrypted the connection string separately, one on each machine. the error i get...
6
by: nrperry | last post by:
Hello, I have a question about this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 I am doing my java development in IBM Rationale...
13
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.