Sergei_Langin

Sergei Encryption Stage 6 Full

Apr 13th, 2020
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.02 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main {
  8.     static String importFile = "./";
  9.     static String exportFile = "./";
  10.  
  11.     public static String enc(String input, char[] alphabet0, int key) {
  12.         String output = "";
  13.         for (int i = 0; i < input.length(); i++) {
  14.             String inputCharacter = Character.toString(input.charAt(i));
  15.  
  16.             for (int j = 0; j < alphabet0.length; j++) {
  17.                 String alphabetCharacter = Character.toString(alphabet0[j]);
  18.                 String decryptedCharacter = Character.toString(alphabet0[(j + key) % alphabet0.length]);
  19.                 if (inputCharacter.equals(alphabetCharacter)) {
  20.                     output += decryptedCharacter;
  21.                 }
  22.             }
  23.         }
  24.         return output;
  25.     }
  26.  
  27.     public static String dec(String input, char[] alphabet0, int key) {
  28.         String output = "";
  29.         int k = alphabet0.length - 1, l = 0;
  30.         char[] alphabet = new char[alphabet0.length];
  31.         while (k >= 0) {
  32.             alphabet[l] = alphabet0[k];
  33.             k--;
  34.             l++;
  35.         }
  36.         for (int i = 0; i < input.length(); i++) {
  37.             String inputCharacter = Character.toString(input.charAt(i));
  38.             for (int j = 0; j < alphabet.length; j++) {
  39.                 String alphabetCharacter = Character.toString(alphabet[j]);
  40.                 String decryptedCharacter = Character.toString(alphabet[(j + key) % alphabet.length]);
  41.                 if (inputCharacter.equals(alphabetCharacter)) {
  42.                     output += decryptedCharacter;
  43.                 }
  44.             }
  45.         }
  46.         return output;
  47.     }
  48.  
  49.     public static String enc2(String input, String alphabet, int key) {
  50.         String output = "";
  51.         for (int i = 0; i < input.length(); i++) {
  52.             String inputCharacter = Character.toString(input.charAt(i));
  53.             if (!alphabet.contains(inputCharacter)) {
  54.                 output += inputCharacter;
  55.             }
  56.             for (int j = 0; j < alphabet.length(); j++) {
  57.                 String alphabetCharacter = Character.toString(alphabet.charAt(j));
  58.                 String decryptedCharacter = Character.toString(alphabet.charAt((j + key) % alphabet.length()));
  59.                 if (inputCharacter.equals(alphabetCharacter)) {
  60.                     output += decryptedCharacter;
  61.                 }
  62.             }
  63.         }
  64.         return output;
  65.     }
  66.  
  67.  
  68.     public static String dec2(String input, String alphabet, int key) {
  69.         String output = "";
  70.         for (int i = 0; i < input.length(); i++) {
  71.             String inputCharacter = Character.toString(input.charAt(i));
  72.             if (!alphabet.contains(inputCharacter)) {
  73.                 output += inputCharacter;
  74.             }
  75.             for (int j = 0; j < alphabet.length(); j++) {
  76.                 String alphabetCharacter = Character.toString(alphabet.charAt(j));
  77.                 String decryptedCharacter = Character.toString(alphabet.charAt((j + key) % alphabet.length()));
  78.                 if (inputCharacter.equals(alphabetCharacter)) {
  79.                     output += decryptedCharacter;
  80.                 }
  81.             }
  82.         }
  83.         return output;
  84.     }
  85.  
  86.     private static void lista(String data, File filename) {
  87.             try {
  88.                 FileWriter writer = new FileWriter(filename);
  89.                 writer.write(data);
  90.                 writer.close();
  91.             } catch (Exception e) {
  92.                 System.out.println("Error");
  93.             }
  94.     }
  95.  
  96.     public static void main(String[] args) throws Exception {
  97.         char[] alphabet = {' ', '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
  98.                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',
  99.                 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  100.                 'Y', 'Z', '[', '\\', ']', '^', '_', '`',
  101.                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  102.                 '{', '|', '}', '~'};
  103.         //char[] alphabet2 = {
  104.         //        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  105.         String alphabet2 = "abcdefghijklmnopqrstuvwxyz";
  106.         String alphabet3 = "zyxwvutsrqponmlkjihgfedcba";
  107.  
  108.         if (args.length > 0 ) {
  109.             for (int i = 0; i < args.length; i++) {
  110.                 switch (args[i]) {
  111.                     case "-in":
  112.                         importFile += args[i + 1];
  113.                         break;
  114.                     case "-out":
  115.                         exportFile += args[i + 1];
  116.                         break;
  117.                 }
  118.             }
  119.         }
  120.         File file = new File(importFile);
  121.         File file2 = new File(exportFile);
  122.  
  123.         int key = 0;
  124.         String data = "";
  125.         int count = 0;
  126.         int count2 = 0;
  127.         int count3 = 0;
  128.         int count4 = 0;
  129.         int count5 = 0;
  130.         int count6 = 0;
  131.         int count7 = 0;
  132.         int count8 = 0;
  133.         int cnt = 0;
  134.         int cnt2 = 0;
  135.         int cnt3 = 0;
  136.         int cnt4 = 0;
  137.  
  138.         for (int i=0; i < args.length; i++) {
  139.             if (args[i].equals("-key")) {
  140.                 if (args[i + 1].matches("[0-9]")) {
  141.                     try {
  142.                         key = Integer.parseInt(args[i + 1]);
  143.                     } catch (Exception e) {
  144.                         System.out.println("Error");
  145.                     }
  146.                 }
  147.             } else {
  148.                 cnt++;
  149.             }
  150.         }
  151.         for (int i=0; i < args.length; i++) {
  152.             if (args[i].equals("-data")) {
  153.                 data = args[i + 1];
  154.                 break;
  155.             } else if (args[i].equals("-in")) {
  156.                 try (Scanner skaner = new Scanner(file)) {
  157.                     data = skaner.nextLine();
  158.                 } catch (Exception e) {
  159.                     System.out.println("Error");
  160.                 }
  161.             } else {
  162.                 cnt2++;
  163.             }
  164.         }
  165.         for (int i=0; i < args.length; i++) {
  166.             if (args[i].equals("-mode")) {
  167.                 if (args[i + 1].equals("enc")) {
  168.                     count3++;
  169.                 } else if (args[i + 1].equals("dec")) {
  170.                     count4++;
  171.                 }
  172.             } else {
  173.                 cnt3++;
  174.             }
  175.         }
  176.         for (int i=0; i < args.length; i++) {
  177.             if (args[i].equals("-out")) {
  178.                 count6++;
  179.             } else {
  180.                 cnt4++;
  181.             }
  182.         }
  183.         for (int i=0; i < args.length; i++) {
  184.             if (args[i].equals("-alg")) {
  185.                 if (args[i + 1].equals("unicode")) {
  186.                     count7++;
  187.                 } else if (args[i + 1].equals("shift")) {
  188.                     count8++;
  189.                 }
  190.             }
  191.         }
  192.         if ((args.length - cnt > 1) | (args.length - cnt3 > 1) | (args.length - cnt4 > 1)) {
  193.             System.out.println("Error");
  194.         } else {
  195.             if ((count3==1 | count3==0) & count4==0) {
  196.                 if (count6==1) {
  197.                     if (count7==1) {
  198.                         lista(enc(data, alphabet, key), file2);
  199.                     } else if (count8==1) {
  200.                         lista(enc2(data, alphabet2, key), file2);
  201.                     }
  202.                 }
  203.             } else if (count4==1) {
  204.                 if (count6==1) {
  205.                     if (count7==1) {
  206.                         //System.out.println(enc(data, alphabet, key));
  207.                         lista(dec(data, alphabet, key), file2);
  208.                     } else if (count8==1) {
  209.                         //System.out.println(enc(data, alphabet, key));
  210.                         lista(dec2(data, alphabet3, key), file2);
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment