0% found this document useful (0 votes)
3 views

exp 3 css

The document contains Java code for two types of ciphers: a Substitution Cipher and a Transposition Cipher. The Substitution Cipher shifts letters by a specified integer value, while the Transposition Cipher rearranges characters based on a provided key. Both implementations include user input for plaintext and necessary parameters, and output the resulting encrypted text.

Uploaded by

aniketmutal40
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exp 3 css

The document contains Java code for two types of ciphers: a Substitution Cipher and a Transposition Cipher. The Substitution Cipher shifts letters by a specified integer value, while the Transposition Cipher rearranges characters based on a provided key. Both implementations include user input for plaintext and necessary parameters, and output the resulting encrypted text.

Uploaded by

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

EXPERIMENT NO : 01

1] Substitution cipher

import java.util.Scanner;

public class Main {

public static String encrypt(String input, int shift) {

StringBuilder result = new StringBuilder();

for (char c : input.toCharArray()) {

if (Character.isLetter(c)) {

char base = Character.isLowerCase(c) ? 'a' : 'A';

char shiftedChar = (char) ((c - base + shift) % 26 + base);

result.append(shiftedChar);

} else {

result.append(c);

return result.toString();

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.println("Enter the plaintext:");

String plaintext = scanner.nextLine();

System.out.print("Enter the shift value for Substitution Cipher (integer): ");

int shift = scanner.nextInt();

String ciphertext = encrypt(plaintext, shift);

System.out.println("Ciphertext: " + ciphertext);

scanner.close();

Output :
2] Transposition Cipher

import java.util.Scanner;

import java.util.Arrays;

public class Main {

// Columnar Transposition Cipher

public static String transpositionCipher(String plaintext, String key) {

int n = key.length();

int rows = (int) Math.ceil((double) plaintext.length() / n);

char[][] grid = new char[rows][n];

// Fill the grid with plaintext characters

int index = 0;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < n; j++) {

if (index < plaintext.length()) {

grid[i][j] = plaintext.charAt(index++);

} else {

grid[i][j] = 'X'; // Padding character if needed

// Create a map of column indices according to key

Integer[] order = new Integer[n];

for (int i = 0; i < n; i++) {

order[i] = i;

}
Arrays.sort(order, (i, j) -> Character.compare(key.charAt(i), key.charAt(j)));

// Read the columns based on the key's order

StringBuilder result = new StringBuilder();

for (int i = 0; i < n; i++) {

int col = order[i];

for (int j = 0; j < rows; j++) {

result.append(grid[j][col]);

return result.toString();

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Get user input

System.out.println("Enter the plaintext:");

String plaintext = scanner.nextLine();

System.out.println("Enter the key for Transposition Cipher:");

String key = scanner.nextLine();

// Perform the transposition cipher

String encryptedText = transpositionCipher(plaintext, key);

// Display the result

System.out.println("Encrypted Text: " + encryptedText);

scanner.close();

}
Output :

You might also like