import java.util.*;
class ProductCipher {
public static void main(String args[]) {
System.out.println(“Enter the input to be encrypted:”);
String substitutionInput = new Scanner(System.in).nextLine();
System.out.println(“Enter a number:”);
int n = new Scanner(System.in).nextInt();
// Substitution encryption
StringBuffer substitutionOutput = new StringBuffer();
for(int i=0 ; i<substitutionInput.length() ; i++) {
char c = substitutionInput.charAt(i);
substitutionOutput.append((char) (c+5));
}
System.out.println(“\nSubstituted text:”);
System.out.println(substitutionOutput);
// Transposition encryption
String transpositionInput = substitutionOutput.toString();
int modulus;
if((modulus = transpositionInput.length()%n) != 0) {
modulus = n-modulus;
// ‘modulus’ is now the number of blanks/padding (X) to be appended
for( ; modulus!=0 ; modulus–) {
transpositionInput += “/”;
}
}
StringBuffer transpositionOutput = new StringBuffer();
System.out.println(“\nTransposition Matrix:”);
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<transpositionInput.length()/n ; j++) {
char c = transpositionInput.charAt(i+(j*n));
System.out.print(c);
transpositionOutput.append(c);
}
System.out.println();
}
System.out.println(“\nFinal encrypted text:”);
System.out.println(transpositionOutput);
// Transposition decryption
n = transpositionOutput.length()/n;
StringBuffer transpositionPlaintext = new StringBuffer();
for(int i=0 ; i<n ; i++) {
for(int j=0 ; j<transpositionOutput.length()/n ; j++) {
char c = transpositionOutput.charAt(i+(j*n));
transpositionPlaintext.append(c);
}
}
// Substitution decryption
StringBuffer plaintext = new StringBuffer();
for(int i=0 ; i<transpositionPlaintext.length() ; i++) {
char c = transpositionPlaintext.charAt(i);
plaintext.append((char) (c-5));
}
System.out.println(“\nPlaintext:”);
System.out.println(plaintext);
}
}
/*
Output:
Enter the input to be encrypted:
The quick brown fox jumps over the lazy dog.
Enter a number:
7
Substituted text:
Ymj%vznhp%gwt|s%kt}%ozrux%t{jw%ymj%qf⌂~%itl3
Transposition Matrix:
Yhszjql
mp%rwf3
j%ku%⌂/
%gtxy~/
vw}%m%/
zt%tji/
n|o{%t/
Final encrypted text:
Yhszjqlmp%rwf3j%ku%⌂/%gtxy~/vw}%m%/zt%tji/n|o{%t/
Plaintext:
The quick brown fox jumps over the lazy dog.*****
*/
Hi sir, i tried your code, but have problem on this line.. (for padding)
for( ; modulus!=0 ; modulus–)
any tips?
Hey Ajirah,
If you’re facing some error due to padding issue, you may first copy+paste the program in Notepad and then try pasting it to your console from there.
for(;modulus!=0;modulus- -)
try this
Sir, i already tried it, but the it state that for this –> for( ; modulus!=0 ; modulus–)
is illegal start of expression, so i cant compile the program 😦
so what do i need to do sir? Bcs i think your code is working well but i cant compile due to the error stated by netbeans
I’m sorry to hear that, Ajirah.
Yes, the code used to work fine in my lab. I was using JDK to run this. Have you tried that? Not sure about Netbeans.
Oh okay, i’ll try it. Thank you so much Sir! 😀
Happy to help, Ajirah.
Replace ‘for( ; modulus!=0 ; modulus–)’ with ‘for(int a=modulus; a!=0; a–)’ then you are good to go
Appreciate your assistance, Prasanna.
sir how use code in netbeans
I’m not entirely sure, Shubham. I used to run this in terminal on Ubuntu.