Question
JAVA HELP Can anyone tell me why the strings are not printing anything out when these functions are called. Code is part of a Vingenere
JAVA HELP
Can anyone tell me why the strings are not printing anything out when these functions are called. Code is part of a Vingenere Ci[her program:
static String encrypt(String text, String key) {
// Using the key, encode the message
// and return the encoded message
StringBuilder t = new StringBuilder();
String k = key;
char vigSquare[][] = createVigSquare();// create vigSquare
for (int i = 0, j = 0; i < text.length(); i++) {
if (Character.isLetter(text.charAt(i)))// if char at i is letter
{
if (j >= key.length()) {
j = 0;// if j reached to length of the key then set j=0
}
// then append char at i in text
t.append(vigSquare[k.charAt(j++)][text.charAt(i)]);
} else// if char at i is not a letter
t.append(text.charAt(i));// then simple append char
}
return t.toString();
}
static String decrypt(String encryptedText, String key) {
// Using the key,
// decode the encoded message and
// return the original message in lowercase
StringBuilder t = new StringBuilder();
String k = key;
char[][] vigSquare = createVigSquare();// create vigSquare
for (int i = 0, j = 0; i < encryptedText.length(); i++) {
if (Character.isLetter(encryptedText.charAt(i)))// if char at i is letter
{
if (j >= key.length()) {
j = 0;// if j reached to length of the key then set j=0
}
int rowIndex = k.charAt(j++);
char[] row = vigSquare[rowIndex];// get row from vigSquare
int colIndex = new String(row).indexOf(encryptedText.charAt(i));
t.append((char) colIndex);// then append asscii value of index
} else// if char at i is not a letter
t.append(encryptedText.charAt(i));// then simple apppend char
}
return t.toString();// return decrypted message
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started