0% found this document useful (2 votes)
4K views

Substitution and Transposition Cipher

The experiment aims to implement a product cipher using substitution and transposition ciphers. It explains that a substitution cipher replaces plaintext units with cipher text according to a fixed system, while a transposition cipher reorders the plaintext units. A product cipher combines multiple transformations to increase security. The program takes an input, encrypts it using a substitution cipher that shifts each character by 5, then a transposition cipher that reorders the characters in a matrix based on a user-input number. It decrypts the output back to plaintext using the reverse transformations.

Uploaded by

coastudies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
4K views

Substitution and Transposition Cipher

The experiment aims to implement a product cipher using substitution and transposition ciphers. It explains that a substitution cipher replaces plaintext units with cipher text according to a fixed system, while a transposition cipher reorders the plaintext units. A product cipher combines multiple transformations to increase security. The program takes an input, encrypts it using a substitution cipher that shifts each character by 5, then a transposition cipher that reorders the characters in a matrix based on a user-input number. It decrypts the output back to plaintext using the reverse transformations.

Uploaded by

coastudies
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment no: 01

Aim: Implementation of a product cipher using Substitution and Transposition ciphers

Theory:

A substitution cipher is a method of encrypting by which units of plaintext are replaced with
cipher text, according to a fixed system; the "units" may be single letters (the most common),
pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers
the text by performing the inverse substitution.

A transposition cipher is methods of encryption by which the positions held by units of


plaintext (which are commonly characters or groups of characters) are shifted according to a
regular system, so that the cipher text constitutes a permutation of the plaintext. That is, the
order of the units is changed (the plaintext is reordered). Mathematically a bijective function
is used on the characters' positions to encrypt and an inverse function to decrypt.

In cryptography, a product cipher combines two or more transformations in a manner


intending that the resulting cipher is more secure than the individual components to make it
resistant to cryptanalysis. The product cipher combines a sequence of simple transformations
such as substitution (S-box), permutation (P-box), and modular arithmetic.

Program:

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(int a=modulus;a!=0;a--)
{
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);
}
}
}
}

Output:

Conclusion:

You might also like