This project was part of the JetBrains Academy Java course. The project was about implementing an Encryption program that uses either a simple Caesar Cipher (or) a Unicode Cipher depending on the arguments being passed to the program. The data to be Encrypted/Decrypted is just one line.
The different arguments being passed are:
- "-mode": enc = Encryption, dec = Decryption.
- "-alg": shift = caesar, unicode = unicode.
- "-in": Name of from to read input from.
- "-data": Data to be Encrypted/Decrypted.
- "-out": File name to which we should write Encrypted/Decrypted message.
- "-key": key to Encrypt/Decrypt data.
Additional things to note:
- If both "-data" and "-in" arguments are given, consider the "-data" argument.
- If no "-out" argument is present, simply print out the result.
- If no "-alg" argument is given, consider it to be shift.
Code:
Main.java
package encryptdecrypt;
public class Main {
public static void main(String[] args) {
String in = "", data = "", mode = "", alg = "shift", out = "";
int key = 0;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-alg":
alg = args[i + 1];
break;
case "-key":
key = Integer.parseInt(args[i + 1]);
break;
case "-data":
data = args[i + 1];
break;
case "-in":
in = args[i + 1];
break;
case "-mode":
mode = args[i + 1];
break;
case "-out":
out = args[i + 1];
break;
}
}
if (alg.equals("unicode")) {
EncryptDecrypt unicode = new UnicodeCipher();
unicode.doDeEncryption(mode, data, in, out, key);
} else {
EncryptDecrypt shift = new ShiftCipher();
shift.doDeEncryption(mode, data, in, out, key);
}
}
}
EncryptDecrypt.java
package encryptdecrypt;
import java.io.*;
public abstract class EncryptDecrypt {
protected StringBuilder plainText = new StringBuilder();
protected StringBuilder cipherText = new StringBuilder();
public void doDeEncryption (String mode, String data, String in, String out, int key) {
readFile(mode, data, in);
if (mode.equals("enc")) {
encryptPlainText(key);
} else {
decryptCipherText(key);
}
writeFile(mode, out);
}
public void readFile (String mode, String data, String in) {
String temp = "";
if (data.equals("")) {
try {
BufferedReader reader = new BufferedReader(new FileReader(in));
temp = reader.readLine();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
temp = data;
}
if (mode.equals("enc")) {
plainText = new StringBuilder(temp);
} else {
cipherText = new StringBuilder(temp);
}
}
public void writeFile (String mode, String out) {
String text = "";
if (mode.equals("enc")) {
text = cipherText.toString();
} else {
text = plainText.toString();
}
if (out.equals("")) {
System.out.println(text);
return;
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(out));
writer.write(text);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public abstract void encryptPlainText(int key);
public abstract void decryptCipherText(int key);
}
ShiftCipher.java
package encryptdecrypt;
public class ShiftCipher extends EncryptDecrypt {
private final int ROLLBACK = 26;
private boolean isValid (char c) {
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}
@Override
public void encryptPlainText(int key) {
for (int i = 0; i < plainText.length(); i++) {
if (isValid(plainText.charAt(i))) {
int temp = (plainText.charAt(i) - 'a' + key) % ROLLBACK;
temp += 'a';
cipherText.append((char)temp);
} else {
cipherText.append(plainText.charAt(i));
}
}
}
@Override
public void decryptCipherText(int key) {
for (int i = 0; i < cipherText.length(); i++) {
if (isValid(cipherText.charAt(i))) {
int temp = (cipherText.charAt(i) - 'a' - key + ROLLBACK) % ROLLBACK;
temp += 'a';
plainText.append((char)temp);
} else {
plainText.append(cipherText.charAt(i));
}
}
}
}
UnicodeCipher:
package encryptdecrypt;
public class UnicodeCipher extends EncryptDecrypt {
@Override
public void encryptPlainText(int key) {
for (int i = 0; i < plainText.length(); i++) {
int encryptedChar = (plainText.charAt(i) + key);
cipherText.append((char)encryptedChar);
}
}
@Override
public void decryptCipherText(int key) {
for (int i = 0; i < cipherText.length(); i++) {
int decryptedChar = (cipherText.charAt(i) - key);
plainText.append((char)decryptedChar);
}
}
}
The Unicode cipher is just adding the key to the codepoint of the character. The shift/Caesar Cipher must only encrypt the English letters.
Is my OOP structure good? Is there a better way to approach this using a design pattern?