Caesar Cipher implementation in Java..

.. with some maven magic going on..

This is my directory layout..

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>caesarcipher</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>caesarcipher</name>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

CaesarCipher.java
package biz.tugay.caesarcipher;
 
import java.util.Locale;
 
/*
    Encyrpts a clear text using Caeser Cipher (https://en.wikipedia.org/wiki/Caesar_cipher)
    with given shift amount.
 
    Provided shift amount (i.e. key) must be a positive integer less than 26.
    Only English alphabet is supported and encyrpted text will be in uppercase.
 
    Shift amount 0 will return the same clear text.
 */
public final class CaesarCipher {
 
    private final String clearText;
    private final int key;
 
    public CaesarCipher(final String clearText, final int key) {
        if (clearText == null) {
            throw new UnsupportedOperationException("Clear text to be encrypted can not be null!");
        }
 
        if (key < 0 || key > 26) {
            throw new UnsupportedOperationException("Key must be between 0 and 26");
        }
 
        this.clearText = clearText;
        this.key = key;
    }
 
    public String encryptText() {
        final StringBuilder cipherTextBuilder = new StringBuilder();
 
        final String clearTextUpperCase = clearText.toUpperCase(Locale.US);
        final char[] clearTextUpperCaseCharArray = clearTextUpperCase.toCharArray();
 
        for (final char c : clearTextUpperCaseCharArray) {
            if (c < 65 || c > 90) { // If the character is not between A .. Z, append white space.
                cipherTextBuilder.append(" ");
                continue;
            }
            final Character encryptedCharacter = encryptCharacter(c);
            cipherTextBuilder.append(encryptedCharacter);
        }
 
        return cipherTextBuilder.toString();
    }
 
    private Character encryptCharacter(final char c) {
        final int initialShift = c + key;
        final int finalShift;
 
        if (initialShift > 90) {
            // This is the case where we go beyond Z, we must cycle back to A.
            finalShift = (initialShift % 90) + 64;
        } else {
            // We are in the boundries so no need to cycle..
            finalShift = initialShift;
        }
 
        return (char) finalShift;
    }
}

CaesarCipherTest.java
package biz.tugay.caesarcipher;
 
import org.junit.Assert;
import org.junit.Test;
 
public class CaesarCipherTest {
 
    @Test
    public void shouldReturnBCDForClearTextABCAndKey1() {
        final String clearText = "abc";
        final CaesarCipher caesarCipher = new CaesarCipher(clearText, 1);
        final String encryptedText = caesarCipher.encryptText();
        Assert.assertTrue(encryptedText.equals("BCD"));
    }
 
    @Test
    public void shouldReturnAForZAndKey1() {
        final String clearText = "Z";
        final CaesarCipher caesarCipher = new CaesarCipher(clearText, 1);
        final String encryptedText = caesarCipher.encryptText();
        Assert.assertTrue(encryptedText.equals("A"));
    }
}

App.java
package biz.tugay;
 
import biz.tugay.caesarcipher.CaesarCipher;
 
public class App {
    public static void main(String[] args) {
        final String plainText = args[0];
        final int shiftAmount = Integer.parseInt(args[1]);
        final CaesarCipher caesarCipher = new CaesarCipher(plainText, shiftAmount);
        System.out.println(caesarCipher.encryptText());
    }
}

In Action

Build

Run

Any suggestions? Feel free to email me!