0% found this document useful (0 votes)
6 views3 pages

Substitution Cipher Techniques Guide

Ciphers

Uploaded by

gogoy76097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Substitution Cipher Techniques Guide

Ciphers

Uploaded by

gogoy76097
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INS Practical Date: / / 2025

1. Write programs to implement the following Substitution Cipher Techniques:


● Caesar Cipher
● Monoalphabetic Cipher

The Caesar Cipher is one of the simplest and oldest encryption techniques.
It's a type of substitution cipher where each letter in the plaintext is shifted a certain number of places down or up the
alphabet.

Caesar Cipher Formula


To encrypt a letter:
Encrypted letter = (Original letter + Shift) % 26
To decrypt:
Decrypted letter = (Encrypted letter - Shift + 26) % 26
(We use +26 before mod to avoid negative numbers.)

Example
Let's say we want to encrypt the word:
Plaintext: HELLO​
Shift: 3
We shift each letter 3 places:
Letter Shifted To
H K
E H
L O
L O
O R
Encrypted Text: KHOOR

Decryption
To decrypt KHOOR with a shift of 3, shift each letter backward by 3:
●​ K → H
●​ H → E
●​ O → L
●​ O → L
●​ R → O
Original Text: HELLO

// [Link]
import [Link];
public class CaesarCipher {
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();

for (char character : [Link]()) {


if ([Link](character)) {
char ch = (char)(((int)character + shift - 65) % 26 + 65);
[Link](ch);
} else if ([Link](character)) {
char ch = (char)(((int)character + shift - 97) % 26 + 97);
[Link](ch);
} else {
[Link](character); // For spaces and symbols
}
}

return [Link]();
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("Enter the plain text: ");


String text = [Link]();

[Link]("Enter shift value (0-25): ");


int shift = [Link]();

String encrypted = encrypt(text, shift);


[Link]("Encrypted Text (Caesar Cipher): " + encrypted);
}
}

Output:

//[Link]

Monoalphabetic Cipher
The Monoalphabetic Cipher is a substitution cipher where each letter of the plaintext is replaced by a unique corresponding
letter of the alphabet.
Unlike the Caesar cipher (which shifts letters a fixed number of positions), the Monoalphabetic Cipher uses a random
permutation of the alphabet.

Monoalphabetic ciphers are stronger than Caesar ciphers, but still not secure.
There are 26! (factorial) possible keys (over 400 septillion), but it can still be broken using frequency analysis (e.g., 'E' is the
most common letter in English).

How It Works
●​ Each letter in the alphabet is mapped to a different letter.
●​ The mapping must be one-to-one and fixed (the same letter always maps to the same substitute).
●​ This creates a key that is a rearranged version of the alphabet.

Example

Let’s define a random substitution key:


Plain A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
Let’s encrypt the word:
Plaintext: HELLO
Use the key to replace each letter:
●​ H → I
●​ E → T
●​ L → S
●​ L → S
●​ O → G
Ciphertext: ITSSG

Decryption
To decrypt ITSSG, we reverse the substitution key:
●​ I → H
●​ T → E
●​ S → L
●​ S → L
●​ G → O
Plaintext: HELLO

import [Link].*;
public class MonoalphabeticCipher {
static String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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


plaintext = [Link]();
key = [Link]();
StringBuilder ciphertext = new StringBuilder();

for (char c : [Link]()) {


int index = [Link](c);
if (index != -1) {
[Link]([Link](index));
} else {
[Link](c); // Non-alphabetic characters
}
}
return [Link]();
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("Enter plain text: ");


String plaintext = [Link]();

[Link]("Enter 26-letter key (e.g., QWERTYUIOPASDFGHJKLZXCVBNM): ");


String key = [Link]();

if ([Link]() != 26) {
[Link]("Invalid key. It must be exactly 26 characters.");
return;
}

String ciphertext = encrypt(plaintext, key);


[Link]("Encrypted Text (Monoalphabetic Cipher): " + ciphertext);
}
}

Output:

You might also like