Untitled
import java.io.*; class CaesarDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any String: "); String str = br.readLine(); String e = encrypt(str, 3); System.out.println("Encrypted String is: " + e); String d = decrypt(e, 3); System.out.println("Decrypted String is: " + d); } static String encrypt(String str, int key) { String cipher = ""; for (int i = 0; i < str.length(); i++) { int c = str.charAt(i); c = c + (key % 26); cipher += (char) c; } return cipher; } static String decrypt(String str, int key) { String plain = ""; for (int i = 0; i < str.length(); i++) { int c = str.charAt(i); c = c - (key % 26); plain += (char) c; } return plain; } }
Leave a Comment