Write a program to calculate implement Monoalphabetic cipher encryption - decryption.
Code:
import java.util.Scanner;
public class MonoalphabeticCipher {
public static void main(String[] args) {
char []real={'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'};
char []mono={'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'};
Scanner sc = new Scanner(System.in);
String str;
int len;
System.out.print("1. Encryption 2. Decryption: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter plain text:");
str = sc.next();
len = str.length();
char []ci = new char[len];
for(int i=0;i<len;i++){
for(int j=0;j<26;j++){
if(real[j]==str.charAt(i)){
ci[i]=mono[j];
break;
}
}
}
System.out.print("Encrypted Text is:");
for(int i=0;i<len;i++){
System.out.print(ci[i]);
}
break;
case 2:
System.out.print("Enter Encrypted text:");
str = sc.next();
len = str.length();
char []di=new char[len];
for(int i=0;i<len;i++){
for(int j=0;j<26;j++){
if(mono[j]==str.charAt(i)){
di[i]=real[j];
break;
}
}
}
System.out.print("Decrypted Text is:");
for(int i=0;i<len;i++){
System.out.print(di[i]);
}
break;
default:
break;
}
}
}
Comments
Post a Comment