1)Write a code to Implement Encryption and Decryption for Ceaser Cipher
Code:
import java.util.Scanner;
class CeaserCipher1{
public String encrypt(String pt,int key){
StringBuilder et = new StringBuilder();
char ch;
for(int i=0;i<pt.length();i++){
ch=(char)((pt.charAt(i) + key-'a')%26+'a');
et.append(ch);
}
return et.toString();
}
public String decrypt(String ct, int key){
StringBuilder dt = new StringBuilder();
char ch;
for(int i=0; i<ct.length();i++){
ch=(char)((ct.charAt(i)-key-'a'+26)%26+'a');
dt.append(ch);
}
return dt.toString();
}
}
class CeaserCipher
{
public static void main(String[] args){
CeaserCipher1 cc = new CeaserCipher1();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Plaintext Text:");
String pt=sc.next();
System.out.println("Enter Key:");
int key=sc.nextInt();
String ct=cc.encrypt(pt,key);
System.out.println("Encrypted text:"+ct);
pt=cc.decrypt(ct,key);
System.out.println("Decrypted text:"+pt);
sc.close();
}
}
Output:
Enter Plaintext Text:
hello
Enter Key:
3
Encrypted text:khoor
Decrypted text:hello
2)Write a code Implement Encryption and Decryption for PlayfairCipher
Code:
import java.util.Scanner;
class PlayFairCipher1 {
private static char[][] keyMatrix = new char[5][5];
String encrypt(String plaintext) {
plaintext = preprocessText(plaintext);
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i < plaintext.length(); i += 2) {
char a = plaintext.charAt(i);
char b = plaintext.charAt(i + 1);
int[] posA = findPosition(a);
int[] posB = findPosition(b);
if (posA == null || posB == null) {
throw new IllegalArgumentException("Character not found in key matrix");
}
if (posA[0] == posB[0]) { // Same row
ciphertext.append(keyMatrix[posA[0]][(posA[1] + 1) % 5]);
ciphertext.append(keyMatrix[posB[0]][(posB[1] + 1) % 5]);
} else if (posA[1] == posB[1]) { // Same column
ciphertext.append(keyMatrix[(posA[0] + 1) % 5][posA[1]]);
ciphertext.append(keyMatrix[(posB[0] + 1) % 5][posB[1]]);
} else {
ciphertext.append(keyMatrix[posA[0]][posB[1]]);
ciphertext.append(keyMatrix[posB[0]][posA[1]]);
}
}
return ciphertext.toString();
}
// Generate the key matrix
void generateKeyMatrix(String key) {
key = key.replace("j", "i").toLowerCase();
boolean[] used = new boolean[26];
int index = 0;
for (char c : key.toCharArray()) {
if (c >= 'a' && c <= 'z' && !used[c - 'a']) {
keyMatrix[index / 5][index % 5] = c;
used[c - 'a'] = true;
index++;
}
}
for (char c = 'a'; c <= 'z'; c++) {
if (c != 'j' && !used[c - 'a']) {
keyMatrix[index / 5][index % 5] = c;
used[c - 'a'] = true;
index++;
}
}
System.out.println("Key Matrix:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(keyMatrix[i][j] + " ");
}
System.out.println();
}
}
String decrypt(String ciphertext) {
StringBuilder plaintext = new StringBuilder();
for (int i = 0; i < ciphertext.length(); i += 2) {
char a = ciphertext.charAt(i);
char b = ciphertext.charAt(i + 1);
int[] posA = findPosition(a);
int[] posB = findPosition(b);
if (posA == null || posB == null) {
throw new IllegalArgumentException("Character not found in key matrix");
}
if (posA[0] == posB[0]) {
plaintext.append(keyMatrix[posA[0]][(posA[1] + 4) % 5]);
plaintext.append(keyMatrix[posB[0]][(posB[1] + 4) % 5]);
} else if (posA[1] == posB[1]) {
plaintext.append(keyMatrix[(posA[0] + 4) % 5][posA[1]]);
plaintext.append(keyMatrix[(posB[0] + 4) % 5][posB[1]]);
} else { // Rectangle swap
plaintext.append(keyMatrix[posA[0]][posB[1]]);
plaintext.append(keyMatrix[posB[0]][posA[1]]);
}
}
return plaintext.toString().replace("x", "");
}
// Preprocess the input text
private String preprocessText(String text) {
text = text.toLowerCase().replaceAll("[^a-z]", "").replace("j", "i");
StringBuilder processed = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
processed.append(text.charAt(i));
if (i < text.length() - 1 && text.charAt(i) == text.charAt(i + 1)) {
processed.append('x');
}
}
if (processed.length() % 2 != 0) {
processed.append('x');
}
return processed.toString();
}
private int[] findPosition(char c) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (keyMatrix[i][j] == c) {
return new int[]{i, j};
}
}
}
return null;
}
}
public class PlayfairCipher {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PlayFairCipher1 playFairCipher = new PlayFairCipher1();
System.out.print("Enter the key: ");
String key = sc.nextLine();
System.out.print("Enter the plaintext: ");
String plaintext = sc.nextLine();
playFairCipher.generateKeyMatrix(key);
String ciphertext = playFairCipher.encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = playFairCipher.decrypt(ciphertext);
System.out.println("Decrypted Text: " + decryptedText);
sc.close();
}
}
Output:
Enter the key: keerth
Enter the plaintext: hanuman
Key Matrix:
kerth
abcdf
gilmn
opqsu
vwxyz
Ciphertext: kfuzgdlz
Decrypted Text: hanuman
3)Write a code to Implement Encryption and Decryption for Hill Cipher
Code:
class HillCipher {
// Method to generate the key matrix from the key string
static void getKeyMatrix(String key, int keyMatrix[][]) {
int k = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
keyMatrix[i][j] = (key.charAt(k)) % 65; // Convert A-Z to 0-25
k++;
}
}
}
// Method to encrypt the message
static void encrypt(int cipherMatrix[][], int keyMatrix[][], int messageVector[][]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 1; j++) {
cipherMatrix[i][j] = 0;
for (int x = 0; x < 3; x++) {
cipherMatrix[i][j] += keyMatrix[i][x] * messageVector[x][j];
}
cipherMatrix[i][j] %= 26; // Keep within 0-25
}
}
}
// Method to find the determinant of a 3x3 matrix modulo 26
static int determinant(int matrix[][]) {
int det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
det %= 26;
if (det < 0) det += 26;
return det;
}
// Method to calculate the modular multiplicative inverse of 'a' under modulo 'm'
static int modInverse(int a, int m) {
a %= m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return 1; // Default return for non-invertible numbers
}
// Method to find the adjoint of a 3x3 matrix
static void adjoint(int matrix[][], int adj[][]) {
adj[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) % 26;
adj[0][1] = (-(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])) % 26;
adj[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) % 26;
adj[1][0] = (-(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])) % 26;
adj[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) % 26;
adj[1][2] = (-(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0])) % 26;
adj[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) % 26;
adj[2][1] = (-(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0])) % 26;
adj[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) % 26;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (adj[i][j] < 0) adj[i][j] += 26;
}
}
}
// Method to decrypt the ciphertext
static void decrypt(int cipherMatrix[][], int keyMatrix[][]) {
int det = determinant(keyMatrix);
int detInverse = modInverse(det, 26);
int adjMatrix[][] = new int[3][3];
adjoint(keyMatrix, adjMatrix);
int inverseMatrix[][] = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
inverseMatrix[i][j] = (adjMatrix[i][j] * detInverse) % 26;
if (inverseMatrix[i][j] < 0) inverseMatrix[i][j] += 26;
}
}
int messageVector[][] = new int[3][1];
for (int i = 0; i < 3; i++) {
messageVector[i][0] = 0;
for (int j = 0; j < 3; j++) {
messageVector[i][0] += inverseMatrix[i][j] * cipherMatrix[j][0];
}
messageVector[i][0] %= 26;
}
String plaintext = "";
for (int i = 0; i < 3; i++) {
plaintext += (char) (messageVector[i][0] + 65);
}
System.out.println("Decrypted Text: " + plaintext);
}
}
class HillCipher1 {
public static void main(String[] args) {
String message = "ACT";
String key = "GYBNQKURP";
int keyMatrix[][] = new int[3][3];
HillCipher.getKeyMatrix(key, keyMatrix);
int messageVector[][] = new int[3][1];
for (int i = 0; i < 3; i++) {
messageVector[i][0] = (message.charAt(i)) % 65;
}
int cipherMatrix[][] = new int[3][1];
HillCipher.encrypt(cipherMatrix, keyMatrix, messageVector);
String ciphertext = "";
for (int i = 0; i < 3; i++) {
ciphertext += (char) (cipherMatrix[i][0] + 65);
}
System.out.println("Ciphertext: " + ciphertext);
HillCipher.decrypt(cipherMatrix, keyMatrix);
}
}
Output:
Ciphertext: POH
Decrypted Text: ACT
4)Write a code to Implement Encryption and Decryption for Vigenere cipher
Code:
import java.util.Scanner;
class VigenereCipher1 {
public String encrypt(String plaintext, String key) {
StringBuilder ciphertext = new StringBuilder();
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
key = generateKey(plaintext, key.toUpperCase().replaceAll("[^A-Z]", ""));
for(int i = 0; i < plaintext.length(); i++){
char p = plaintext.charAt(i);
char k = key.charAt(i);
char c = (char) ((p + k - 2 * 'A') % 26 + 'A');
ciphertext.append(c);
}
return ciphertext.toString();
public String decrypt(String ciphertext, String key){
StringBuilder plaintext = new StringBuilder();
ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", "");
key = generateKey(ciphertext, key.toUpperCase().replaceAll("[^A-Z]", ""));
for(int i = 0; i < ciphertext.length(); i++){
char c = ciphertext.charAt(i);
char k = key.charAt(i);
char p = (char) ((c - k + 26) % 26 + 'A');
plaintext.append(p);
}
return plaintext.toString();
}
private String generateKey(String text, String key){
StringBuilder extendedKey = new StringBuilder(key);
while(extendedKey.length() < text.length()){
extendedKey.append(key);
}
return extendedKey.substring(0, text.length());
}
class VigenereCipher{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
VigenereCipher1 vc = new VigenereCipher1();
System.out.println("Enter the plaintext: ");
String plaintext = sc.nextLine();
System.out.println("Enter the key: ");
String key = sc.nextLine();
String ciphertext = vc.encrypt(plaintext, key);
System.out.println("Encrypted text: " + ciphertext);
String decryptedText = vc.decrypt(ciphertext, key);
System.out.println("Decryted text: " + decryptedText);
sc.close();
}
}
output:
Enter the plaintext:
firewall
Enter the key:
abc
Encrypted text: FJTEXCLM
Decryted text: FIREWALL