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

Eces Experiment - 1

The document describes an experiment to implement the Caesar cipher and monoalphabetic cipher to encrypt plaintext into ciphertext. It includes code for the Caesar cipher that encrypts and decrypts text by shifting letters by a key value. It also includes code for the monoalphabetic cipher that uses a keyword to map letters to other letters for encryption and decryption. The code takes a plaintext string, encryption key, and encrypts or decrypts the text accordingly, then prints the output.

Uploaded by

Jaimish
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)
38 views4 pages

Eces Experiment - 1

The document describes an experiment to implement the Caesar cipher and monoalphabetic cipher to encrypt plaintext into ciphertext. It includes code for the Caesar cipher that encrypts and decrypts text by shifting letters by a key value. It also includes code for the monoalphabetic cipher that uses a keyword to map letters to other letters for encryption and decryption. The code takes a plaintext string, encryption key, and encrypts or decrypts the text accordingly, then prints the output.

Uploaded by

Jaimish
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
You are on page 1/ 4

EXPERIMENT: 1

AIM: IMPLEMENT CAESAR CIPHER AND MONOALPHABETIC CIPHER TO CONVERT


PLAIN TEXT INTO CIPHER TEXT

CAESAR CIPHER:

import​ java.util.Scanner;
import​ java.lang.*;

class​ Cipher{
​private​ ​int​ key;

​public​ Cipher​(​int​ key)​{


​this​.key = key;
}

​public​ String encrypt​(String plain)​{


StringBuilder cipher_text = ​new​ StringBuilder(plain.length());
​int​ n = plain.length();

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


​char​ c = plain.charAt(i);
​int​ unic;
​if​(Character.isUpperCase(c)){
unic = ​65​;
}​else​{
unic = ​97​;
}
cipher_text.append((​char​)(((​int​)c+​this​.key-unic)%​26​ +
unic));
}

​return​ cipher_text.toString();
}

​public​ String decrypt​(String plain)​{


StringBuilder cipher_text = ​new​ StringBuilder(plain.length());
​int​ n = plain.length();

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


​char​ c = plain.charAt(i);
​int​ unic;
​if​(Character.isUpperCase(c)){
unic = ​65​;
}​else​{
unic = ​97​;
}
cipher_text.append((​char​)(((​int​)c-​this​.key-unic)%​26​ +
unic));
}

​return​ cipher_text.toString();
}

​public​ ​static​ ​void​ main​(String[] args)​{


System.out.print(​"Enter a key : "​);
Scanner t = ​new​ Scanner(System.in);
​int​ key = t.nextInt();
Cipher c = ​new​ Cipher(key);
Scanner sc = ​new​ Scanner(System.in);
System.out.print(​"Enter a text : "​);
String s = sc.nextLine();
String e = c.encrypt(s);
System.out.println(​"Encrypted Text : "​+ e);
System.out.println(​"Decrypted Text : "​+ c.decrypt(e));
}
}

OUTPUT:
MONOALPHABETIC CIPHER:

import​ java.util.*;
import​ java.lang.*;

class​ Mono{
​private​ String keyword;
​private​ Map<Character, Character> mapp;
​private​ Map<Character, Character> inv_mapp;

​public​ Mono​(String keyword)​{


​this​.keyword = keyword;
mapp = ​new​ HashMap<>();
inv_mapp = ​new​ HashMap<>();
prepareMap(keyword);
prepareInvMap(mapp);
}

​private​ ​void​ prepareMap​(String keyword)​{


StringBuilder track = ​new​ StringBuilder(​26​);
​int​ i;
​for​(i=​0​; i<keyword.length(); i++){
​this​.mapp.put((​char​)(​65​+i), keyword.charAt(i));
track.append(keyword.charAt(i));
}
String t = track.toString();
​int​ m = ​65​;
​for​(​int​ j=​65​+i; j<​91​; j++){
​while​(t.indexOf((​char​)m) != -​1​){
m++;
}
​this​.mapp.put((​char​)(j), (​char​)m);
StringBuilder temp = ​new​ StringBuilder(t);
temp.insert(t.length(), (​char​)m);
t = temp.toString();
}
​this​.mapp.put(​' '​, ​' '​);
}

​private​ ​void​ prepareInvMap​(Map<Character, Character> mapp)​{


​for​(Map.Entry m : mapp.entrySet()){
​this​.inv_mapp.put((Character)m.getValue(),
(Character)m.getKey());
}
}
​public​ String encrypt​(String plain)​{
StringBuilder encrypt_text = ​new​ StringBuilder(plain.length());
​for​(​int​ i=​0​; i<plain.length(); i++){
​char​ c = (​char​)​this​.mapp.get(plain.charAt(i));
encrypt_text.append(c);
}
​return​ encrypt_text.toString();
}

​public​ String decrypt​(String plain)​{


StringBuilder decrypt_text = ​new​ StringBuilder(plain.length());

​for​(​int​ i=​0​; i<plain.length(); i++){


​char​ c = (​char​)​this​.inv_mapp.get(plain.charAt(i));
decrypt_text.append(c);
}
​return​ decrypt_text.toString();
}

​public​ ​static​ ​void​ main​(String[] args)​{


System.out.print(​"Enter A Key : "​);
Scanner sc = ​new​ Scanner(System.in);
String key = sc.nextLine();
Mono c = ​new​ Mono(key);
System.out.print(​"Enter a text : "​);
String plain = sc.nextLine();
String e = c.encrypt(plain);
System.out.println(​"Encrypted Text : "​+ e);
System.out.println(​"Decrypted Text : "​+ c.decrypt(e));
}
}

OUTPUT:

You might also like