Cryptography and network security
Cryptography and network security
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");
}
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();
}
}
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", "");
}
playFairCipher.generateKeyMatrix(key);
String ciphertext = playFairCipher.encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
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 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
}
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();
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