Advertisement
moujik

Untitled

Feb 27th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.io.*;//323;4171716;599
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Cesar1 {
  7. // static String alphabet = "—абвгдеёжзийклмнопрстуфхцчшщъыьэюя,.!?»;`()…[]-+\":1234567890*&\\«=" + " %";
  8. static String alphabet="—abcdefghijklmnopqrstuvwxyz,.!?»;`'>()…[]-+\":1234567890*&\\«+="+" %";
  9. public static void main(String[] args) throws IOException {
  10. Scanner scanner = new Scanner(System.in);
  11. System.out.println("Введите два взаимнопростых n и k:");
  12. int m = alphabet.length();
  13. int n = scanner.nextInt();
  14.  
  15. int k = scanner.nextInt();
  16.  
  17. System.out.println("Для операции шифрования введите 1,для дешифрования введите 0");
  18. //здесь шифруем
  19. if(scanner.nextInt() == 1){
  20. FileWriter out = new FileWriter("out.txt");
  21. Scanner scanner1 = new Scanner(new FileReader("input.txt"));
  22. while (scanner1.hasNextLine()) {
  23. String s = scanner1.nextLine().toLowerCase();//считываем строку
  24. String s1 = "";
  25. for (int i = 0; i < s.length(); i++) {//идем посимвольно
  26. int a = alphabet.indexOf(s.charAt(i));//считали букву
  27. if(a==-1)a=3;//если нет такой в алфавите,просто присваиваем число 3
  28. s1 += String.valueOf(alphabet.charAt((a * n + k % m) % m));
  29. }
  30. out.write(s1);
  31. out.append('\n');
  32. }
  33. out.flush();
  34. }
  35. else {//дешифрование
  36. FileWriter out = new FileWriter("inout.txt");
  37. Scanner scanner1 = new Scanner(new FileReader("out.txt"));
  38. BigInteger a1 = new BigInteger(String.valueOf(n));//засовывваю в спец класс, в котором есть функция деления по модулю
  39. BigInteger a2 = new BigInteger(String.valueOf(m));
  40. BigInteger a3 = a1.modInverse(a2);//это вот в^-1 степени
  41. int v = Integer.parseInt(String.valueOf(a3));
  42. /* int l=m; // можно еще брут форсом найти это число и не париться.найденное l подставляешь в формулу ниже
  43. while(l>0){
  44. if((l*n)%m==1)
  45. break;
  46. l--;
  47. }
  48. */
  49. while (scanner1.hasNextLine()) {
  50. String s = scanner1.nextLine();
  51. String s1 = "";
  52. for (int i = 0; i < s.length(); i++) {
  53. int b = alphabet.indexOf(s.charAt(i));
  54. s1 += String.valueOf(alphabet.charAt((v * (b + m - k%m)) % m));
  55. }
  56. out.write(s1);
  57. out.append('\n');
  58. }
  59. out.flush();
  60. }
  61.  
  62.  
  63.  
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement