fork download
  1. package com.sanfoundry.setandstring;
  2.  
  3.  
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8.  
  9. public class VigenereCipher
  10. {
  11. public static String encrypt(String text, final String key)
  12. {
  13. String res = "";
  14. text = text.toUpperCase();
  15. for (int i = 0, j = 0; i < text.length(); i++)
  16. {
  17. char c = text.charAt(i);
  18. if (c < 'A' || c > 'Z')
  19. continue;
  20. res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
  21. j = ++j % key.length();
  22. }
  23. return res;
  24. }
  25.  
  26. public static String decrypt(String text, final String key)
  27. {
  28. String res = "";
  29. text = text.toUpperCase();
  30. for (int i = 0, j = 0; i < text.length(); i++)
  31. {
  32. char c = text.charAt(i);
  33. if (c < 'A' || c > 'Z')
  34. continue;
  35. res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
  36. j = ++j % key.length();
  37. }
  38. return res;
  39. }
  40.  
  41. public static void main(String[] args)
  42. {
  43. String key = "VIGENERECIPHER";
  44. String message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
  45. String encryptedMsg = encrypt(message, key);
  46. System.out.println("String: " + message);
  47. System.out.println("Encrypted message: " + encryptedMsg);
  48. System.out.println("Decrypted message: " + decrypt(encryptedMsg, key));
  49. }
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class VigenereCipher is public, should be declared in a file named VigenereCipher.java
public class VigenereCipher
       ^
1 error
stdout
Standard output is empty