Program:
import java.util.*;
class simpleColumnar{
public static void main(String sap[]){
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter plaintext(enter in lower case): ");
String message = sc.next();
System.out.print("\nEnter key in numbers: ");
String key = sc.next();
/* columnCount would keep track of columns */
int columnCount = key.length();
/* rowCount will keep of track of rows...no of rows = (plaintextlength + keylength) / keylength */
int rowCount = (message.length()+columnCount)/columnCount;
/*plainText and cipherText would be array containing ASCII values for respective alphabets */
int plainText[][] = new int[rowCount][columnCount];
int cipherText[][] = new int[rowCount][columnCount];
/*Encryption Process*/
System.out.print("\n-----Encryption-----\n");
cipherText = encrypt(plainText, cipherText, message, rowCount, columnCount, key);
// prepare final string
String ct = "";
for(int i=0; i<columnCount; i++)
{
for(int j=0; j<rowCount; j++)
{
if(cipherText[j][i] == 0)
ct = ct + 'x';
else{
ct = ct + (char)cipherText[j][i];
}
}
}
System.out.print("\nCipher Text: " + ct);
/*Decryption Process*/
System.out.print("\n\n\n-----Decryption-----\n");
plainText = decrypt(plainText, cipherText, ct, rowCount, columnCount, key);
// prepare final string
String pt = "";
for(int i=0; i<rowCount; i++)
{
for(int j=0; j<columnCount; j++)
{
if(plainText[i][j] == 0)
pt = pt + "";
else{
pt = pt + (char)plainText[i][j];
}
}
}
System.out.print("\nPlain Text: " + pt);
System.out.println();
}
static int[][] encrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){
int i,j;
int k=0;
/* here array would be filled row by row */
for(i=0; i<rowCount; i++)
{
for(j=0; j<columnCount; j++)
{
/* terminating condition...as string length can be smaller than 2-D array */
if(k < message.length())
{
/* respective ASCII characters would be placed */
plainText[i][j] = (int)message.charAt(k);
k++;
}
else
{
break;
}
}
}
/* here array would be filled according to the key column by column */
for(i=0; i<columnCount; i++)
{
/* currentCol would have current column number i.e. to be read...as there would be ASCII value stored in key so we would subtract it by 48 so that we can get the original number...and -1 would be subtract as array position starts from 0*/
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
cipherText[j][i] = plainText[j][currentCol];
}
}
System.out.print("Cipher Array(read column by column): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)cipherText[i][j]+"\t");
}
System.out.println();
}
return cipherText;
}
static int[][] decrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){
int i,j;
int k=0;
for(i=0; i<columnCount; i++)
{
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
plainText[j][currentCol] = cipherText[j][i];
}
}
System.out.print("Plain Array(read row by row): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)plainText[i][j]+"\t");
}
System.out.println();
}
return plainText;
}
}
import java.util.*;
class simpleColumnar{
public static void main(String sap[]){
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter plaintext(enter in lower case): ");
String message = sc.next();
System.out.print("\nEnter key in numbers: ");
String key = sc.next();
/* columnCount would keep track of columns */
int columnCount = key.length();
/* rowCount will keep of track of rows...no of rows = (plaintextlength + keylength) / keylength */
int rowCount = (message.length()+columnCount)/columnCount;
/*plainText and cipherText would be array containing ASCII values for respective alphabets */
int plainText[][] = new int[rowCount][columnCount];
int cipherText[][] = new int[rowCount][columnCount];
/*Encryption Process*/
System.out.print("\n-----Encryption-----\n");
cipherText = encrypt(plainText, cipherText, message, rowCount, columnCount, key);
// prepare final string
String ct = "";
for(int i=0; i<columnCount; i++)
{
for(int j=0; j<rowCount; j++)
{
if(cipherText[j][i] == 0)
ct = ct + 'x';
else{
ct = ct + (char)cipherText[j][i];
}
}
}
System.out.print("\nCipher Text: " + ct);
/*Decryption Process*/
System.out.print("\n\n\n-----Decryption-----\n");
plainText = decrypt(plainText, cipherText, ct, rowCount, columnCount, key);
// prepare final string
String pt = "";
for(int i=0; i<rowCount; i++)
{
for(int j=0; j<columnCount; j++)
{
if(plainText[i][j] == 0)
pt = pt + "";
else{
pt = pt + (char)plainText[i][j];
}
}
}
System.out.print("\nPlain Text: " + pt);
System.out.println();
}
static int[][] encrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){
int i,j;
int k=0;
/* here array would be filled row by row */
for(i=0; i<rowCount; i++)
{
for(j=0; j<columnCount; j++)
{
/* terminating condition...as string length can be smaller than 2-D array */
if(k < message.length())
{
/* respective ASCII characters would be placed */
plainText[i][j] = (int)message.charAt(k);
k++;
}
else
{
break;
}
}
}
/* here array would be filled according to the key column by column */
for(i=0; i<columnCount; i++)
{
/* currentCol would have current column number i.e. to be read...as there would be ASCII value stored in key so we would subtract it by 48 so that we can get the original number...and -1 would be subtract as array position starts from 0*/
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
cipherText[j][i] = plainText[j][currentCol];
}
}
System.out.print("Cipher Array(read column by column): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)cipherText[i][j]+"\t");
}
System.out.println();
}
return cipherText;
}
static int[][] decrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){
int i,j;
int k=0;
for(i=0; i<columnCount; i++)
{
int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++)
{
plainText[j][currentCol] = cipherText[j][i];
}
}
System.out.print("Plain Array(read row by row): \n");
for(i=0;i<rowCount;i++){
for(j=0;j<columnCount;j++){
System.out.print((char)plainText[i][j]+"\t");
}
System.out.println();
}
return plainText;
}
}
Output:
Enter plaintext(enter in lower case): networksecurity
Enter key in numbers: 31452
-----Encryption-----
Cipher Array(read column by column):
t n w o e
s r e c k
i u t y r
Cipher Text: tsixnruxwetxocyxekrx
-----Decryption-----
Plain Array(read row by row):
n e t w o
r k s e c
u r i t y
Plain Text: networksecurity
Bhai bhai
ReplyDeleteThanks Bro...
DeleteGreat Article android based projects
DeleteJava Training in Chennai
Project Center in Chennai
Java Training in Chennai
projects for cse
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Thanks for giving single columnar transposition program which improves my knowledge a lot. Keep updating...
ReplyDeleteBest Online Software Training Institute | Core Java Training
what we need to do if we want that key should accept letters digits also?
ReplyDeletemyTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance
ReplyDeleteNice post....sas training in Marathahalli
ReplyDeletesas training institutes in Marathahalli
Thanks for sharing this post
ReplyDeletejava training in Bangalore
spring training in Bangalore
java training institute in Bangalore
spring and hibernate training in Bangalore
Nice post..
ReplyDeletebest android training center in btm
best android development institute in btm
android training institutes in btm
ios training in btm
android training in btm
mobile app development training in btm
Nice post..
ReplyDeleteDOT NET training in btm
dot net training institute in btm
dot net course in btm
best dot net training institute in btm
Nice Article
ReplyDeleteangularjs training in Bangalore
angularjs training institutes in Bangalore
best angularjs training in Bangalore
Really this is a very nice informative post. This is the good informative post for the Core Java freshers. Thank you for posting the information.
ReplyDeleteHi I want to talk about one site fantastic best gambling sites there I spend all my free time there I spend all my free time and get a small income
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeleteCEH Training In Hyderbad
Nebosh courses in Chennai
ReplyDeleteNebosh HSW Course in Chennai
Nebosh course in Chennai
Nebosh HSL course in Chennai
Nebosh
Nebosh Process Safety Management course
palan
ReplyDelete1 no
mast builder
kadak na
khidikitod
AES algorithm bhi dalo
I got what i am searching from last few days in your Blog. I hope you will share more info about it. Please keep sharing...
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
good article
ReplyDeletethanks for sharing
data science course in chennai
ccna course in chennai
iot course in chennai
ethical hacking course in chennai
cyber security course in chennai
Great Informative post. I really Appreciate your work. Thank you for sharing the informationsalesforce course in chennai
ReplyDeletesoftware testing course in chennai
robotic process automation rpa course in chennai
blockchain course in chennai
devops course in chennai
Learned a lot of new things in this post. Thanks for taking the time to share this blog...
ReplyDeletewhat does a devops engineer do
what is soft skill development
how to learn tableau
best way to improve spoken english
blue prism technical interview questions
blue prism interview questions for freshers