Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Created October 6, 2025 12:35
Show Gist options
  • Select an option

  • Save nsfyn55/d204c6f0b5ed0cccca27ea9fd7dc5892 to your computer and use it in GitHub Desktop.

Select an option

Save nsfyn55/d204c6f0b5ed0cccca27ea9fd7dc5892 to your computer and use it in GitHub Desktop.
AI Generated Ceasar Cipher Implementation
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* A simple and elegant Caesar cipher implementation that handles Unicode characters.
* Supports both encryption and decryption with command line interface.
*
* Usage:
* java CaesarCipher encrypt <key> <text>
* java CaesarCipher decrypt <key> <text>
* java CaesarCipher encrypt <key> --file <filepath>
* java CaesarCipher decrypt <key> --file <filepath>
*/
public class CaesarCipher {
/**
* Encrypts text using Caesar cipher with the given shift key.
*
* @param text The text to encrypt
* @param key The shift key (can be positive or negative)
* @return The encrypted text
*/
public static String encrypt(String text, int key) {
return shiftText(text, key);
}
/**
* Decrypts text using Caesar cipher with the given shift key.
*
* @param text The text to decrypt
* @param key The shift key (can be positive or negative)
* @return The decrypted text
*/
public static String decrypt(String text, int key) {
return shiftText(text, -key);
}
/**
* Core method that performs the character shifting for both encryption and decryption.
* Handles Unicode characters by working with code points.
*
* @param text The input text
* @param shift The shift amount
* @return The shifted text
*/
private static String shiftText(String text, int shift) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); ) {
int codePoint = text.codePointAt(i);
int shiftedCodePoint = shiftCodePoint(codePoint, shift);
result.appendCodePoint(shiftedCodePoint);
i += Character.charCount(codePoint);
}
return result.toString();
}
/**
* Shifts a single Unicode code point by the given amount.
* Preserves the character type (letter, digit, symbol, etc.).
*
* @param codePoint The Unicode code point to shift
* @param shift The shift amount
* @return The shifted code point
*/
private static int shiftCodePoint(int codePoint, int shift) {
// Handle different character types
if (Character.isLetter(codePoint)) {
return shiftLetter(codePoint, shift);
} else if (Character.isDigit(codePoint)) {
return shiftDigit(codePoint, shift);
} else {
// For non-alphanumeric characters, return as-is
return codePoint;
}
}
/**
* Shifts letters while preserving case and handling Unicode properly.
*
* @param codePoint The letter's code point
* @param shift The shift amount
* @return The shifted letter's code point
*/
private static int shiftLetter(int codePoint, int shift) {
int base;
int range;
if (Character.isUpperCase(codePoint)) {
base = 'A';
range = 26;
} else if (Character.isLowerCase(codePoint)) {
base = 'a';
range = 26;
} else {
// For other letter types (like accented letters), use Unicode blocks
return shiftUnicodeLetter(codePoint, shift);
}
int relativePosition = codePoint - base;
int shiftedPosition = (relativePosition + shift) % range;
if (shiftedPosition < 0) {
shiftedPosition += range;
}
return base + shiftedPosition;
}
/**
* Handles shifting for Unicode letters that don't fit standard ASCII ranges.
*
* @param codePoint The Unicode letter's code point
* @param shift The shift amount
* @return The shifted letter's code point
*/
private static int shiftUnicodeLetter(int codePoint, int shift) {
// For simplicity, we'll shift within the same Unicode block
// This is a basic implementation - more sophisticated handling could be added
return codePoint + shift;
}
/**
* Shifts digits (0-9) with wraparound.
*
* @param codePoint The digit's code point
* @param shift The shift amount
* @return The shifted digit's code point
*/
private static int shiftDigit(int codePoint, int shift) {
int relativePosition = codePoint - '0';
int shiftedPosition = (relativePosition + shift) % 10;
if (shiftedPosition < 0) {
shiftedPosition += 10;
}
return '0' + shiftedPosition;
}
/**
* Reads text from a file.
*
* @param filePath The path to the file
* @return The content of the file
* @throws IOException If the file cannot be read
*/
private static String readFromFile(String filePath) throws IOException {
Path path = Paths.get(filePath);
return Files.readString(path);
}
/**
* Writes text to a file.
*
* @param filePath The path to the file
* @param content The content to write
* @throws IOException If the file cannot be written
*/
private static void writeToFile(String filePath, String content) throws IOException {
Path path = Paths.get(filePath);
Files.writeString(path, content);
}
/**
* Prints usage information.
*/
private static void printUsage() {
System.out.println("Caesar Cipher - Encrypt and decrypt text using Unicode-aware Caesar cipher");
System.out.println();
System.out.println("Usage:");
System.out.println(" java CaesarCipher encrypt <key> <text>");
System.out.println(" java CaesarCipher decrypt <key> <text>");
System.out.println(" java CaesarCipher encrypt <key> --file <filepath>");
System.out.println(" java CaesarCipher decrypt <key> --file <filepath>");
System.out.println();
System.out.println("Examples:");
System.out.println(" java CaesarCipher encrypt 3 \"Hello World!\"");
System.out.println(" java CaesarCipher decrypt 3 \"Khoor Zruog!\"");
System.out.println(" java CaesarCipher encrypt 5 --file input.txt");
System.out.println(" java CaesarCipher decrypt 5 --file encrypted.txt");
System.out.println();
System.out.println("Notes:");
System.out.println(" - Key can be any integer (positive or negative)");
System.out.println(" - Letters are shifted within their case (A-Z, a-z)");
System.out.println(" - Digits are shifted within 0-9 range");
System.out.println(" - Non-alphanumeric characters are preserved");
System.out.println(" - Unicode characters are supported");
}
/**
* Main method that handles command line arguments and orchestrates the cipher operations.
*
* @param args Command line arguments: [encrypt|decrypt] <key> [text|--file <filepath>]
*/
public static void main(String[] args) {
if (args.length < 3) {
printUsage();
return;
}
String operation = args[0].toLowerCase();
if (!operation.equals("encrypt") && !operation.equals("decrypt")) {
System.err.println("Error: Operation must be 'encrypt' or 'decrypt'");
printUsage();
return;
}
int key;
try {
key = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.err.println("Error: Key must be a valid integer");
printUsage();
return;
}
String text;
boolean isFileInput = args.length >= 4 && args[2].equals("--file");
if (isFileInput) {
String filePath = args[3];
try {
text = readFromFile(filePath);
} catch (IOException e) {
System.err.println("Error: Cannot read file '" + filePath + "': " + e.getMessage());
return;
}
} else {
text = args[2];
}
String result;
if (operation.equals("encrypt")) {
result = encrypt(text, key);
} else {
result = decrypt(text, key);
}
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment