So I\'m doing this encryption project using columnar transposition cipher in jav
ID: 3571763 • Letter: S
Question
So I'm doing this encryption project using columnar transposition cipher in java but I need help addind padding to the end of my message. What I'm trying to do is make a while loop add an "X" to the end of my encrypted string if the length of the message is not a multiple of the number of rows. In this case I'm using the String "is it time to go yet?" as my message. This is what I've done so far, it just prints the encrypted cipher but without padding. I dont really know where to insert the while loop in the method or what the simplest way to add padding would be. The output on the bottom is what Im trying to get my program to print. Please add comments just bc I'm not familiar with everything on java. Thanks if anyone could help out it would be much appriciated. Sorry if I made this confusing.
***
//My progress so far
public class Cipher {
public static void main(String[] args){
paddedencryption("is it time to go yet");
}
public static void paddedencryption(String message){
System.out.println("Message encrypted with columnar transposition cipher and padding. ");
for(int rows = 2; rows <= 10; rows++){
for(int i = 0; i+1 <= rows; i++){
for (int letter = i; letter < message.length(); letter = letter + rows){
System.out.print(message.charAt(letter));
}
}
System.out.println(" ");
}
}
}
***
***
//Output I want.
***
Explanation / Answer
//Cipher class defined
public class Cipher
{
public static void main(String[] args)
{
//Calls paddedencryption by passing message
paddedencryption("is it time to go yet?");
}
//paddedencryption method definition
public static void paddedencryption(String message)
{
System.out.println("Message encrypted with columnar transposition cipher and padding. ");
//Loops row to row 10
for(int rows = 2; rows <= 10; rows++)
{
//Finds out the remainder by dividing message length with row number
int s = message.length() % rows;
//If it is divisible then we have equal character as per the row
//If it is not divisible to padd extrac X subtract reminder from rows to get the number of padding
if(s != 0)
s = (rows - s);
//Padding X based on the s value calculated above
for(int p = 0; p < s; p++)
//Padding X with the message
message = message + "X";
//Displays the padded message
System.out.println(" Clear text padded for " + rows + " rows: " + message);
//Displays the Encrypted message
System.out.print(" Encrypted with " + rows + " rows: " );
//Loops till number of rows
for(int i = 0; i+1 <= rows; i++)
{
//Loops from i position to the length of the string by skipping rows count
for (int letter = i; letter < message.length(); letter = letter + rows)
{
System.out.print(message.charAt(letter));
}
}
//Reset the message to the original
message = "is it time to go yet?";
System.out.println(" ");
}
}
}
Output:
Message encrypted with columnar transposition cipher and padding.
Clear text padded for 2 rows: is it time to go yet?X
Encrypted with 2 rows: i ttm og e?si iet oytX
Clear text padded for 3 rows: is it time to go yet?
Encrypted with 3 rows: iiteooesti t mtgy?
Clear text padded for 4 rows: is it time to go yet?XXX
Encrypted with 4 rows: itmo ?s e yX t geXiitotX
Clear text padded for 5 rows: is it time to go yet?XXXX
Encrypted with 5 rows: i o?stt X ioyXim eXtegtX
Clear text padded for 6 rows: is it time to go yet?XXX
Encrypted with 6 rows: itoesi t mg?ieoXt X tyX
Clear text padded for 7 rows: is it time to go yet?
Encrypted with 7 rows: iigsmo e i ytte ott ?
Clear text padded for 8 rows: is it time to go yet?XXX
Encrypted with 8 rows: im sey eittto? XtgXioX
Clear text padded for 9 rows: is it time to go yet?XXXXXX
Encrypted with 9 rows: iees t t?ioXt X gXtoXi XmyX
Clear text padded for 10 rows: is it time to go yet?XXXXXXXXX
Encrypted with 10 rows: i ?stX oXi XtgX oXt XiyXmeXetX
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.