caesar.java
22 linesjava
DOWNLOAD
1// Aim: Program to perform Caesar cipher (Caesar Cipher).
2public class caesar {
3    public static String encrypt(String text,int shift){
4        StringBuilder st = new StringBuilder();
5        for(char c : text.toCharArray()){
6            st.append((char)(((c-'A'+shift)%26)+'A'));
7        }
8        return st.toString();
9    }
10    public static String decrypt(String text,int shift){
11        return encrypt(text, 26-(shift%26));
12    }
13    public static void main(String[] args) {
14        String text = "CRYPTOGRAPHY";
15        int shift = 3;
16        String encrypted = encrypt(text,shift);
17        String decrypted = decrypt(encrypted, shift);
18        System.out.println("encrypted : "+encrypted);
19        System.out.println("decrypted : "+decrypted);
20    }
21}
22