Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
78 views6 pages

Encryption Techniques

The document describes a Java program that implements encryption and decryption using three cipher algorithms: Caesar cipher, substitution cipher, and Hill cipher. The program includes methods for encrypting and decrypting strings using each cipher, along with code to get user input, perform the encryption/decryption, and output the results.

Uploaded by

Sohana Amreen
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)
78 views6 pages

Encryption Techniques

The document describes a Java program that implements encryption and decryption using three cipher algorithms: Caesar cipher, substitution cipher, and Hill cipher. The program includes methods for encrypting and decrypting strings using each cipher, along with code to get user input, perform the encryption/decryption, and output the results.

Uploaded by

Sohana Amreen
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/ 6

3.

Write a Java program to perform encryption and decryption using the following algorithms: a) Ceaser
Cipher b) Substitution Cipher c) Hill Cipher

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 {

// TODO code application logic here

System.out.print("Enter any String: ");

String str = br.readLine();

System.out.print("\nEnter the Key: ");

int key = sc.nextInt();

String encrypted = encrypt(str, key);

System.out.println("\nEncrypted String is: " +encrypted);

String decrypted = decrypt(encrypted, key);

System.out.println("\nDecrypted String is: " +decrypted);

System.out.println("\n");

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;

}
B)substitution

import java.io.*;

import java.util.*;

public class SubstitutionCipher {

static Scanner sc = new Scanner(System.in);

static BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

// TODO code application logic here

String a = "abcdefghijklmnopqrstuvwxyz";

String b = "zyxwvutsrqponmlkjihgfedcba";

System.out.print("Enter any string: ");

String str = br.readLine();

String decrypt = "";

char c;

for(int i=0;i<str.length();i++)

c = str.charAt(i);

int j = a.indexOf(c);

decrypt = decrypt+b.charAt(j);

System.out.println("The encrypted data is: " +decrypt);

}
c)Hill Cipher

import java.io.*;

import java.util.*;

import java.io.*;

public class HillCipher {

static float[][] decrypt = new float[3][1];

static float[][] a = new float[3][3];

static float[][] b = new float[3][3];

static float[][] mes = new float[3][1];

static float[][] res = new float[3][1];

static BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws IOException {

// TODO code application logic heregetkeymes();

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

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

for(int k=0;k<3;k++) {

res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }

System.out.print("\nEncrypted string is : ");

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

System.out.print((char)(res[i][0]%26+97));

res[i][0]=res[i][0];

inverse();

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

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

for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }

System.out.print("\nDecrypted string is : ");

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

System.out.print((char)(decrypt[i][0]%26+97));

System.out.print("\n");

public static void getkeymes() throws IOException {

System.out.println("Enter 3x3 matrix for key (It should be inversible): ");

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

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

a[i][j] = sc.nextFloat();

System.out.print("\nEnter a 3 letter string: ");

String msg = br.readLine();

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

mes[i][0] = msg.charAt(i)-97;

public static void inverse() {

float p,q;

float[][] c = a;

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

for(int j=0;j<3;j++) {

//a[i][j]=sc.nextFloat();

if(i==j)

b[i][j]=1;

else b[i][j]=0;

for(int k=0;k<3;k++) {

for(int i=0;i<3;i++) {
p = c[i][k];

q = c[k][k];

for(int j=0;j<3;j++) {

if(i!=k) {

c[i][j] = c[i][j]*q-p*c[k][j];

b[i][j] = b[i][j]*q-p*b[k][j];

}}}}

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

for(int j=0;j<3;j++) {

b[i][j] = b[i][j]/c[i][i]; }

System.out.println("");

System.out.println("\nInverse Matrix is : ");

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

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

System.out.print(b[i][j] + " ");

System.out.print("\n"); }

}}

You might also like