Created
February 4, 2019 13:19
-
-
Save landjd19/6f5c7b26258e9ac57488282210ea749e to your computer and use it in GitHub Desktop.
CaesarCipher2
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("Press 1 to encrypt, press 2 to decrypt with a key, press 3 to brute force."); | |
key = sc.nextInt(); | |
if(key == 1){ | |
System.out.println("Please enter the message to be encrypted."); | |
sc.nextLine(); | |
message = sc.nextLine(); | |
System.out.println("How much would you like to shift the message?"); | |
key = sc.nextInt(); | |
encryptMessage(message, key); | |
}else if(key == 2){ | |
System.out.println("Please enter the message to be decrypted."); | |
sc.nextLine(); | |
message = sc.nextLine(); | |
System.out.println("How much did they shift their message?"); | |
key = -1 * sc.nextInt(); | |
encryptMessage(message, key); | |
}else if(key == 3){ | |
System.out.println("Please enter the encrypted text."); | |
sc.nextLine(); | |
message = sc.nextLine(); | |
for(int i = 1; i < 26; i ++){ | |
encryptMessage(message, -i); | |
} | |
} | |
} | |
//A = 65 | |
//a = 97 | |
public void encryptMessage(String input, int key){ | |
for(int i = 0; i < input.length(); i++){ | |
c = input.charAt(i); | |
encoded += (char)((c + key) % 128); | |
} | |
System.out.println(key + " " + encoded); | |
encoded = ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment