Right now I have the following code for a Caesar Cipher, but I run into an issue when I try encrypting text with a very large number. For example 1000.
static String encrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder(plaintext);
for (int i = 0; i < ciphertext.length(); i++) {
ciphertext.setCharAt(i, encrypt(ciphertext.charAt(i)));
}
return ciphertext.toString();
}
static String decrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder(plaintext);
for (int i = 0; i < ciphertext.length(); i++) {
ciphertext.setCharAt(i, decrypt(ciphertext.charAt(i)));
}
return ciphertext.toString();
}
static char encrypt(char c) {
return (char) ('!' + (c - '!' + 1000) % ('~' - '!' + 1));
}
static char decrypt(char c) {
return (char) ('!' + (c - '!' - 1000) % ('~' - '!' + 1));
}
Lets say I input "abc123" into the encryption, using 1000 as my key, I get a bunch of unknown characters. Keep in mind I don't want it to just cycle through a-z but symbols too, using ASCII codes.
Any help would be great!