Java program to encipher and decipher text using Caesar Cipher.
Text: ABCDEFGHIJKLMNOPQRSTUVWXYZ Enciphered: XYZABCDEFGHIJKLMNOPQRSTUVW Deciphered: ABCDEFGHIJKLMNOPQRSTUVWXYZ
// ? 2019 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Main { public static void main(String[] args) { String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; final int shift = 23; String encipheredText = encipher(text, shift); System.out.println("Text: " + text); System.out.println("Enciphered: " + encipheredText); System.out.println("Deciphered: " + decipher(encipheredText, shift)); } private static String encipher(String text, int shift){ String result = ""; text = text.toUpperCase(); for(int i = 0; i < text.length(); ++i){ result += (char)((((text.charAt(i) - 'A') + shift) % 26) + 'A'); } return result; } private static String decipher(String text, int shift){ return encipher(text, 26 - shift); } }
Java Caesar Cipher