Here is an attempt I made at a simple Substitution based cipher machine, it shifts input characters by an amount then returns an unreadable string which can then be decrypted back to its original self:
package Pack;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* Start of CiphermachineMK3.
**/
public class CipherMachineMK3 {
/**
* Encryption character map before Encryption.
*/
private List<String> encryptMappingfrom = Arrays.asList(":", "/", "?", "#", ".", " ");
/**
* Encryption character map after Encryption.
*/
private List<String> encryptMappingto = Arrays.asList("!", "-", "+", ",", "]", " ");
/**
* Decryption character map before Decryption.
*/
private List<String> decryptMappingfrom = Arrays.asList("!", "-", "+", ",", "]", " ");
/**
* Decryption character map after Decryption.
*/
private List<String> decryptMappingto = Arrays.asList(":", "/", "?", "#", ".", " ");
/**
* HashMap to switch inputted character with encryption value.
*/
private static HashMap<String, String> encryptMapping = new HashMap<>();
/**
* HashMap to switch inputted character with decryption value.
*/
private static HashMap<String, String> decryptMapping = new HashMap<>();
/**
* Amount of shift required to loop the characters of the alphabet from A-Z and vice
* versa.
*/
private static final int SHIFT_ROTATE_VALUE = 26;
/**
* Encoding shift amount.
*/
private static final int NUMBER_TO_SHIFT_DIGITS = 3;
/**
* Shift amount required for decoding challenge.
*/
private static final int SECRET_DECODE_VALUE = 18;
/**
* Storage for lower case alphabet variables.
*/
private static final String ALPHABET_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
/**
* Storage for upper case alphabet variables.
*/
private static final String ALPHABET_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* Mapping for the encoding of specific characters.
*
* @param encryptMappingfrom
* Characters to the be encrypted.
* @param encryptMappingto
* Characters encrypted to.
* @return Encryption map
*/
static HashMap<String, String> encryptFixedmappings(final List<String> encryptMappingfrom,
final List<String> encryptMappingto) {
for (int i = 0; i < encryptMappingfrom.size(); i++) {
encryptMapping.put(encryptMappingfrom.get(i), encryptMappingto.get(i));
}
return encryptMapping;
}
/**
* Mapping for the decoding of specific characters.
*
* @param decryptMappingfrom
* Characters to be decrypted.
* @param decryptMappingto
* Characters decrypted to.
* @return Decryption map
*/
static HashMap<String, String> decryptFixedmappings(final List<String> decryptMappingfrom,
final List<String> decryptMappingto) {
for (int i = 0; i < decryptMappingfrom.size(); i++) {
decryptMapping.put(decryptMappingfrom.get(i), decryptMappingto.get(i));
}
return decryptMapping;
}
/**
* Start of Encrypt method.
*
* @param plainText
* Input of clear readable text.
* @param shiftKey
* Amount to shift text by when encoding.
* @return cipherText The final encoded result.
*/
public String encrypt(final String plainText, final int shiftKey) {
String cipherText = "";
String charasString = "";
encryptMapping = CipherMachineMK3.encryptFixedmappings(encryptMappingfrom, encryptMappingto);
if (plainText == null) {
return null;
}
// Loop begins
for (int i = 0; i < plainText.length(); i++) {
// Checks if character is alphabetic
if (Character.isAlphabetic(plainText.charAt(i))) {
// Performs tasks if character is Lower-case
if (Character.isLowerCase(plainText.charAt(i))) {
int lowerCharPosition = ALPHABET_LOWERCASE.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + lowerCharPosition) % SHIFT_ROTATE_VALUE;
if (keyVal < 0) {
keyVal = ALPHABET_LOWERCASE.length() + keyVal;
}
char sortEncryptLowerCaseASCII = (char) keyVal;
char replaceVal = ALPHABET_LOWERCASE.charAt(sortEncryptLowerCaseASCII);
cipherText += replaceVal;
}
// Performs tasks if character is Upper-case
if (Character.isUpperCase(plainText.charAt(i))) {
int upperCharPosition = ALPHABET_UPPERCASE.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + upperCharPosition) % SHIFT_ROTATE_VALUE;
if (keyVal < 0) {
keyVal = ALPHABET_UPPERCASE.length() + keyVal;
}
char sortEncryptUpperCaseASCII = (char) keyVal;
char replaceVal = ALPHABET_UPPERCASE.charAt(sortEncryptUpperCaseASCII);
cipherText += replaceVal;
}
} else {
// Begins symbol checker loop
if (Character.isWhitespace(plainText.charAt(i))) {
cipherText += plainText.charAt(i);
continue;
} else {
charasString = String.valueOf(plainText.charAt(i));
if (!encryptMapping.containsKey(charasString)) {
// Default value for unrecognised characters
cipherText += "_";
} else {
cipherText += encryptMapping.get(charasString);
}
}
// Checks if character is alphabetic
}
}
return cipherText;
}
/**
* Start of Decrypt method.
*
* @param cipherText
* Input of encoded text.
* @param shiftKey
* Amount to shift text by when decoding.
* @return plainText The final decoded result.
*/
public String decrypt(final String cipherText, final int shiftKey) {
String plainText = "";
String charasString = "";
decryptMapping = CipherMachineMK3.decryptFixedmappings(decryptMappingfrom, decryptMappingto);
if (cipherText == null) {
return null;
}
// Loop begins
for (int i = 0; i < cipherText.length(); i++) {
// Checks if character is alphabetic
if (Character.isAlphabetic(cipherText.charAt(i))) {
// Performs tasks if character is Lower-case
if (Character.isLowerCase(cipherText.charAt(i))) {
int charPosition = ALPHABET_LOWERCASE.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % SHIFT_ROTATE_VALUE;
if (keyVal < 0) {
keyVal = ALPHABET_LOWERCASE.length() + keyVal;
}
char sortDecryptLowerCaseASCII = (char) keyVal;
char replaceVal = ALPHABET_LOWERCASE.charAt(sortDecryptLowerCaseASCII);
plainText += replaceVal;
}
// Performs tasks if character is Upper-case
if (Character.isUpperCase(cipherText.charAt(i))) {
int charPosition = ALPHABET_UPPERCASE.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % SHIFT_ROTATE_VALUE;
if (keyVal < 0) {
keyVal = ALPHABET_UPPERCASE.length() + keyVal;
}
char sortDecryptUpperCaseASCII = (char) keyVal;
char replaceVal = ALPHABET_UPPERCASE.charAt(sortDecryptUpperCaseASCII);
plainText += replaceVal;
}
}
// Checks if character is non-alphabetic
if (!Character.isAlphabetic(cipherText.charAt(i))) {
// Begins symbol checker loop
if (Character.isWhitespace(cipherText.charAt(i))) {
plainText += cipherText.charAt(i);
continue;
} else {
charasString = String.valueOf(cipherText.charAt(i));
if (!decryptMapping.containsKey(charasString)) {
// Default value for unrecognised characters
plainText += "*";
}
plainText += decryptMapping.get(charasString);
}
}
}
return plainText;
}
/**
* Start of main method.
*
* @param args
* args
* @throws UnsupportedEncodingException Throws exception in case string to byte conversion fails.
*/
public static void main(final String[] args) throws UnsupportedEncodingException {
CipherMachineMK3 cipherMachine = new CipherMachineMK3();
System.out.println(cipherMachine.encrypt("Hello World", NUMBER_TO_SHIFT_DIGITS));
System.out.println(cipherMachine.encrypt("Well done: #Decoded am i right?", NUMBER_TO_SHIFT_DIGITS));
System.out.println(cipherMachine.decrypt("Khoor Zruog", NUMBER_TO_SHIFT_DIGITS));
System.out.println(cipherMachine.decrypt("Ugfyjslmdslagfk ]]] Lwdd fgtgvq lzwhskkogjv ak! ,Uzwvvsj",
SECRET_DECODE_VALUE));
}
}
Written in Eclipse. Any feedback regarding potential improvements or even just "Get rid of X and do X instead" would be greatly appreciated.