0% found this document useful (0 votes)
27 views4 pages

Monoalphabetic Substitution Cipher

This document contains code for implementing monoalphabetic and polyalphabetic ciphers in Java. It includes code to encrypt and decrypt strings using a monoalphabetic cipher by substituting each letter with another according to a fixed mapping. It also includes code for a polyalphabetic cipher that shifts each letter by an amount determined by the corresponding letter in an encryption keyword. The code takes plaintext as input, applies the cipher algorithm, and outputs the resulting ciphertext.

Uploaded by

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

Monoalphabetic Substitution Cipher

This document contains code for implementing monoalphabetic and polyalphabetic ciphers in Java. It includes code to encrypt and decrypt strings using a monoalphabetic cipher by substituting each letter with another according to a fixed mapping. It also includes code for a polyalphabetic cipher that shifts each letter by an amount determined by the corresponding letter in an encryption keyword. The code takes plaintext as input, applies the cipher algorithm, and outputs the resulting ciphertext.

Uploaded by

Zoi Añano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Java Program to Implement the Monoalphabetic Cypher

import java.io.*;
class GFG {
public static char normalChar[]
= { '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' };

public static char codedChar[]


= { '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' };

// Function which returns encrypted string


public static String stringEncryption(String s)
{
// initializing an empty String
String encryptedString = "";

// comparing each character of the string and


// encoding each character using the indices
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < 26; j++) {

// comparing the character and


// adding the corresponding char
// to the encryptedString
if (s.charAt(i) == normalChar[j])
{
encryptedString += codedChar[j];
break;
}

// if there are any special characters


// add them directly to the string
if (s.charAt(i) < 'a' || s.charAt(i) > 'z')
{
encryptedString += s.charAt(i);
break;
}
}
}

// return encryptedString
return encryptedString;
}

// Function which returns descryptedString


public static String stringDecryption(String s)
{

// Initializing the string


String decryptedString = "";

// Run the for loop for total string


for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++) {
// compare each characters and decode them
// using indices
if (s.charAt(i) == codedChar[j])
{
decryptedString += normalChar[j];
break;
}

// Add the special characters directly to


// the String
if (s.charAt(i) < 'A' || s.charAt(i) > 'Z')
{
decryptedString += s.charAt(i);
break;
}
}
}

// return the decryptedString


return decryptedString;
}
public static void main(String args[])
{
String str = "Welcome to geeksforgeeks";

// print plain text


System.out.println("Plain text: " + str);

// Changing whole string to lower case


// function call to stringEncryption and storing in
// encryptedString
String encryptedString = stringEncryption(str.toLowerCase());

// printing encryptedString
System.out.println("Encrypted message: "
+ encryptedString);

// function call to stringDecryption and printing


// the decryptedString
System.out.println("Decrypted message: "
+ stringDecryption(encryptedString));

}
}

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MonoalphabeticCipher {


public static void main(String[] args) {
// Create a substitution key mapping for encryption
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String key = "zyxwvutsrqponmlkjihgfedcba"; // Example key, you can change
it

Map<Character, Character> substitutionKey = new HashMap<>();


for (int i = 0; i < alphabet.length(); i++) {
char plainChar = alphabet.charAt(i);
char cipherChar = key.charAt(i);
substitutionKey.put(plainChar, cipherChar);
}

// Input plaintext from the user


Scanner scanner = new Scanner(System.in);
System.out.print("Enter plaintext: ");
String plaintext = scanner.nextLine().toLowerCase(); // Convert to
lowercase for simplicity

// Encrypt the plaintext


StringBuilder ciphertext = new StringBuilder();
for (char plainChar : plaintext.toCharArray()) {
if (Character.isLetter(plainChar)) {
char cipherChar = substitutionKey.getOrDefault(plainChar,
plainChar);
ciphertext.append(cipherChar);
} else {
// Preserve non-alphabet characters as they are
ciphertext.append(plainChar);
}
}

// Output the ciphertext


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

import java.util.Scanner;

public class PolyalphabeticCipher {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input plaintext from the user


System.out.print("Enter plaintext: ");
String plaintext = scanner.nextLine().toLowerCase(); // Convert to
lowercase for simplicity

// Input encryption keyword from the user


System.out.print("Enter encryption keyword: ");
String keyword = scanner.nextLine().toLowerCase(); // Convert to lowercase
for consistency

// Encrypt the plaintext


String ciphertext = encrypt(plaintext, keyword);

// Output the ciphertext


System.out.println("Ciphertext: " + ciphertext);
}
public static String encrypt(String plaintext, String keyword) {
StringBuilder ciphertext = new StringBuilder();
int keywordLength = keyword.length();
int keywordIndex = 0;

for (char plainChar : plaintext.toCharArray()) {


if (Character.isLetter(plainChar)) {
char keywordChar = keyword.charAt(keywordIndex % keywordLength);
int shift = keywordChar - 'a'; // Calculate the shift based on the
keyword character

char encryptedChar = (char) ('a' + (plainChar - 'a' + shift) % 26);


ciphertext.append(encryptedChar);

keywordIndex++; // Move to the next character in the keyword


} else {
// Preserve non-alphabet characters as they are
ciphertext.append(plainChar);
}
}

return ciphertext.toString();
}
}

You might also like