Skip to main content
Tweeted twitter.com/StackCodeReview/status/1598285806753415168
added 53 characters in body
Source Link

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.

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.

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.

Minor corrections (the Unicode cipher works with all codepoints, not just ASCII subset)
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Implementing an Encryption An encryption/decryption program with two different algorithms, Caesar Cipher and a Unicode Cipher

The unicodeUnicode cipher is just adding the key to the ASCII valuecodepoint of the character. The shift/Caesar Cipher must only encrypt the English alphabetsletters.

Implementing an Encryption program with two different algorithms, Caesar Cipher and a Unicode Cipher

The unicode cipher is just adding the key to the ASCII value of the character. The shift/Caesar Cipher must only encrypt the English alphabets.

An encryption/decryption program with two different algorithms, Caesar Cipher and a Unicode Cipher

The Unicode cipher is just adding the key to the codepoint of the character. The shift/Caesar Cipher must only encrypt the English letters.

Source Link

Implementing an Encryption program with two different algorithms, Caesar Cipher and a Unicode Cipher

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 different arguments being passed are:

  1. "-mode": enc = Encryption, dec = Decryption.
  2. "-alg": shift = caesar, unicode = unicode.
  3. "-in": Name of from to read input from.
  4. "-data": Data to be Encrypted/Decrypted.
  5. "-out": File name to which we should write Encrypted/Decrypted message.
  6. "-key": key to Encrypt/Decrypt data.

Additional things to note:

  1. If both "-data" and "-in" arguments are given, consider the "-data" argument.
  2. If no "-out" argument is present, simply print out the result.
  3. 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 ASCII value of the character. The shift/Caesar Cipher must only encrypt the English alphabets.

Is my OOP structure good? Is there a better way to approach this using a design pattern?