Advertisement
ibragimova_mariam

Вижинер

Dec 1st, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Vigenere {
  4.  
  5. private static String characters = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ";
  6. private static final int N = characters.length();
  7.  
  8. private static String encode(String input, String keyword)
  9. {
  10. input = input.toUpperCase();
  11. keyword = keyword.toUpperCase();
  12.  
  13. String result = "";
  14.  
  15. int keyword_index = 0;
  16.  
  17. for (char symbol : input.toCharArray())
  18. {
  19. if (symbol == ' ')
  20. {
  21. result += " ";
  22. continue;
  23. }
  24.  
  25. int c = (characters.indexOf(symbol)
  26. + characters.indexOf(keyword.charAt(keyword_index))) % N;
  27.  
  28. result += characters.charAt(c);
  29.  
  30. keyword_index++;
  31.  
  32. if ((keyword_index) == keyword.length())
  33. keyword_index = 0;
  34. }
  35.  
  36. return result;
  37. }
  38.  
  39. private static String decode(String input, String keyword)
  40. {
  41. input = input.toUpperCase();
  42. keyword = keyword.toUpperCase();
  43.  
  44. String result = "";
  45.  
  46. int keyword_index = 0;
  47.  
  48. for (char symbol : input.toCharArray())
  49. {
  50. if (symbol == ' ')
  51. {
  52. result += " ";
  53. continue;
  54. }
  55.  
  56. int p = (characters.indexOf(symbol) + N -
  57. characters.indexOf(keyword.charAt(keyword_index))) % N;
  58.  
  59. result += characters.charAt(p);
  60.  
  61. keyword_index++;
  62.  
  63. if ((keyword_index) == keyword.length())
  64. keyword_index = 0;
  65. }
  66.  
  67. return result;
  68. }
  69.  
  70. public static void main(String []args)
  71. {
  72. Scanner in = new Scanner(System.in);
  73. String input, key;
  74.  
  75. while (true)
  76. {
  77. System.out.println("Введите текст для шифрования: ");
  78. input = in.nextLine();
  79.  
  80. System.out.println("Введите ключ: ");
  81. key = in.nextLine();
  82.  
  83. String cipher = encode(input, key);
  84. System.out.println("Зашифрованный текст: " + cipher);
  85. System.out.println("Расшифровка: " + decode(cipher, key));
  86.  
  87. System.out.println("Продолжить [д/н]? ");
  88.  
  89. if(in.nextLine().equalsIgnoreCase("н"))
  90. break;
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement