I came along these two ciphers in my cryptography book and though I'd implement them just for fun. After that I went on developing a cipher based on these two that minimize the weakness of each cipher alone. I would appreciate your expert opinions on it and if it would be possible to crypt-analyse.
import java.util.*;
import java.io.*;
class CaesarCipher{
static String encrypt(String s, int key){
List<Character> arr = Arrays.asList('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');
StringBuilder str = new StringBuilder();
int index;
for(int i = 0; i< s.length(); i++)
{
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index = arr.indexOf(Character.toUpperCase(s.charAt(i)));
str.append(arr.get(Math.abs(index + key) % arr.size()));
}
}
return str.toString();
}
}
class VigenereCaesar{
static String encrypt(String s, String k, int h){
List<Character> arr = Arrays.asList('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',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
for(int i = 0; i< s.length(); i++)
{
if(i % k.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(s.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + index2) % arr.size()));
}
}
return str.toString();
}
static String encryptFile(BufferedReader buff, String k,int h) throws IOException{
List<Character> arr = Arrays.asList('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',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
String input = buff.readLine();
int count = 0;
while(input != null){
for(int i = 0; i< input.length(); i++)
{
if(count % k.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(input.charAt(i))) == false)
str.append(input.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(input.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + index2) % arr.size()));
}
count++;
}
str.append("\n");
input = buff.readLine();
}
return str.toString();
}
static String decrypt(String s, String k,int h){
List<Character> arr = Arrays.asList('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',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
for(int i = 0; i < s.length(); i++)
{
if(i % key.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(s.charAt(i))) == false)
str.append(s.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(s.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + (arr.size() - index2) )% arr.size()));
System.out.println(s.charAt(i) + " ---> " + arr.get((index1 + (arr.size() - index2) )% arr.size()));
}
}
return str.toString();
}
static String decryptFile(BufferedReader buff, String k,int h) throws IOException{
List<Character> arr = Arrays.asList('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',' ','0','1','2','3','4','5','6','7','8','9');
Random rand = new Random(h);
StringBuilder str = new StringBuilder();
int index1, index2;
int hash = h;
String key = k;
String input = buff.readLine();
int count = 0;
while(input!= null){
for(int i = 0; i < input.length(); i++)
{
if(count % key.length() == 0){
key = CaesarCipher.encrypt(key,++hash);
Collections.shuffle(arr,rand);
}
if(arr.contains(Character.toUpperCase(input.charAt(i))) == false)
str.append(input.charAt(i));
else{
index1 = arr.indexOf(Character.toUpperCase(input.charAt(i)));
index2 = arr.indexOf(Character.toUpperCase(key.charAt(i % key.length())));
str.append(arr.get((index1 + (arr.size() - index2) )% arr.size()));
}
count++;
}
str.append("\n");
input = buff.readLine();
}
return str.toString();
}
}
public class MyCipher{
public static void main(String[] args) throws IOException{
if(args.length == 2){
try{
BufferedReader fileBuff = new BufferedReader(new FileReader(args[0]));
if(!args[1].equals("encrypt") && !args[1].equals("decrypt")){
System.out.println("Error Usage:\n- Encryption: java MyCipher fileName encrypt\n- Decryption: java MyCipher fileName decrypt");
System.exit(1);
}
if(args[1].equals("encrypt")){
System.out.println("Enter encryption key (The longer and more random, the harder it gets to decrypt!) :");
String key; int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.println("Encrypting file...");
String out = VigenereCaesar.encryptFile(fileBuff,key,hash);
try{
PrintWriter writer = new PrintWriter(args[0] + " - Encrypted");
writer.write(out);
writer.close();
}catch(IOException exc){
System.out.println("Error: Could not write to file." + exc.getMessage());
System.exit(1);
}
System.out.println("File encryption complete.");
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: [email protected] >\n");
System.exit(0);
}
else if(args[1].equals("decrypt")){
System.out.println("Enter decryption key:");
String key;
int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.println("Decrypting file...");
String out = VigenereCaesar.decryptFile(fileBuff,key,hash);
try{
PrintWriter writer = new PrintWriter(args[0] + "-Decrypted");
writer.write(out);
writer.close();
}catch(IOException exc){
System.out.println("Error: Could not write to file." + exc.getMessage());
System.exit(1);
}
System.out.println("File decryption complete.");
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: [email protected] >\n");
System.exit(0);
}
}catch(IOException exc){
System.out.println("Error: Cannot procces file. " + exc.getMessage());
System.exit(1);
}
}
if(args.length != 1){
System.out.println("Error Usage:\n- Encryption: java MyCipher encrypt\n- Decryption: java MyCipher decrypt");
System.exit(1);
}
if(args[0].equals("encrypt")){
System.out.println("Enter encryption key (The longer and more random, the harder it gets to decrypt!) :");
String key; int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.print("Plaintext: ");
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
String out = VigenereCaesar.encrypt(s,key,hash);
System.out.println("-----------\nCiphertext: " + out);
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: [email protected] >\n");
System.exit(0);
}
else if(args[0].equals("decrypt")){
System.out.println("Enter decryption key:");
String key;
int hash = 0;
key = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Enter hash:");
try{
hash = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(NumberFormatException e)
{
System.out.println("Hash must be a whole number.");
System.exit(1);
}
System.out.print("Ciphertext: ");
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
String out = VigenereCaesar.decrypt(s,key,hash);
System.out.println("\n-----------\nPlaintext: " + out);
System.out.println("\n--------------------------------------------------------------------");
System.out.println("< Created by Issa Khoury. I appreciate comments at: [email protected] >\n");
System.exit(0);
}
else{
System.out.println("Error Usage:\n- Encryption: java MyCipher encrypt\n- Decryption: java MyCipher decrypt");
System.exit(1);
}
}
}