Skip to content

Instantly share code, notes, and snippets.

@landjd19
Created January 30, 2019 16:40
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 landjd19/26cae157060f08d4ce3a90ca19471e98 to your computer and use it in GitHub Desktop.
Save landjd19/26cae157060f08d4ce3a90ca19471e98 to your computer and use it in GitHub Desktop.
CaesarCipher
import java.util.*;
public class CaesarCipher
{
String message;
int key;
Scanner sc = new Scanner(System.in);
String encoded = "";
char c;
public CaesarCipher(){
System.out.println("Please enter the message to be encrypted.");
getMessage();
encryptMessage(message);
}
public void getMessage(){
message = sc.nextLine();
System.out.println("How much would you like to shift the message?");
key = sc.nextInt();
}
//A = 65
//a = 97
public void encryptMessage(String input){
for(int i = 0; i < input.length(); i++){
c = input.charAt(i);
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
c = (char)(c + key);
if((c > 'Z' && c < 'a') || c > 'z'){
c = (char)(c - 26);
}
encoded += c;
}else{
encoded += c;
}
}
System.out.println(encoded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment