CSS Shruti Experiment No. 01
CSS Shruti Experiment No. 01
Aim: Design and Implementation of a product cipher using Substitution and Transposition
Objectives:
To understand the encryption and decryption fundamentals.
To understand the concepts of the product cipher.
Theory:
Substitution cipher is a method of encryption by which units of plaintext are replaced with
ciphertext according to a regular 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 an inverse substitution.
Substitution ciphers can be compared with Transposition ciphers. In a transposition cipher, the
units of the plaintext are rearranged in a different and usually quite complex order, but the
units themselves are left unchanged. By contrast, in a substitution cipher, the units of the
plaintext are retained in the same sequence in the ciphertext, but the units themselves are
altered.
Caesar Cipher: In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift
cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption
techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by
a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A
would be replaced by D, B would become E, and so on. The method is named after Julius
Caesar, who used it to communicate with his generals.
Example:
The transformation can be represented by aligning two alphabets? the cipher alphabet is the
plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar
cipher using a left rotation of three places (the shift parameter, here 3, is used as the key):
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC
In a regular columnar transposition cipher, any spare spaces are filled with nulls? in an
irregular columnar transposition cipher, the spaces are left blank. Finally, the message is read
off in columns, in the order specified by the keyword.
Algorithm/Procedure:
Substitution
Transposition
1. Count how many letters are in your ciphertext (for example, 75) and factor that
number (75
=5*5*3).
2. Create all of the possible matrices to fit this ciphertext (in our case, 3x25, 5x15,
15x5, 25x3).
3. Write the ciphertext into these matrices down the columns.
4. For each of your matrices, consider all of the possible permutations of the
columns (for n columns, there are n! possible rearrangements). In our case, we
hope that the message was enciphered using one of the last two matrices (the
15x5 and the 25x3), since in those cases, we have only 6 and 120 possibilities
to check (3! = 6, 5! = 120, 15! ~ 1.31x10^12, 25! ~ 1.55x10^25).
5. Rearrange each matrix to see if you get anything intelligible. Read the message
off rowby row. Note that this is much more easily done by a computer than by
hand, but it is doable (for small matrices).
Source Code:
import java.util.*;
class Cipher {
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;
// Transposition decryption
n = transpositionOutput.length() / n;
StringBuffer transpositionPlaintext = new StringBuffer();
// 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:
Conclusion :
A product cipher is a composite of two or more elementary ciphers with the goal of
producing a cipher which is more secure that any of the individual components. In
product cipher substitution and transposition are applied to create confusion and
diffusion in the text message.