Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;//323;4171716;599
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Cesar1 {
- // static String alphabet = "—абвгдеёжзийклмнопрстуфхцчшщъыьэюя,.!?»;`()…[]-+\":1234567890*&\\«=" + " %";
- static String alphabet="—abcdefghijklmnopqrstuvwxyz,.!?»;`'>()…[]-+\":1234567890*&\\«+="+" %";
- public static void main(String[] args) throws IOException {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Введите два взаимнопростых n и k:");
- int m = alphabet.length();
- int n = scanner.nextInt();
- int k = scanner.nextInt();
- System.out.println("Для операции шифрования введите 1,для дешифрования введите 0");
- //здесь шифруем
- if(scanner.nextInt() == 1){
- FileWriter out = new FileWriter("out.txt");
- Scanner scanner1 = new Scanner(new FileReader("input.txt"));
- while (scanner1.hasNextLine()) {
- String s = scanner1.nextLine().toLowerCase();//считываем строку
- String s1 = "";
- for (int i = 0; i < s.length(); i++) {//идем посимвольно
- int a = alphabet.indexOf(s.charAt(i));//считали букву
- if(a==-1)a=3;//если нет такой в алфавите,просто присваиваем число 3
- s1 += String.valueOf(alphabet.charAt((a * n + k % m) % m));
- }
- out.write(s1);
- out.append('\n');
- }
- out.flush();
- }
- else {//дешифрование
- FileWriter out = new FileWriter("inout.txt");
- Scanner scanner1 = new Scanner(new FileReader("out.txt"));
- BigInteger a1 = new BigInteger(String.valueOf(n));//засовывваю в спец класс, в котором есть функция деления по модулю
- BigInteger a2 = new BigInteger(String.valueOf(m));
- BigInteger a3 = a1.modInverse(a2);//это вот в^-1 степени
- int v = Integer.parseInt(String.valueOf(a3));
- /* int l=m; // можно еще брут форсом найти это число и не париться.найденное l подставляешь в формулу ниже
- while(l>0){
- if((l*n)%m==1)
- break;
- l--;
- }
- */
- while (scanner1.hasNextLine()) {
- String s = scanner1.nextLine();
- String s1 = "";
- for (int i = 0; i < s.length(); i++) {
- int b = alphabet.indexOf(s.charAt(i));
- s1 += String.valueOf(alphabet.charAt((v * (b + m - k%m)) % m));
- }
- out.write(s1);
- out.append('\n');
- }
- out.flush();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement