Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.98 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.event.ActionEvent;
  4. import javafx.fxml.Initializable;
  5. import javafx.scene.control.*;
  6. import javafx.stage.Stage;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.net.URL;
  11. import java.util.*;
  12. import java.util.stream.Stream;
  13.  
  14. import static java.lang.StrictMath.abs;
  15.  
  16. public class Controller implements Initializable {
  17.  
  18. public Label result_label;
  19. public Label klucz_label;
  20. public Label slowo_label;
  21. public Label warning;
  22.  
  23. public RadioButton radio_railfence;
  24. public RadioButton radio_macierzeA;
  25. public RadioButton radio_macierzeB;
  26. public RadioButton cezar;
  27. public RadioButton radio_macierzeC;
  28. public RadioButton radio_vigener;
  29.  
  30. public TextField klucz_tf;
  31. public TextField slowo_tf;
  32. public TextField result3_tf;
  33. public Button end_button;
  34. public Button randomButton;
  35.  
  36. ToggleGroup toggleGroup = new ToggleGroup();
  37. ArrayList<String> words;
  38.  
  39. @Override
  40. public void initialize(URL url, ResourceBundle resourceBundle) {
  41.  
  42. radio_railfence.setToggleGroup(toggleGroup);
  43. radio_macierzeB.setToggleGroup(toggleGroup);
  44. radio_macierzeA.setToggleGroup(toggleGroup);
  45. radio_macierzeC.setToggleGroup(toggleGroup);
  46. cezar.setToggleGroup(toggleGroup);
  47. radio_vigener.setToggleGroup(toggleGroup);
  48.  
  49. words = new ArrayList<>();
  50. File file = new File("src/sample/file.txt");
  51. try {
  52. Scanner in = new Scanner(file);
  53. while(in.hasNext()){
  54. words.add(in.nextLine());
  55. }
  56. } catch (FileNotFoundException e) {
  57.  
  58. e.printStackTrace();
  59. }
  60.  
  61. }
  62.  
  63. public void endButtonClicked(ActionEvent actionEvent) {
  64. Stage stage = (Stage) end_button.getScene().getWindow();
  65. stage.close();
  66. }
  67.  
  68. public void randomButtonPressed(ActionEvent actionEvent) {
  69. Random variable = new Random();
  70. int k = variable.nextInt(words.size());
  71.  
  72. slowo_tf.setText(words.get(k));
  73. }
  74.  
  75. public void szyfrujButtonClicked(ActionEvent actionEvent) {
  76. RadioButton selected = (RadioButton) toggleGroup.getSelectedToggle();
  77.  
  78. if (klucz_tf.getText().isEmpty() || slowo_tf.getText().isEmpty()) {
  79. warning.setText("Pola 'Klucz' oraz 'Słowo' nie mogą być puste ");
  80. } else {
  81.  
  82. /*Szyfrowanie metodą rail fence*/
  83. if (radio_railfence.equals(selected)) {
  84. String word = slowo_tf.getText();
  85. int key = Integer.parseInt(klucz_tf.getText());
  86.  
  87. char[] output = SzyfrowanieRailFence(word.toCharArray(), key);
  88. result3_tf.setText(String.valueOf(output));
  89.  
  90. } /*Szyfrowanie metodą macierzową A*/ else if (radio_macierzeA.equals(selected)) {
  91. String word= slowo_tf.getText();
  92. String key = String.valueOf((klucz_tf.getText()));
  93. int[] keyArr = Stream.of(key.replaceAll("-", "").split("")).mapToInt(Integer::parseInt).toArray();
  94. String result = SzyfrowanieMacierzoweA(word, keyArr);
  95.  
  96. result3_tf.setText(result);
  97.  
  98.  
  99. } /*Szyfrowanie metodą macierzową B*/ else if (radio_macierzeB.equals(selected)) {
  100. String word = slowo_tf.getText();
  101. String key = klucz_tf.getText();
  102. String result = szyfrowanieMacierzB(word,key);
  103. result3_tf.setText(result);
  104.  
  105. } /*Szyfr Cezara*/ else if(cezar.equals(selected)){
  106. String word = slowo_tf.getText();
  107. int key = Integer.parseInt(klucz_tf.getText());
  108. String result = Cezar(word,key,0);
  109.  
  110. result3_tf.setText(result);
  111.  
  112. } /*Szyfrowanie metodą macierzową C*/ else if(radio_macierzeC.equals(selected)){
  113. String word = slowo_tf.getText();
  114. String key = klucz_tf.getText();
  115. String result = szyfrowanieMacierzC(word,key);
  116. result3_tf.setText(result);
  117.  
  118. } /*Szyfrowanie Vigenere'a*/ else if(radio_vigener.equals(selected)){
  119.  
  120. }
  121. else {
  122. warning.setText("Wybierz metodę szyfrowania");
  123. }
  124.  
  125. }
  126. }
  127.  
  128. public void deszyfrujButtonClicked(ActionEvent actionEvent) {
  129. RadioButton selected = (RadioButton) toggleGroup.getSelectedToggle();
  130.  
  131. if (klucz_tf.getText().isEmpty() || slowo_tf.getText().isEmpty()) {
  132. warning.setText("Pola 'Klucz' oraz 'Słowo' nie mogą być puste ");
  133. } else {
  134. String word = slowo_tf.getText();
  135.  
  136.  
  137. /*Deszyfrowanie metodą rail fence*/
  138. if (radio_railfence.equals(selected)) {
  139. int key = Integer.parseInt(klucz_tf.getText());
  140.  
  141. char[] result = DeszyfrowanieRailFence(word.toCharArray(), key);
  142. result3_tf.setText(String.valueOf(result));
  143.  
  144. }
  145. /*Deszyfrowanie metodą macierzową A*/ else if (radio_macierzeA.equals(selected)) {
  146. String key = String.valueOf((klucz_tf.getText()));
  147. int[] keyArr = Stream.of(key.replaceAll("-", "").split("")).mapToInt(Integer::parseInt).toArray();
  148. String result = DeszyfrowanieMacierzoweA(word, keyArr);
  149. result3_tf.setText(result);
  150.  
  151. } /*Deszyfrowanie metodą macierzową B*/ else if (radio_macierzeB.equals(selected)) {
  152. String key = klucz_tf.getText();
  153. String result = deszyfrowanieMacierzB(word,key);
  154. result3_tf.setText(result);
  155.  
  156. } /*Deszyfrowanie Cezara*/ else if(cezar.equals(selected)){
  157. int key = Integer.parseInt(klucz_tf.getText());
  158. String result = Cezar(word,key,1);
  159.  
  160. result3_tf.setText(result);
  161.  
  162. }
  163. /*Deszyfrowanie metodą macierzową C*/ else if(radio_macierzeC.equals(selected)) {
  164. String key = klucz_tf.getText();
  165. String result = deszyfrowanieMacierzC(word,key);
  166. result3_tf.setText(result);
  167.  
  168. } /*Deszyfrowanie metodą vigener'a*/ else if(radio_vigener.equals(selected)){
  169.  
  170. }
  171. else {
  172. warning.setText("Wybierz metodę deszyfrowania");
  173. }
  174. }
  175. }
  176.  
  177.  
  178. /* Rail fence poniżej */
  179. public static void init(char[][] tab, int height,int size){
  180.  
  181. for(int y=0;y<height;y++){
  182. for(int x=0;x<size;x++) {
  183. tab[y][x] = '0';
  184. }
  185. }
  186.  
  187. }
  188.  
  189.  
  190. public static char[] result(char[]output, int size, char[][] password,int key){
  191. int line=0;
  192.  
  193. for(int i=0;i<key;i++)
  194. for(int x=0;x<size;x++){
  195. if(password[i][x] != '0'){
  196. output[line] = password[i][x];
  197. line++;
  198. }
  199. }
  200.  
  201. return output;
  202. }
  203. public static String SzyfrowanieMacierzoweA(String password, int[] keyArr) {
  204. double cols = keyArr.length;
  205. int i = 0;
  206. int j = 0;
  207. double rows = Math.ceil(password.length() / cols);
  208.  
  209. char[][] cipher = fillMatrixA(cols, rows, password);
  210. StringBuilder answer = new StringBuilder();
  211. for (i = 0; i < rows; i++) {
  212. for (int k : keyArr) {
  213. char next = cipher[i][k - 1];
  214. if (next != '0') {
  215. answer.append(cipher[i][k - 1]);
  216. }
  217.  
  218. }
  219. }
  220. return answer.toString();
  221. }
  222. public static String DeszyfrowanieMacierzoweA(String password,int[] keyArr) {
  223. double cols = keyArr.length;
  224. double rows = Math.ceil(password.length() / cols);
  225. char[][] tab= new char[(int) rows][(int) cols];
  226.  
  227. //wypełnienie zerami
  228. for (char[] row: tab)
  229. Arrays.fill(row, '0');
  230. fillMatrixDecode(password.length(),rows,cols,tab,keyArr,password);
  231. StringBuilder answer = new StringBuilder();
  232. for (int i = 0; i < rows; i++) {
  233. for (int j = 0; j < cols; j++) {
  234. char next = tab[i][j];
  235. if (next != '0') {
  236. answer.append(tab[i][j]);
  237. }
  238. }
  239. }
  240. System.out.println(answer);
  241. return answer.toString();
  242. }
  243. public static char[][] fillMatrixA(double cols, double rows, String password) {
  244. char[][] cipher = new char[(int) rows][(int) cols];
  245. char[] e = password.toCharArray();
  246. int i = 0;
  247. int j = 0;
  248. init(cipher, (int) rows, (int) cols);
  249. for (i = 0; i < e.length; i++) {
  250. cipher[j][(int) (i % cols)] = e[i];
  251. if (i % cols == cols - 1) {
  252. j++;
  253. }
  254. }
  255. return cipher;
  256. }
  257. private static void fillMatrixDecode(int wordLength, double rows, double cols, char[][] matrix, int[] arr, String text) {
  258. int row = 0;
  259. int keyIndex = 0;
  260. int col = arr[keyIndex];
  261. int remainder = (int) (wordLength % cols);
  262. System.out.println("reszta" + remainder);
  263.  
  264. for (int i = 0; i < wordLength; i++) {
  265.  
  266. // check field out of bounds
  267. while (remainder > 0 && row == rows - 1 && col > remainder) {
  268. keyIndex++;
  269. col = arr[keyIndex];
  270. }
  271. matrix[row][col - 1] = text.charAt(i);
  272. keyIndex++;
  273.  
  274. if (keyIndex == cols) {
  275. row++;
  276. keyIndex = 0;
  277. }
  278.  
  279. col = arr[keyIndex];
  280.  
  281.  
  282. }
  283.  
  284. }
  285. public static char[] SzyfrowanieRailFence(char[] password, int key){
  286. char[][] szyfr = new char[key][password.length];
  287. char[] output = new char[password.length];
  288. int new_key = 2*key - 2;
  289. int j = 0, height = 1;
  290.  
  291. init(szyfr, key,password.length);
  292.  
  293. for(int i=0;i<password.length;i++){
  294. szyfr[abs(j)][i] = password[i];
  295.  
  296. if(i%new_key == 0){
  297. height *= 1;
  298. }
  299. if((i+key-1)%new_key == 0){
  300. height *= -1;
  301. }
  302. j += height;
  303. }
  304.  
  305. result(output, password.length, szyfr, key);
  306.  
  307. return output;
  308. }
  309.  
  310. public static char[] DeszyfrowanieRailFence(char[] password, int key){
  311. char[][] szyfr = new char[key][password.length];
  312. char[] output = new char[password.length];
  313. int new_key = 2*key - 2;
  314. int j = 0, height = -1, n=0;
  315.  
  316. init(szyfr, key,password.length);
  317.  
  318. for(int i=0;i<password.length;i++){
  319. szyfr[abs(j)][i] = '*';
  320. if(i%new_key == 0 || (i+key-1)%new_key == 0)
  321. height *= -1;
  322.  
  323. j += height;
  324. }
  325.  
  326. for(int y=0;y<key;y++)
  327. for(int x=0;x<password.length;x++){
  328. if(szyfr[y][x] == '*'){
  329. szyfr[y][x] = password[n];
  330. n++;
  331. }
  332. }
  333.  
  334. j=0;
  335. height = -1;
  336.  
  337. for(int i=0;i<password.length;i++){
  338. output[i] =szyfr[abs(j)][i];
  339.  
  340. if(i%new_key == 0){
  341. height *= 1;
  342. }
  343. if((i+key-1)%new_key == 0){
  344. height *= -1;
  345. }
  346. j += height;
  347. }
  348.  
  349. return output;
  350. }
  351.  
  352.  
  353. /* Szyfr Cezara*/
  354.  
  355. public static String Cezar(String password, int key,int param){
  356. StringBuilder sb = new StringBuilder();
  357.  
  358. switch (param){
  359. case 0:
  360. for(int i=0;i<password.length();i++){
  361. sb.append(szyfrujZnak(password.substring(i,i+1),key));
  362. }
  363. break;
  364. case 1:
  365. for(int i=0;i<password.length();i++){
  366. sb.append(deszyfrujZnak(password.substring(i,i+1),key));
  367. }
  368. break;
  369. }
  370.  
  371. return sb.toString();
  372. }
  373.  
  374. public static String szyfrujZnak(String s, int key){
  375. int index = 0;
  376. String[] alphabet = {"A","B", "C", "D", "E", "F", "G", "H", "I", "J",
  377. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  378.  
  379. for(int i=0;i<alphabet.length;i++){
  380. if(s.equalsIgnoreCase(alphabet[i])) {
  381. index = (i+key)%(alphabet.length);
  382. break;
  383. }
  384. if(s.equals(" ") || s.equals(",")){
  385. return s;
  386. }
  387. }
  388.  
  389. return alphabet[index];
  390. }
  391.  
  392. public static String deszyfrujZnak(String s, int key){
  393. int index = 0;
  394. String[] alphabet = {"A","B", "C", "D", "E", "F", "G", "H", "I", "J",
  395. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  396.  
  397. for(int i=0;i<alphabet.length;i++){
  398.  
  399. if(s.equalsIgnoreCase(alphabet[i])) {
  400. index = i - key;
  401.  
  402. if(index <0){
  403. index = alphabet.length + index;
  404. }
  405. break;
  406. }
  407. if(s.equals(" ") || s.equals(",")){
  408. return s;
  409. }
  410. }
  411.  
  412. return alphabet[index];
  413. }
  414.  
  415. /*Szyfrowanie metodą macierzową C*/
  416.  
  417. public static String szyfrowanieMacierzC(String s, String k)
  418. {
  419.  
  420. s = s.replaceAll(" ", "");
  421. int[] key = kolejnoscKlucza(k);
  422. String out = "";
  423. int r = 0;
  424. int count = 0;
  425.  
  426. //ustalenie ile bedzie potrzebnych wierszy do macierzy
  427. for(int i=0; i < key.length; i++) {
  428. count += key[i];
  429. r++;
  430. if(count >= s.length())
  431. break;
  432. }
  433.  
  434. //wypelnienie tablicy zerami
  435. char[][] tab = new char[r][k.length()];
  436. for (char[] row: tab)
  437. Arrays.fill(row, '0');
  438.  
  439. //wpisywanie po kolei liter danego slowa do macierzy wierszami(do momentu określonego przez key)
  440. int x=0;
  441. for (int i=0; i< r; i++) {
  442. for(int j=0; j<key[i]; j++) {
  443.  
  444. if(x==s.length())
  445. break;
  446. tab[i][j] = s.charAt(x);
  447. x++;
  448. }
  449. }
  450.  
  451. //odczytywanie kolumnami zaszyfrowanego kodu
  452. for (int i=0; i < key.length; i++) {
  453. for (int j=0; j<r; j++) {
  454. if(tab[j][key[i]-1] != '0') {
  455. out += tab[j][key[i] - 1];
  456.  
  457. }
  458. }out+=" ";
  459. }
  460.  
  461. return out;
  462. }
  463.  
  464. public static String deszyfrowanieMacierzC(String s, String k) {
  465. s = s.replaceAll(" ", "");
  466. int[] o = kolejnoscKlucza(k);
  467. int[] key = polozenie(o);
  468. String out = "";
  469. int r = 0;
  470. int count = 0;
  471. int last = 0;
  472.  
  473. for(int i=0; i < o.length; i++) {
  474. if(count+o[i]>=s.length()) {
  475. last = s.length() - count;
  476. r++;
  477. break;
  478. }
  479. count += o[i];
  480. r++;
  481. }
  482.  
  483. char[][] tab = new char[r][k.length()];
  484. for (char[] row: tab)
  485. Arrays.fill(row, '0');
  486.  
  487. int x=0;
  488. for (int i=0; i< o.length; i++) {
  489. int c = o[i]-1;
  490. for(int j=0; j<r; j++) {
  491. if(x==s.length())
  492. break;
  493. if (o[j] > c) {
  494. if(j < r-1 || (j==(r-1) && c<last)) {
  495. tab[j][c] = s.charAt(x);
  496. x++;
  497. }
  498. }
  499. }
  500. }
  501.  
  502. for (int j=0; j<r; j++) {
  503. for (int i=0; i < key.length; i++) {
  504. if(tab[j][i] != '0')
  505. out += tab[j][i];
  506. }
  507. }
  508.  
  509. return out;
  510. }
  511.  
  512. //zwraca tablice, ktora przechowuje miejsca liter klucza (zgodnie z alfabetem)
  513. public static int[] polozenie(int[] key) {
  514. int[] k = new int[key.length];
  515. for (int i = 0; i < key.length; i++) {
  516. k[key[i] - 1] = i + 1;
  517. }
  518. return k;
  519. }
  520.  
  521. //funkcja do przyporzadkowania kolejności liter klucza co do alfabetu
  522. private static int[] kolejnoscKlucza(String s) {
  523. String alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
  524. int[] k = new int[s.length()]; //tablica o rozmiarze równym liczbie kolumn macierzy(liter klucza)
  525. int x = 0;
  526. int c;
  527.  
  528. for (int j = 0; j < alphabet.length(); j++) {
  529. c = -1;
  530. c = s.indexOf(alphabet.charAt(j), c + 1);
  531. //wchodzimy do while jesli znajdziemy litere z klucza
  532. while (c >= 0) {
  533. k[c] = ++x;
  534. c = s.indexOf(alphabet.charAt(j), c + 1); // jak dana litera wiecej sie nie powtorzy, to wychodzimy z petli (c=-1)
  535. }
  536. }
  537.  
  538. return polozenie(k);
  539.  
  540. }
  541. public static String szyfrowanieMacierzB(String s, String k){
  542. s=s.replace(" ","");
  543. String output="";
  544. int pom1 = 0; //zmienna pomocnicza potrzebna do wprowadzenia waidomosci do macierzy
  545. int wiersze;
  546. int[] klucz = kolejnoscKlucza(k); //zamiana hasla na ciag liczbowy
  547.  
  548. if (s.length() % k.length() == 0) {
  549. wiersze = s.length() / k.length(); //obliczanie ilosci wierszy
  550. } else wiersze = wiersze = (s.length() / k.length()) + 1;
  551.  
  552.  
  553. char[][] tab = new char[k.length()][wiersze];
  554.  
  555. for (int j = 0; j < wiersze; j++) {
  556. for (int i = 0; i < k.length(); i++) { //wypelnienie tablicy zerami
  557. tab[i][j] = '0';
  558. }
  559. }
  560.  
  561. for (int j = 0; j < wiersze; j++) {
  562. for (int i = 0; i < k.length(); i++) {
  563. if (pom1 >= s.length()) {
  564. break; //wprowadzenie wiadomosci do tablicy
  565. } else {
  566. tab[i][j] = s.charAt(pom1);
  567. pom1++;
  568. }
  569. }
  570. }
  571.  
  572. int i = 0;
  573. for (int p = 0; p < k.length(); p++) {
  574. for (int j = 0; j < wiersze; j++) {
  575. if (tab[klucz[i] - 1][j] != '0') { //szyfrowanie waidomosci outut przy pomocy klucza
  576. output = output + tab[klucz[i] - 1][j];
  577. } else break;
  578.  
  579.  
  580. }
  581. output = output + " ";
  582. i++;
  583. }
  584. return output;
  585. }
  586. public static String deszyfrowanieMacierzB(String s, String k) {
  587. String output = "";
  588. int pom1 = 0;
  589. int[] klucz = ustawianieKlucza(k);
  590. int kol;
  591.  
  592. kol = (s.length() / k.length()) + 1; //okreslenie ilosci kolumn (pomocnicze)
  593.  
  594. char[][] tab = new char[kol][k.length()]; // tablica pomocnicza
  595. char[][] tabOut = new char[kol][k.length()]; //tablica z przestawionymi wierszami
  596.  
  597. for (int j = 0; j < k.length(); j++) {
  598. for (int i = 0; i < kol; i++) {
  599. tab[i][j] = '0';
  600. }
  601. }
  602. for (int j = 0; j < k.length(); j++) {
  603. for (int i = 0; i < kol; i++) {
  604. tabOut[i][j] = '0';
  605. }
  606. }
  607.  
  608. for (int j = 0; j < k.length(); j++) {
  609. for (int i = 0; i < kol; i++) {
  610. if (pom1 >= s.length()) {
  611. break;
  612. }
  613. if (s.charAt(pom1) != ' ') {
  614. tab[i][j] = s.charAt(pom1); //wypelnienie tablicy poziomo ustawonymi fragmetami zakodowanej wiadomosci
  615. pom1++;
  616. } else {
  617. pom1++;
  618.  
  619. break;
  620.  
  621. }
  622. }
  623. }
  624.  
  625.  
  626.  
  627. int pom2 = 0;
  628. for (int j = 0; j < k.length(); j++) {
  629. for (int i = 0; i < kol; i++) {
  630. if (pom2 >= k.length()) break;
  631. tabOut[i][j] = tab[i][klucz[pom2] - 1]; // wypelnianie tablicy Out przestawionymi wierszami tablicy pomocniczej
  632.  
  633. }
  634. pom2++;
  635. }
  636.  
  637. int pom =0;
  638.  
  639. for(int j=0;j<kol;j++){
  640. for(int i=0;i<k.length();i++){
  641. if(tabOut[j][i]=='0')break; //odszyfrowywanie waidomosci
  642. output=output+tabOut[j][i];
  643.  
  644. }}
  645.  
  646.  
  647. return output;
  648. }
  649.  
  650. public static int[] ustawianieKlucza(String s) {
  651. String alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
  652. int[] k = new int[s.length()]; //tablica o rozmiarze równym liczbie kolumn macierzy(liter klucza)
  653. int x = 0;
  654. int c;
  655.  
  656. for (int j = 0; j < alphabet.length(); j++) {
  657. c = -1;
  658. c = s.indexOf(alphabet.charAt(j), c + 1);
  659. //wchodzimy do while jesli znajdziemy litere z klucza
  660. while (c >= 0) {
  661. k[c] = ++x;
  662. c = s.indexOf(alphabet.charAt(j), c + 1); // jak dana litera wiecej sie nie powtorzy, to wychodzimy z petli (c=-1)
  663. }
  664. }
  665.  
  666. return k;
  667.  
  668. }
  669. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement