Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Vigenere {
- private static String characters = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ";
- private static final int N = characters.length();
- private static String encode(String input, String keyword)
- {
- input = input.toUpperCase();
- keyword = keyword.toUpperCase();
- String result = "";
- int keyword_index = 0;
- for (char symbol : input.toCharArray())
- {
- if (symbol == ' ')
- {
- result += " ";
- continue;
- }
- int c = (characters.indexOf(symbol)
- + characters.indexOf(keyword.charAt(keyword_index))) % N;
- result += characters.charAt(c);
- keyword_index++;
- if ((keyword_index) == keyword.length())
- keyword_index = 0;
- }
- return result;
- }
- private static String decode(String input, String keyword)
- {
- input = input.toUpperCase();
- keyword = keyword.toUpperCase();
- String result = "";
- int keyword_index = 0;
- for (char symbol : input.toCharArray())
- {
- if (symbol == ' ')
- {
- result += " ";
- continue;
- }
- int p = (characters.indexOf(symbol) + N -
- characters.indexOf(keyword.charAt(keyword_index))) % N;
- result += characters.charAt(p);
- keyword_index++;
- if ((keyword_index) == keyword.length())
- keyword_index = 0;
- }
- return result;
- }
- public static void main(String []args)
- {
- Scanner in = new Scanner(System.in);
- String input, key;
- while (true)
- {
- System.out.println("Введите текст для шифрования: ");
- input = in.nextLine();
- System.out.println("Введите ключ: ");
- key = in.nextLine();
- String cipher = encode(input, key);
- System.out.println("Зашифрованный текст: " + cipher);
- System.out.println("Расшифровка: " + decode(cipher, key));
- System.out.println("Продолжить [д/н]? ");
- if(in.nextLine().equalsIgnoreCase("н"))
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement