Vigenere Cipher

1. 2. package com.sanfoundry.setandstring; 3. 4. public class VigenereCipher 5. { 6. public static String encrypt(String text, final String key) 7. { 8. String res = ""; 9. text = text.toUpperCase(); 10. for (int i = 0, j = 0; i < text.length(); i++) 11. { 12. char c = text.charAt(i); 13. if (c < 'A' || c > 'Z') 14. continue; 15. res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); 16. j = ++j % key.length(); 17. } 18. return res; 19. } 20. 21. public static String decrypt(String text, final String key) 22. { 23. String res = ""; 24. text = text.toUpperCase(); 25. for (int i = 0, j = 0; i < text.length(); i++) 26. { 27. char c = text.charAt(i); 28. if (c < 'A' || c > 'Z') 29. continue; 30. res += (char) ((c - key.charAt(j) + 26) % 26 + 'A'); 31. j = ++j % key.length(); 32. } 33. return res; 34. } 35. 36. public static void main(String[] args) 37. { 38. String key = "VIGENERECIPHER"; 39. String message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"; 40. String encryptedMsg = encrypt(message, key); 41. System.out.println("String: " + message); 42. System.out.println("Encrypted message: " + encryptedMsg); 43. System.out.println("Decrypted message: " + decrypt(encryptedMsg, key)); 44. } 45. }

Output: $ javac VigenereCipher.java $ java VigenereCipher String: Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Encrypted message: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY Decrypted message: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

Vigenere Cipher.pdf

Download. Connect more apps... Try one of the apps below to open or edit this item. Vigenere Cipher.pdf. Vigenere Cipher.pdf. Open. Extract. Open with. Sign In.

137KB Sizes 1 Downloads 76 Views

Recommend Documents

vigenere cipher example pdf
Page 1 of 1. vigenere cipher example pdf. Click here if your download doesn't start automatically. Page 1 of 1. vigenere cipher example pdf. vigenere cipher ...