Sei sulla pagina 1di 9

DEPARTMENT OF COMPUTER ENGINEERING Experiment No.

4
Semester B.E. Semester VIIi– Computer Engineering
Subject Cryptography and System Security
Subject Professor In- Prof. Sneha Annappanavar
charge
Assisting Teachers Prof. Sneha Annappanavar
Laboratory M 312 A

Student Name Madhura Bartakke


Roll Number 15102B0035
Grade and Subject
Teacher’s Signature

Experiment 4
Number
Experiment Implement keyed transposition cipher technique- (a) Simple Columnar (b) Double
Title Columnar
Resources / Hardware: Software:
Apparatus Normal PC configuration Java
Required

Objectives In this Experiment, we can understand the concept of Simple Columnar and Double
Columnar and difference between them.
Theory of
Operation (a)Simple Columnar TranspositonTechnique:

It is another type of cipher where the order of the alphabets in the plaintext is
rearranged to create the ciphertext. The actual plaintext alphabets are not replaced.

Algorithm for Encryption:


Step1: Write the plain text message row by row in the rectangle of predefined size.
Step2: Read the message column by column, but not in the sequential way, we have to
follow the key specified to obtain the cipher text.
An example is a ‘simple columnar transposition’ cipher where the plaintext is written
horizontally with a certain alphabet width. Then the ciphertext is read vertically as
shown.

For example, the plaintext is “golden statue is in eleventh cave” and the secret random
key chosen is “five”. We arrange this text horizontally in table with number of column
equal to key value. The resulting text is shown below.
The ciphertext is obtained by reading column vertically downward from first to last
column. The ciphertext is ‘gnuneaoseenvltiltedasehetivc’.

To decrypt, the receiver prepares similar table. The number of columns is equal to key
number. The number of rows is obtained by dividing number of total ciphertext
alphabets by key value and rounding of the quotient to next integer value.

The receiver then writes the received ciphertext vertically down and from left to right
column. To obtain the text, he reads horizontally left to right and from top to bottom
row.

(b)Double Coumnar Transposition Cipher:

The Double Columnar Transposition was introduced is a modification of the Columnar


Transposition. It is quite similar to its predecessor, and it has been used in similar
situations.
The Double Columnar Transposition rearranges the plaintext letters, based on matrices
filled with letters in the order determined by the secret keyword.
The encryption and decryption can be performed by hand, using a piece of paper and a
simple matrix, in a similar way as it is done for the Columnar Transposition.

Algorithm for Encryption:


Step1: Write the plain text message row by row in the rectangle of predefined size.
Step2: Read the message column by column, but not in the sequential way, we have to
follow the key specified to obtain the cipher(middle) text.
Step3: Repeat the step1 and step2 to generate the cipher text.

Ex: Plaintext: attack at four

First transportation: permute rows from (1, 2, 3) to (3, 2, 1)


Middle text: fourckatatta
Second transportation: permute columns from (1, 2, 3, 4) to (4, 2, 1, 3)

Ciphertext: ROFUTKCAATAT

Program (a) Simple Columnar


import java.util.*;
class scol{
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();
int columnCount = key.length();
int rowCount = (message.length()+columnCount)/columnCount;
int plainText[][] = new int[rowCount][columnCount];
int cipherText[][] = new int[rowCount][columnCount];

System.out.print("\n-----Encryption-----\n");
cipherText = encrypt(plainText, cipherText, message, rowCount, columnCount, key);
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);

System.out.print("\n\n\n-----Decryption-----\n");
plainText = decrypt(plainText, cipherText, ct, rowCount, columnCount, key);
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;
for(i=0; i<rowCount; i++)
{
for(j=0; j<columnCount; j++)
{

if(k < message.length())


{
/* respective ASCII characters would be placed */
plainText[i][j] = (int)message.charAt(k);
k++;
}
else
{
break;
}
}
}
for(i=0; i<columnCount; i++)
{
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;
}
}

Double Columnar:
import java.util.*;
class double{
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();
int columnCount = key.length();
int rowCount = (message.length()+columnCount)/columnCount;
int plainText[][] = new int[rowCount][columnCount];
int cipherText[][] = new int[rowCount][columnCount];

System.out.print("\n-----Encryption-----\n");
cipherText = encrypt(plainText, cipherText, message, rowCount, columnCount, key);
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("\nMiddle Text: " + ct);

int cipherText2[][] = new int[rowCount][columnCount];


cipherText2 = encrypt(plainText, cipherText2, ct, rowCount, columnCount, key);

String ct2 = "";


for(int i=0; i<columnCount; i++)
{
for(int j=0; j<rowCount; j++)
{
if(cipherText[j][i] == 0)
ct2 = ct2 + 'x';
else{
ct2 = ct2 + (char)cipherText2[j][i];
}
}
}
System.out.print("\nCipher Text: " + ct2);

System.out.print("\n\n\n-----Decryption-----\n");
plainText = decrypt(plainText, cipherText, ct, rowCount, columnCount, key);
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;

for(i=0; i<rowCount; i++)


{
for(j=0; j<columnCount; j++)
{

if(k < message.length())


{
/* respective ASCII characters would be placed */
plainText[i][j] = (int)message.charAt(k);
k++;
}
else
{
break;
}
}
}
for(i=0; i<columnCount; i++)
{
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 Single Columnar:


Double Columnar:

Conclusion:
In this Experiment, we can understand the concept of Simple Columnar and Double
Columnar and difference between them.

Potrebbero piacerti anche