Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.swing.*;
- import java.util.*;
- public class Main {
- int keyLength;
- String key;
- List<Block> blocks;
- public static void main(String[] args){
- new Main().runProgram();
- }
- public void runProgram(){
- Scanner in = new Scanner(System.in);
- System.out.println("Введите ключ:");
- key = in.nextLine();
- keyLength = key.length();
- // makeTablesForCrypt();
- // crypt();
- makeTablesForDecrypt();
- }
- public void crypt(){
- int[] indexes = getSortedIndexes(key.toCharArray());
- // File file = chooseFile();
- try(BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/Desktop" + "/криптограммаслона.txt"))) {
- for (Block block : blocks) {
- for (int i = 0; i < indexes.length; i++) {
- for (String line : block.getBlock()) {
- if ((line.length() - 1) >= indexes[i])
- out.write(line.charAt(indexes[i]));
- }
- }
- }
- }catch(IOException ex){
- ex.printStackTrace();
- }
- }
- public void makeTablesForCrypt(){
- blocks = new ArrayList<>();
- String buffer = readFromFile(chooseFile());
- for(int i = 0; i < buffer.length();){
- List<String> currentBlock = new ArrayList<>();
- // формируем блок;
- for(int lengthCurrentLine = keyLength; lengthCurrentLine > 0 && i < buffer.length(); lengthCurrentLine--) {
- StringBuilder currentLineBlock = new StringBuilder();
- for (int j = 0; j < lengthCurrentLine && i < buffer.length(); i++) {
- if(isGoodChar(buffer.charAt(i))) {
- currentLineBlock.append(buffer.charAt(i));
- j++;
- }
- }
- currentBlock.add(currentLineBlock.toString());
- }
- //
- Block block = new Block(currentBlock);
- block.printBlock();
- blocks.add(block);
- }
- }
- public void makeTablesForDecrypt(){
- blocks = new ArrayList<>();
- String buffer = readFromFile(chooseFile());
- int[] indexes = getSortedIndexes(key.toCharArray());
- for(int i = 0; i < buffer.length();){
- List<StringBuilder> currentBlock = getEmptyTempBlock(keyLength);
- for(int indexColumn : indexes){
- for(int indexLine = 0; indexLine < keyLength - indexColumn && i < buffer.length(); indexLine++, i++)
- currentBlock.get(indexLine).setCharAt(indexColumn, buffer.charAt(i));
- }
- List<String> block = new ArrayList<>();
- for(StringBuilder line : currentBlock){
- block.add(line.toString());
- }
- Block temp = new Block(block);
- temp.printBlock();
- blocks.add(temp);
- }
- }
- public List<StringBuilder> getEmptyTempBlock(int keyLength){
- List<StringBuilder> result = new ArrayList<>();
- int key = keyLength;
- for(int i = 0; i < keyLength ; i++, key--) {
- StringBuilder currentLine = new StringBuilder();
- for (int lengthCurrentLine = key; lengthCurrentLine > 0; lengthCurrentLine--){
- currentLine.append("#");
- }
- result.add(currentLine);
- }
- return result;
- }
- public static String readFromFile(File file){
- StringBuilder result = new StringBuilder();
- try(BufferedReader in = new BufferedReader(new FileReader(file))){
- String currentLine;
- while((currentLine = in.readLine()) != null)
- result.append(currentLine);
- return result.toString().toUpperCase();
- }catch(IOException ex){
- ex.printStackTrace();
- }
- return null;
- }
- public File chooseFile() {
- JFileChooser chooser = new JFileChooser();
- chooser.showOpenDialog(null);
- return chooser.getSelectedFile();
- }
- public static boolean isGoodChar(char value){
- if((value >= 'А' && value <= 'Я')
- || (value >= '0' && value <= '9')
- || (value == 'Ё')){
- return true;
- }
- return false;
- }
- int[] getSortedIndexes(char[] key){
- ArrayList<Pair> pairs = new ArrayList<>();
- for(int i = 0; i < key.length; i++)
- pairs.add(new Pair(i, key[i]));
- Collections.sort(pairs);
- int[] result = new int[pairs.size()];
- int index = 0;
- for(Pair pair : pairs)
- result[index++] = pair.getIndex();
- return result;
- }
- class Block {
- List<String> block;
- Block(List<String> block){
- this.block = block;
- }
- public List<String> getBlock(){
- return block;
- }
- public char getCharAt(int i, int j){
- return block.get(i).charAt(j);
- }
- public void printBlock(){
- for(String line : block){
- StringBuilder currentLine = new StringBuilder();
- for(int i = 0; i < line.length(); i++){
- currentLine.append(line.charAt(i) + " ");
- }
- System.out.println(currentLine.toString());
- }
- }
- }
- class Pair implements Comparable<Pair> {
- int i;
- char c;
- public Pair(int i, char c){
- this.i = i;
- this.c = c;
- }
- int getIndex(){
- return i;
- }
- @Override
- public int compareTo(Pair o) {
- return Character.valueOf(this.c).compareTo(Character.valueOf(o.c));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement