Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1.  
  2. public class Vigenerecipher {
  3. public static void main(String[] args) {
  4. String key = "ncl";
  5. String original = "newcastleuniversity";
  6. String encripted = encrypt(original, key);
  7. System.out.println(encripted);
  8. System.out.println(decrypt(encripted, key));
  9. }
  10.  
  11. static String encrypt(String text, final String key) {
  12. String res = "";
  13. text = text.toLowerCase();
  14. char[] stringArray = text.toCharArray();
  15. for (int i = 0, j = 0; i < text.length(); i++) {
  16.  
  17. if ((int)stringArray[i] < 97 || (int)stringArray[i] > 122) continue;
  18. res += (char)(((int)stringArray[i] + key.charAt(j) - 2 * 'a') % 26 + 'a');
  19. j = ++j % key.length();
  20. }
  21. return res;
  22. }
  23.  
  24. static String decrypt(String text, final String key) {
  25. String res = "";
  26. text = text.toLowerCase();
  27. for (int i = 0, j = 0; i < text.length(); i++) {
  28. char c = text.charAt(i);
  29. if (c < 97 || c > 122) continue;
  30. res += (char)((c - key.charAt(j) + 26) % 26 + 'a');
  31. j = ++j % key.length();
  32. }
  33. return res;
  34. }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement