0% found this document useful (0 votes)
10 views5 pages

Cns Exp1

The document contains two Java programs that implement encryption and decryption using a substitution cipher. The first program reads plaintext and a key from the user, encrypts the plaintext, and then decrypts it back to the original text. The second program also performs similar functions but uses a different structure and method for encryption and decryption.

Uploaded by

Haresh Lad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Cns Exp1

The document contains two Java programs that implement encryption and decryption using a substitution cipher. The first program reads plaintext and a key from the user, encrypts the plaintext, and then decrypts it back to the original text. The second program also performs similar functions but uses a different structure and method for encryption and decryption.

Uploaded by

Haresh Lad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM:

import java.io.*;

public class Adder {

static int a[] = new int[26];

static char c[] = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z'};

//static char c[] = new char[26];

static String str1 = "", s = "";

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

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

a[i] = i;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.println("ENTER PLAINTEXT: ");

s = br.readLine();

System.out.println("ENTER KEY VALUE: ");

int k = Integer.parseInt(br.readLine());

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

int j;

if (s.charAt(i) == ' ') {

i++;

str1 = str1.concat(" ");

for (j = 0; j < 26; j++) {

if (s.charAt(i) == c[j]) {

break;

}
}

j = (a[j] + k) % 26;

str1 = str1.concat(c[j] + "");

System.out.println("AFTER ENCRYPTION: ");

System.out.println(str1);

s = "";

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

int j;

if (str1.charAt(i) == ' ') {

i++;

s = s.concat(" ");

for (j = 0; j < 26; j++) {

if (str1.charAt(i) == c[j]) {

break;

j = (a[j] - k) % 26;

if (j < 0) {

j = 26 + j;

s = s.concat(c[j] + "");

System.out.println("AFTER DECRYPTION: ");

System.out.println(s);

}}
OUTPUT:

PROGRAM:

import java.io.*;

import java.util.*;

class Subsitution

public static final String str="abcdefghijklmnopqrstuvwxyz";

public static String encrypt(String plaint,int key)

plaint=plaint.toLowerCase();

String ciphert="";

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

int charpos=str.indexOf(plaint.charAt(i));

int keyval=(charpos+key)%26;

char replaceval=str.charAt(keyval);
ciphert=ciphert+replaceval;

return ciphert;

public static String decrypt(String ciphert,int key)

ciphert=ciphert.toLowerCase();

String plaint="";

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

int charpos=str.indexOf(ciphert.charAt(i));

int keyval=(charpos-key)%26;

if(keyval<0)

keyval=str.length()+keyval;

char replaceval=str.charAt(keyval);

plaint=plaint+replaceval;

return plaint;

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

BufferedReader kr=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a plain text");

String msg=br.readLine();

System.out.println("Enter a key");
String key=br.readLine();

int k=Integer.parseInt(key);

System.out.println("Encrypted Text");

System.out.println(encrypt(msg,k));

System.out.println("The decrypted text");

System.out.println(decrypt(encrypt(msg,k),k));

OUTPUT:

You might also like