Skip to content

Instantly share code, notes, and snippets.

@mackenly
Created January 29, 2023 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackenly/48179ce923321d7b8f4f9373978e4946 to your computer and use it in GitHub Desktop.
Save mackenly/48179ce923321d7b8f4f9373978e4946 to your computer and use it in GitHub Desktop.
Java Ceaser Cipher
/**
* -------------------------------------------
* File name: Cypher.java
* Project Name: Ceaser Cipher
* Created By: Mackenly Jones
* Date: 4/12/22
* -------------------------------------------
*/
package CeaserCipher;
/**
* Class Name: Cypher
* Purpose: Defines a Caesar cypher class
* Date: Apr 12, 2022
*/
public class Cypher {
// Instance Variables
// Cypher key:
private int cypher;
// Parameterized Constructor
public Cypher(int cypher) {
this.cypher = cypher;
}
// Getters
public int getCypherKey() {
return this.cypher;
}
// Encrypt Method
/**
* doEncryption
* Encrypts a string using the cypher key
* (doesn't support non-alphabetical values)
* (only supports the lower or upper case English alphabet)
* (unsupported characters are ignored)
* @param s String to be encrypted
* @return String encrypted
*/
public String doEncryption(String s) {
String s1 = "";
// Loop through the string and perform the encryption
for (int i = 0; i < s.length(); i++) {
// Grab the character
char c = s.charAt(i);
// Find out if it's uppercase or lowercase
// Chars represent numbers in ASCII, so we can do math on them
// By offsetting the character by the cypher we can effectively shift the character
if (Character.isUpperCase(c)) {
c = (char) (c + this.cypher);
// If we go past 'Z' we need to wrap around to 'A'
if (c > 'Z') {
// There are 26 letters in the English alphabet
c = (char) (c - 26);
}
} else if (Character.isLowerCase(c)) {
c = (char) (c + this.cypher);
// If we go past 'z' we need to wrap around to 'a'
if (c > 'z') {
// There are 26 letters in the English alphabet
c = (char) (c - 26);
}
}
// Append the result to the output string
s1 += c;
}
return s1;
}
// Decryption Method
/**
* doDecryption
* Decrypts a string using the cypher key
* (doesn't support non-alphabetical values)
* (only supports the lower or upper case English alphabet)
* (unsupported characters are ignored)
* @param s String to be decrypted
* @return String decrypted
*/
public String doDecryption(String s) {
String s2 = "";
// Loop through the string and perform the decryption
for (int i = 0; i < s.length(); i++) {
// Grab the character
char c = s.charAt(i);
// Find out if it's uppercase or lowercase
// Chars represent numbers in ASCII, so we can do math on them
// By offsetting the character by the cypher we can effectively shift the character using numbers
if (Character.isUpperCase(c)) {
c = (char) (c - this.cypher);
// If we go past 'A' we need to wrap around to 'Z'
if (c < 'A') {
// There are 26 letters in the English alphabet
c = (char) (c + 26);
}
} else if (Character.isLowerCase(c)) {
c = (char) (c - this.cypher);
// If we go past 'a' we need to wrap around to 'z'
if (c < 'a') {
// There are 26 letters in the English alphabet
c = (char) (c + 26);
}
}
// Append the result to the output string
s2 += c;
}
return s2;
}
}
/**
* -------------------------------------------
* File name: Demo.java
* Project Name: Ceaser Cipher
* Created By: Mackenly Jones
* Date: 4/12/22
* -------------------------------------------
*/
package CeaserCipher;
import java.util.Scanner;
/**
* Class Name: MyCypherDemo
* Purpose: Test the MyCypher class
* Date: Apr 12, 2022
*/
public class CypherDemo {
public static void main(String[] args) {
//start code here:
// Declare variables
String input = "", encrypted = "", decrypted = "";
// Print welcome message
printWelcomeSignature("Mackenly Jones", "Problem 2", "CypherDemo");
// Ask user for input
input = inputHelper("Enter the input string: ");
// Create a new cypher object
Cypher cypher = new Cypher(13);
// Print the encrypted string
encrypted = cypher.doEncryption(input);
System.out.println("Print the encrypted string: " + encrypted);
// Print the decrypted string
decrypted = cypher.doDecryption(encrypted);
System.out.println("\nPrint the decrypted string: " + decrypted);
} //end main
/**
* Method Name: printWelcomeSignature
* Method Description: Takes some arguments and formats them into a welcome message and prints it
* @param programmerName programmer to show up in message
* @param problemName course name to show up in message
* @param fileName project name to show in message
* @author Mackenly Jones
*/
public static void printWelcomeSignature(String programmerName, String problemName, String fileName) {
int neededLen = 2 + programmerName.length() + 3 + problemName.length() + 3 + fileName.length() + 2;
String topBorder = "/";
String bottomBorder = "\\";
for (int i = 0; i < neededLen; i++) {
topBorder += "-";
bottomBorder += "-";
}
topBorder = topBorder + "\\";
bottomBorder = bottomBorder + "/";
System.out.println(topBorder + "\n" + "| " + programmerName + " - " + problemName + " - " + fileName + " |" + "\n" + bottomBorder + "\n\n");
}
/**
* inputHelper
* Shows user a prompt and gets a string from the user
* @param prompt - prompt to display to user
* @return - returns the user's input
*/
public static String inputHelper(String prompt) {
System.out.print(prompt);
Scanner input = new Scanner(System.in);
return input.nextLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment