1// Aim: Program to perform Monoalphabetic cipher (Monoalphabetic Cipher).
2import java.util.*;
3import java.util.Scanner;
4public class mono {
5 private static String alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
6 public static String encrypt(String text ,String key){
7 text.toUpperCase();
8 StringBuilder ct = new StringBuilder();
9 for(char c : text.toCharArray()){
10 int idx = alfa.indexOf(c);
11 if(idx!=-1){
12 ct.append(key.charAt(idx));
13 }
14 else{
15 ct.append(c);
16 }
17 }
18 return ct.toString();
19 }
20 public static String decrypt(String ct ,String key){
21 ct.toUpperCase();
22 StringBuilder pt = new StringBuilder();
23 for(char c : ct.toCharArray()){
24 int idx = key.indexOf(c);
25 if(idx!=-1){
26 pt.append(alfa.charAt(idx));
27 }
28 else{
29 pt.append(c);
30 }
31 }
32 return pt.toString();
33 }
34
35}
36