Data & Methods Description int numChars Character from the file string. String k
ID: 3808344 • Letter: D
Question
Data & Methods
Description
int numChars
Character from the file string.
String key
Character frequency in file string
char[][] vigSquare;
Binary character code for compression
Vigenere (int numChs,
String akey)
Constructor: initializes
numChars to numChs, key to akey,
vigSquare by calling
createVigSquare()
void createVigSquare ()
Creates the vigSquare by simply using a nested loop for the row and column:
0 to numChars-1 with the one line given below:
String encrypt(String msg)
Return an encrypted msg
String decrypt(String msg)
Return a decrypted msg
int getColIndex(char ch)
Returns the column index of ch in row 0
int getRowIndex(char keyCh)
Returns the row index of keyCh in col 0
char getPlainTextChar
(int rowIndex, char ch)
Returns the plaintext character corresponding to ‘ch’.
First, find the columnIndex of ‘ch’ using rowIndex and then return the char from the square at row 0 and columnIndex.
/**
* Class used to encrypt and decrypt a text string using Vigenere square
*/
public class Vigenere
{
private int numChars;
private char[][] vigSquare;
private String key;
/**
* Constructor a Vigenere object and create the square
*/
public Vigenere (int numChs, String akey)
{
// add code
}
/**
* Create the Vigenere square matrix
*/
private void createVigSquare()
{
// add code
}
/**
* Using the key, encode the message and return the encoded message
*/
public String encrypt (String message)
{
// add code
}
/**
* Return the column index of the character, ch, in the message to be encoded
*/
private int getColIndex (char ch)
{
// add code
}
/**
* Return the row index of the key character in the Vigenere square
*/
private int getRowIndex (char keyCh)
{
// add code
}
/**
* Decrypt the plaintext string using the key and vigSquare
* The key characters are the 'row' index and the ciphertext
* characters are in the matrix at vigSquare[row][column]
* The plaintext characters are the column index
*/
public String decrypt (String text)
{
// add code
}
/**
* Returns the plaintext character corresponding to the ciphertext
* character 'ch' located in the vigSquare[rowIndex][colIndex]
*/
private char getPlainTextChar (int rowIndex, char ch)
{
// add code
}
}
Data & Methods
Description
int numChars
Character from the file string.
String key
Character frequency in file string
char[][] vigSquare;
Binary character code for compression
Vigenere (int numChs,
String akey)
Constructor: initializes
numChars to numChs, key to akey,
vigSquare by calling
createVigSquare()
void createVigSquare ()
Creates the vigSquare by simply using a nested loop for the row and column:
0 to numChars-1 with the one line given below:
vigSquare[row][col] =
(char) ((col + row) % numChars);
String encrypt(String msg)
Return an encrypted msg
String decrypt(String msg)
Return a decrypted msg
int getColIndex(char ch)
Returns the column index of ch in row 0
int getRowIndex(char keyCh)
Returns the row index of keyCh in col 0
char getPlainTextChar
(int rowIndex, char ch)
Returns the plaintext character corresponding to ‘ch’.
First, find the columnIndex of ‘ch’ using rowIndex and then return the char from the square at row 0 and columnIndex.
Explanation / Answer
Vigenere.java
public class Vigenere {
private int numChars;
private char[][] vigSquare;
private String key;
private static final int LETTERS_IN_ALPHABET = 26;
private static final int ASCII_RANGE = 256;
static String encrypt(String text, String key) {
StringBuilder t = new StringBuilder();
String k = key;
char[][] square = createVigSquare();
for (int i = 0, j = 0; i < text.length(); i++, j++) {
if (j >= key.length()) {
j = 0;
}
t.append(square[k.charAt(j)][text.charAt(i)]);
}
return t.toString();
}
static String decrypt(String text, String key) {
StringBuilder t = new StringBuilder();
String k = key;
char[][] square = createVigSquare();
for (int i = 0; i < k.length(); i++) {
//char index = 'a';
int rowIndex = k.charAt(i);
char[] row = square[rowIndex];
int colIndex = new String(row).indexOf(text.charAt(i));
t.append((char) colIndex);
}
return t.toString();
}
static char[][] createVigSquare() {
char[][] square = new char[ASCII_RANGE][ASCII_RANGE];
int start = 'a';
int end = start + (LETTERS_IN_ALPHABET - 1);
int index = start;
for (int i = start; i <= end; i++) {
for (int j = start; j <= end; j++) {
if (index > end) {
index = start;
}
square[i][j] = (char) index;
index++;
}
index = i + 1;
}
return square;
}
static int getColIndex(char[][] vigSquare, char ch) {
int index = 0;
// //TODO Return the column index of the
// character, ch, in the message to be encripted
for(int i=0; i<vigSquare.length; i++){
for(int j=0; j<vigSquare[i].length; j++){
if(ch == vigSquare[i][j]){
index = j;
}
}
}
return index;
}
static int getRowIndex(char[][] vigSquare, char keyCh) {
int index = 0;
// Return the row index of the next
// character, keyCh, in the key
for(int i=0; i<vigSquare.length; i++){
for(int j=0; j<vigSquare[i].length; j++){
if(keyCh == vigSquare[i][j]){
index = i;
}
}
}
return index;
}
static char getPlainTextChar(char[][] vigSquare, int rowIndex, char ch) {
// Return the plaintext character
// corresponding to the ciphertext
// character 'ch' from the square
for(int i=0; i<vigSquare.length; i++){
for(int j=0; j<vigSquare[i].length; j++){
if(ch == vigSquare[i][j]){
ch = vigSquare[i][j];
}
}
}
return ch;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.