exp 3 cns

*AIM:* Write a Java program to perform encryption and decryption using Ceaser Cipher algorithm:
*Caeser Cipher: Source Code*
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CeaserCipher {
 static Scanner sc=new Scanner(System.in);
 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 public static void main(String[] args) throws IOException {
  System.out.print("Enter any String: ");
  String str = br.readLine();
  System.out.print("Enter the Key: ");
  int key = sc.nextInt();
  String encrypted = encrypt(str, key);
  System.out.println("Encrypted String is: " +encrypted);
  String decrypted = decrypt(encrypted, key);
  System.out.println("Decrypted String is: " +decrypted);
 }
 public static String encrypt(String str, int key) {
  String encrypted = "";
  for (int i = 0; i < str.length(); i++) {
   int c = str.charAt(i);
   if (Character.isUpperCase(c)) {
    c = c + (key % 26);
    if (c > 'Z')
     c = c - 26;
   }
   else if (Character.isLowerCase(c)) {
    c = c + (key % 26);
    if (c > 'z')
     c = c - 26;
   }
   encrypted += (char) c;
  }
  return encrypted;
 }
 public static String decrypt(String str, int key) {
  String decrypted = "";
  for(int i = 0; i < str.length(); i++) {
   int c = str.charAt(i);
   if (Character.isUpperCase(c)) {
    c = c - (key % 26);
    if (c < 'A')
     c = c + 26;
   }
   else if (Character.isLowerCase(c)) {
    c = c - (key % 26);
    if (c < 'a')
     c = c + 26;
   }
   decrypted += (char) c;
  }
  return decrypted;
 }
}
*Output:*
*Enter any String:* Are you feeling better now
*Enter the Key:* 8
*Encrypted String is:* Izm gwc nmmtqvo jmbbmz vwe
*Decrypted String is:* Are you feeling better now








dear students have to do Ceaser Cipher program in CNS lab

*AIM*: Write a Java program to perform encryption and decryption using Hill Cipher
 algorithms:

*Hill Cipher: Source Code*
// Java code to implement Hill Cipher

public class Hill
{
 
// Following function generates the
// key matrix for the key string

public 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;

            k++;

        }

    }
}
 
// Following function encrypts the message

public static void encrypt(int cipherMatrix[][],

            int keyMatrix[][], 

            int messageVector[][])
{

    int x, i, j;

    for (i = 0; i < 3; i++) 

    {

        for (j = 0; j < 1; j++)

        {

            cipherMatrix[i][j] = 0;

         

            for (x = 0; x < 3; x++)

            {

                cipherMatrix[i][j] += 

                    keyMatrix[i][x] * messageVector[x][j];

            }

         

            cipherMatrix[i][j] = cipherMatrix[i][j] % 26;

        }

    }
}
 
// Function to implement Hill Cipher

public static void HillCipher(String message, String key)
{

    // Get key matrix from the key string

    int [][]keyMatrix = new int[3][3];

    getKeyMatrix(key, keyMatrix);
 

    int [][]messageVector = new int[3][1];
 

    // Generate vector for the message

    for (int i = 0; i < 3; i++)

        messageVector[i][0] = (message.charAt(i)) % 65;
 

    int [][]cipherMatrix = new int[3][1];
 

    // Following function generates

    // the encrypted vector

    encrypt(cipherMatrix, keyMatrix, messageVector);
 

    String CipherText="";
 

    // Generate the encrypted text from 

    // the encrypted vector

    for (int i = 0; i < 3; i++)

        CipherText += (char)(cipherMatrix[i][0] + 65);
 

    // Finally print the ciphertext

    System.out.print(" Ciphertext:" + CipherText);
}
 
// Driver code

public static void main(String[] args) 
{
    // Get the message to be encrypted
    String message = "ACT";
 

    // Get the key

    String key = "GYBNQKURP";
    HillCipher(message, key);

    }
}

*Output:*
*Ciphertext:* POH