Advertisement
ibragimova_mariam

3 стенография

Sep 29th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. package zaschita1;
  2. import java.io.File;
  3. import java.util.Scanner;
  4. import java.io.FileWriter;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class Zaschita1 {
  12.  
  13. static char[] russAlph = {'а', 'г', 'е', 'о', 'п', 'р', 'с', 'у', 'х', 'А', 'В', 'Е', 'К', 'М', 'Н', 'О', 'Р', 'С', 'Т', 'Х'};
  14. static char[] engAlph = {'a', 'r', 'e', 'o', 'n', 'p', 'c', 'y', 'x', 'A', 'B', 'E', 'K', 'M', 'H', 'O', 'P', 'C', 'T', 'X'};
  15.  
  16. public static void encoding() throws Exception{ // ok
  17. System.out.print("Encoding: \n");
  18.  
  19. Scanner sc = new Scanner(new File("src//zaschita1//input_word.txt"));
  20. String word = sc.nextLine();
  21. System.out.println("Phrase: " + word);
  22.  
  23. String[] binAscii = new String[word.length()];
  24. for (int i = 0; i < binAscii.length; i++ ){
  25. binAscii[i] = String.format("%16s", Integer.toBinaryString((int) word.charAt(i))).replace(' ', '0');
  26. System.out.println(word.charAt(i) + " " + (int) word.charAt(i) + " " + binAscii[i]);
  27. }
  28.  
  29. StringBuilder binAsciiConcat = new StringBuilder();//add
  30. int binAcounter = 0;// add суммарно сколько в слове бит
  31. for(int i = 0; i < binAscii.length; i ++){//add
  32. binAsciiConcat.append(binAscii[i]);//add
  33. }
  34.  
  35. //List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//input_text.txt"), StandardCharsets.UTF_8);
  36. List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//input_text.txt"), StandardCharsets.UTF_8);
  37. List<StringBuilder> sb = new ArrayList<StringBuilder>(lines.size());
  38. for(int i = 0; i < lines.size(); i ++){
  39. sb.add(i, new StringBuilder(lines.get(i)));
  40. }
  41.  
  42. FileWriter writer = new FileWriter("src//zaschita1//output.txt", false);
  43.  
  44. System.out.println("binCodeConcat: " + binAsciiConcat.toString() + " " + binAsciiConcat.toString().length());
  45.  
  46. for(int i = 0; i < lines.size(); i ++){
  47. for(int j = 0; j < lines.get(i).length(); j ++){
  48. here:
  49. for(int k = 0; k < russAlph.length; k ++){
  50. if(lines.get(i).charAt(j) == russAlph[k] && (binAcounter < binAsciiConcat.toString().length())){ // есть русская буква-перевертыш
  51. if(binAsciiConcat.toString().charAt(binAcounter) == '1'){ // = 1
  52. if(binAcounter == 47) System.out.println("ij: " + i + j);
  53. sb.get(i).replace(j, j + 1, String.valueOf(engAlph[k]));
  54. }
  55. binAcounter++;
  56. break here;
  57. }
  58. }
  59. if(binAcounter == binAsciiConcat.toString().length()){
  60. sb.get(i).insert(j + 1, '\u200B');
  61. binAcounter++;
  62. }
  63. }
  64. writer.append(sb.get(i).toString() + '\n'); //добавляем строку
  65. }
  66.  
  67. writer.flush();
  68. System.out.println("u: " + sb.get(5).toString().indexOf('\u200b'));//"sym: " + sb.get(5).toString().charAt(1));
  69. System.out.println("sym: " + sb.get(5).toString().charAt(6));
  70. System.out.println("sym: " + lines.get(5).charAt(6));
  71. System.out.println("eng" + (int)(sb.get(5).toString().charAt(6)));
  72. System.out.println("rus" + (int)(lines.get(5).charAt(6)));
  73. System.out.println("rus" + (int)('В'));
  74.  
  75. }
  76. public static void decoding() throws Exception{
  77. System.out.println("\nDecoding: ");
  78. List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//output.txt"), StandardCharsets.UTF_8);
  79. StringBuilder sb = new StringBuilder();
  80.  
  81. for(String line: lines){
  82. sb.append(line);
  83. }
  84.  
  85. int indexU200B = sb.toString().indexOf('\u200B');
  86. int sbLength = sb.toString().length();
  87. sb.delete(indexU200B, sbLength);
  88.  
  89. //System.out.println("sb: " + sb.toString());
  90.  
  91. //FileWriter writerOutput = new FileWriter("src/com/company/output_word.txt", false);
  92. //writerOutput.write(sb.toString());
  93. //writerOutput.flush();
  94.  
  95. StringBuilder resBool = new StringBuilder();
  96. while(sb.toString().indexOf(' ') != -1){ // пока есть пробелы
  97. if(sb.toString().charAt(sb.toString().indexOf(' ')) == sb.toString().charAt(sb.toString().indexOf(' ') + 1)){
  98. resBool.append('1');
  99. sb.delete(0, sb.toString().indexOf(' ') + 2);
  100. }
  101. else {
  102. resBool.append('0');
  103. sb.delete(0, sb.toString().indexOf(' ') + 1);
  104. }
  105. //System.out.println("остаток:" + sb.toString());
  106. }
  107. //System.out.println(resBool.toString());
  108.  
  109.  
  110.  
  111. int start = 0, end = 16;
  112. int[] asciiCode = new int[resBool.toString().length() / 16];
  113.  
  114. StringBuilder res = new StringBuilder();
  115. for (int i = 0; i < asciiCode.length; i++ ){
  116. asciiCode[i] = (char)Integer.parseInt(resBool.toString().substring(start, end), 2);
  117. start += 16;
  118. end += 16;
  119. System.out.println("code: " + asciiCode[i]);
  120. res.insert(res.length(), (char)asciiCode[i]);
  121. //System.out.println("char: " + res.toString());
  122. }
  123.  
  124. FileWriter writerOutput = new FileWriter("src//zaschita1//output_word.txt", false);
  125. writerOutput.write(res.toString());
  126. writerOutput.flush();
  127.  
  128. }
  129.  
  130. public static void main(String[] args) throws Exception{
  131. encoding();
  132. //decoding();
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement