Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zaschita1;
- import java.io.File;
- import java.util.Scanner;
- import java.io.FileWriter;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- public class Zaschita1 {
- static char[] russAlph = {'а', 'г', 'е', 'о', 'п', 'р', 'с', 'у', 'х', 'А', 'В', 'Е', 'К', 'М', 'Н', 'О', 'Р', 'С', 'Т', 'Х'};
- static char[] engAlph = {'a', 'r', 'e', 'o', 'n', 'p', 'c', 'y', 'x', 'A', 'B', 'E', 'K', 'M', 'H', 'O', 'P', 'C', 'T', 'X'};
- public static void encoding() throws Exception{ // ok
- System.out.print("Encoding: \n");
- Scanner sc = new Scanner(new File("src//zaschita1//input_word.txt"));
- String word = sc.nextLine();
- System.out.println("Phrase: " + word);
- String[] binAscii = new String[word.length()];
- for (int i = 0; i < binAscii.length; i++ ){
- binAscii[i] = String.format("%16s", Integer.toBinaryString((int) word.charAt(i))).replace(' ', '0');
- System.out.println(word.charAt(i) + " " + (int) word.charAt(i) + " " + binAscii[i]);
- }
- StringBuilder binAsciiConcat = new StringBuilder();//add
- int binAcounter = 0;// add суммарно сколько в слове бит
- for(int i = 0; i < binAscii.length; i ++){//add
- binAsciiConcat.append(binAscii[i]);//add
- }
- //List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//input_text.txt"), StandardCharsets.UTF_8);
- List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//input_text.txt"), StandardCharsets.UTF_8);
- List<StringBuilder> sb = new ArrayList<StringBuilder>(lines.size());
- for(int i = 0; i < lines.size(); i ++){
- sb.add(i, new StringBuilder(lines.get(i)));
- }
- FileWriter writer = new FileWriter("src//zaschita1//output.txt", false);
- System.out.println("binCodeConcat: " + binAsciiConcat.toString() + " " + binAsciiConcat.toString().length());
- for(int i = 0; i < lines.size(); i ++){
- for(int j = 0; j < lines.get(i).length(); j ++){
- here:
- for(int k = 0; k < russAlph.length; k ++){
- if(lines.get(i).charAt(j) == russAlph[k] && (binAcounter < binAsciiConcat.toString().length())){ // есть русская буква-перевертыш
- if(binAsciiConcat.toString().charAt(binAcounter) == '1'){ // = 1
- if(binAcounter == 47) System.out.println("ij: " + i + j);
- sb.get(i).replace(j, j + 1, String.valueOf(engAlph[k]));
- }
- binAcounter++;
- break here;
- }
- }
- if(binAcounter == binAsciiConcat.toString().length()){
- sb.get(i).insert(j + 1, '\u200B');
- binAcounter++;
- }
- }
- writer.append(sb.get(i).toString() + '\n'); //добавляем строку
- }
- writer.flush();
- System.out.println("u: " + sb.get(5).toString().indexOf('\u200b'));//"sym: " + sb.get(5).toString().charAt(1));
- System.out.println("sym: " + sb.get(5).toString().charAt(6));
- System.out.println("sym: " + lines.get(5).charAt(6));
- System.out.println("eng" + (int)(sb.get(5).toString().charAt(6)));
- System.out.println("rus" + (int)(lines.get(5).charAt(6)));
- System.out.println("rus" + (int)('В'));
- }
- public static void decoding() throws Exception{
- System.out.println("\nDecoding: ");
- List<String> lines = Files.readAllLines(Paths.get("src//zaschita1//output.txt"), StandardCharsets.UTF_8);
- StringBuilder sb = new StringBuilder();
- for(String line: lines){
- sb.append(line);
- }
- int indexU200B = sb.toString().indexOf('\u200B');
- int sbLength = sb.toString().length();
- sb.delete(indexU200B, sbLength);
- //System.out.println("sb: " + sb.toString());
- //FileWriter writerOutput = new FileWriter("src/com/company/output_word.txt", false);
- //writerOutput.write(sb.toString());
- //writerOutput.flush();
- StringBuilder resBool = new StringBuilder();
- while(sb.toString().indexOf(' ') != -1){ // пока есть пробелы
- if(sb.toString().charAt(sb.toString().indexOf(' ')) == sb.toString().charAt(sb.toString().indexOf(' ') + 1)){
- resBool.append('1');
- sb.delete(0, sb.toString().indexOf(' ') + 2);
- }
- else {
- resBool.append('0');
- sb.delete(0, sb.toString().indexOf(' ') + 1);
- }
- //System.out.println("остаток:" + sb.toString());
- }
- //System.out.println(resBool.toString());
- int start = 0, end = 16;
- int[] asciiCode = new int[resBool.toString().length() / 16];
- StringBuilder res = new StringBuilder();
- for (int i = 0; i < asciiCode.length; i++ ){
- asciiCode[i] = (char)Integer.parseInt(resBool.toString().substring(start, end), 2);
- start += 16;
- end += 16;
- System.out.println("code: " + asciiCode[i]);
- res.insert(res.length(), (char)asciiCode[i]);
- //System.out.println("char: " + res.toString());
- }
- FileWriter writerOutput = new FileWriter("src//zaschita1//output_word.txt", false);
- writerOutput.write(res.toString());
- writerOutput.flush();
- }
- public static void main(String[] args) throws Exception{
- encoding();
- //decoding();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement