public class CaesarCipher {
public static String encrypt(String plaintext, int shift) {
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i plaintext.length(); i++) {
char c = plaintext.charAt(i);
if (Character.isLetter(c)) {
c = (char) (((c - 'a' + shift) % 26) + 'a');
}
ciphertext.append(c);
}
return ciphertext.toString();
}
public static String decrypt(String ciphertext, int shift) {
StringBuilder plaintext = new StringBuilder();
for (int i = 0; i ciphertext.length(); i++) {
char c = ciphertext.charAt(i);
if (Character.isLetter(c)) {
c = (char) (((c - 'a' - shift + 26) % 26) + 'a');
}
plaintext.append(c);
}
return plaintext.toString();
}
public static void main(String[] args) {
String plaintext = "hello world";
int shift = 3;
String ciphertext = encrypt(plaintext, shift);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext, shift);
System.out.println("Decrypted text: " + decryptedText);
}
}

If Input Text is " Sookshmas" what is its cipher text?

Posted on by