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
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close