Created
January 30, 2019 16:40
-
-
Save landjd19/26cae157060f08d4ce3a90ca19471e98 to your computer and use it in GitHub Desktop.
CaesarCipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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