Additive Cipher > Java Program
Additive Cipher > Java Program
Cryptography and System Security
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);
}
}
ENTER PLAIN TEXT:
prat pan
ENTER KEY VALUE:
4
AFTER ENCRYPTION:
tvex ter
AFTER DECRYPTION:
prat pan
BUILD SUCCESSFUL (total time: 45 seconds)
Comments
Post a Comment