Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 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.scene.paint.Color;
  7. import javafx.stage.Stage;
  8.  
  9. import java.awt.event.KeyAdapter;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.Random;
  15. import java.util.ResourceBundle;
  16. import java.util.Scanner;
  17. import java.util.stream.Stream;
  18.  
  19. import static java.lang.StrictMath.abs;
  20. import static javafx.scene.paint.Color.RED;
  21.  
  22. public class Controller implements Initializable {
  23.  
  24. public Label result_label;
  25. public Label result2_label;
  26. public Label klucz_label;
  27. public Label slowo_label;
  28. public Label warning;
  29.  
  30. public RadioButton radio_railfence;
  31. public RadioButton radio_macierzeA;
  32. public RadioButton radio_macierzeB;
  33. public RadioButton cezar;
  34. public RadioButton radio_macierzeC;
  35. public RadioButton radio_vigener;
  36.  
  37. public TextField klucz_tf;
  38. public TextField slowo_tf;
  39.  
  40. public Button end_button;
  41. public Button randomButton;
  42.  
  43. ToggleGroup toggleGroup = new ToggleGroup();
  44. ArrayList<String> words;
  45.  
  46. @Override
  47. public void initialize(URL url, ResourceBundle resourceBundle) {
  48.  
  49. radio_railfence.setToggleGroup(toggleGroup);
  50. radio_macierzeB.setToggleGroup(toggleGroup);
  51. radio_macierzeA.setToggleGroup(toggleGroup);
  52. radio_macierzeC.setToggleGroup(toggleGroup);
  53. cezar.setToggleGroup(toggleGroup);
  54. radio_vigener.setToggleGroup(toggleGroup);
  55.  
  56. words = new ArrayList<>();
  57. File file = new File("src/sample/words.txt");
  58. try {
  59. Scanner in = new Scanner(file);
  60. while(in.hasNext()){
  61. words.add(in.nextLine());
  62. }
  63. } catch (FileNotFoundException e) {
  64.  
  65. e.printStackTrace();
  66. }
  67.  
  68. }
  69.  
  70. public void endButtonClicked(ActionEvent actionEvent) {
  71. Stage stage = (Stage) end_button.getScene().getWindow();
  72. stage.close();
  73. }
  74.  
  75. public void randomButtonPressed(ActionEvent actionEvent) {
  76. Random variable = new Random();
  77. int k = variable.nextInt(words.size());
  78.  
  79. slowo_tf.setText(words.get(k));
  80. }
  81.  
  82. public void szyfrujButtonClicked(ActionEvent actionEvent) {
  83. RadioButton selected = (RadioButton) toggleGroup.getSelectedToggle();
  84.  
  85. if (klucz_tf.getText().isEmpty() || slowo_tf.getText().isEmpty()) {
  86. warning.setText("Pola 'Klucz' oraz 'Słowo' nie mogą być puste ");
  87. } else {
  88.  
  89. /*Szyfrowanie metodą rail fence*/
  90. if (radio_railfence.equals(selected)) {
  91. String word = slowo_tf.getText();
  92. int key = Integer.parseInt(klucz_tf.getText());
  93.  
  94. char[] output = SzyfrowanieRailFence(word.toCharArray(), key);
  95. result2_label.setText(String.valueOf(output));
  96.  
  97. } /*Szyfrowanie metodą macierzową A*/ else if (radio_macierzeA.equals(selected)) {
  98. String word= slowo_tf.getText();
  99. String key= String.valueOf((klucz_tf.getText()));
  100. String result = SzyfrowanieMacierzoweA(word,key);
  101. result2_label.setText(result);
  102.  
  103.  
  104. } /*Szyfrowanie metodą macierzową B*/ else if (radio_macierzeB.equals(selected)) {
  105.  
  106. } /*Szyfr Cezara*/ else if(cezar.equals(selected)){
  107. String word = slowo_tf.getText();
  108. int key = Integer.parseInt(klucz_tf.getText());
  109. String result = Cezar(word,key,0);
  110.  
  111. result2_label.setText(result);
  112. } /*Szyfrowanie metodą macierzową C*/ else if(radio_macierzeC.equals(selected)){
  113.  
  114. } /*Szyfrowanie Vigenere'a*/ else if(radio_vigener.equals(selected)){
  115.  
  116. }
  117. else {
  118. warning.setText("Wybierz metodę szyfrowania");
  119. }
  120.  
  121. }
  122. }
  123.  
  124. public void deszyfrujButtonClicked(ActionEvent actionEvent) {
  125. RadioButton selected = (RadioButton) toggleGroup.getSelectedToggle();
  126.  
  127. if (klucz_tf.getText().isEmpty() || slowo_tf.getText().isEmpty()) {
  128. warning.setText("Pola 'Klucz' oraz 'Słowo' nie mogą być puste ");
  129. } else {
  130.  
  131. /*Deszyfrowanie metodą rail fence*/
  132. if (radio_railfence.equals(selected)) {
  133. String word = slowo_tf.getText();
  134. int key = Integer.parseInt(klucz_tf.getText());
  135.  
  136. char[] result = DeszyfrowanieRailFence(word.toCharArray(), key);
  137. result2_label.setText(String.valueOf(result));
  138.  
  139. } /*Deszyfrowanie metodą macierzową A*/ else if (radio_macierzeA.equals(selected)) {
  140.  
  141. } /*Deszyfrowanie metodą macierzową B*/ else if (radio_macierzeB.equals(selected)) {
  142.  
  143. } /*Deszyfrowanie Cezara*/ else if(cezar.equals(selected)){
  144. String word = slowo_tf.getText();
  145. int key = Integer.parseInt(klucz_tf.getText());
  146. String result = Cezar(word,key,1);
  147.  
  148. result2_label.setText(result);
  149. } /*Deszyfrowanie metodą macierzową C*/ else if(radio_macierzeC.equals(selected)) {
  150.  
  151. } /*Deszyfrowanie metodą vigener'a*/ else if(radio_vigener.equals(selected)){
  152.  
  153. }
  154. else {
  155. warning.setText("Wybierz metodę deszyfrowania");
  156. }
  157. }
  158. }
  159.  
  160.  
  161. /* Rail fence poniżej */
  162. public static void init(char[][] tab, int height,int size){
  163.  
  164. for(int y=0;y<height;y++){
  165. for(int x=0;x<size;x++) {
  166. tab[y][x] = '0';
  167. }
  168. }
  169.  
  170. }
  171.  
  172. public static char[] result(char[]output, int size, char[][] password,int key){
  173. int line=0;
  174.  
  175. for(int i=0;i<key;i++)
  176. for(int x=0;x<size;x++){
  177. if(password[i][x] != '0'){
  178. output[line] = password[i][x];
  179. line++;
  180. }
  181. }
  182.  
  183. return output;
  184. }
  185. public static String SzyfrowanieMacierzoweA(String password,String key) {
  186.  
  187. int[] keyArr = Stream.of(key.replaceAll("-", "").split("")).mapToInt(Integer::parseInt).toArray();
  188. double cols = keyArr.length;
  189. int i = 0;
  190. int j = 0;
  191. char[] e;
  192. e = password.toCharArray();
  193. double rows = Math.ceil(password.length() / cols);
  194. char[][] cipher = new char[(int) rows][(int) cols];
  195. init(cipher, (int) rows, (int) cols);
  196. for (i = 0; i < e.length; i++) {
  197. cipher[j][(int) (i % rows)] = e[i];
  198. if (i % cols == cols - 1) {
  199. j++;
  200. }
  201. }
  202. StringBuilder answer = new StringBuilder();
  203. for (i = 0; i < rows; i++) {
  204. for (int k : keyArr) {
  205. char next = cipher[i][k - 1];
  206. if (next != '0') {
  207. answer.append(cipher[i][k - 1]);
  208. }
  209.  
  210. }
  211. }
  212. return answer.toString();
  213. }
  214.  
  215.  
  216.  
  217.  
  218. public static char[] SzyfrowanieRailFence(char[] password, int key){
  219. char[][] szyfr = new char[key][password.length];
  220. char[] output = new char[password.length];
  221. int new_key = 2*key - 2;
  222. int j = 0, height = 1;
  223.  
  224. init(szyfr, key,password.length);
  225.  
  226. for(int i=0;i<password.length;i++){
  227. szyfr[abs(j)][i] = password[i];
  228.  
  229. if(i%new_key == 0){
  230. height *= 1;
  231. }
  232. if((i+key-1)%new_key == 0){
  233. height *= -1;
  234. }
  235. j += height;
  236. }
  237.  
  238. result(output, password.length, szyfr, key);
  239.  
  240. return output;
  241. }
  242.  
  243. public static char[] DeszyfrowanieRailFence(char[] password, int key){
  244. char[][] szyfr = new char[key][password.length];
  245. char[] output = new char[password.length];
  246. int new_key = 2*key - 2;
  247. int j = 0, height = -1, n=0;
  248.  
  249. init(szyfr, key,password.length);
  250.  
  251. for(int i=0;i<password.length;i++){
  252. szyfr[abs(j)][i] = '*';
  253. if(i%new_key == 0 || (i+key-1)%new_key == 0)
  254. height *= -1;
  255.  
  256. j += height;
  257. }
  258.  
  259. for(int y=0;y<key;y++)
  260. for(int x=0;x<password.length;x++){
  261. if(szyfr[y][x] == '*'){
  262. szyfr[y][x] = password[n];
  263. n++;
  264. }
  265. }
  266.  
  267. j=0;
  268. height = -1;
  269.  
  270. for(int i=0;i<password.length;i++){
  271. output[i] =szyfr[abs(j)][i];
  272.  
  273. if(i%new_key == 0){
  274. height *= 1;
  275. }
  276. if((i+key-1)%new_key == 0){
  277. height *= -1;
  278. }
  279. j += height;
  280. }
  281.  
  282. return output;
  283. }
  284.  
  285.  
  286. /* Szyfr Cezara*/
  287.  
  288. public static String Cezar(String password, int key,int param){
  289. StringBuilder sb = new StringBuilder();
  290.  
  291. switch (param){
  292. case 0:
  293. for(int i=0;i<password.length();i++){
  294. sb.append(szyfrujZnak(password.substring(i,i+1),key));
  295. }
  296. break;
  297. case 1:
  298. for(int i=0;i<password.length();i++){
  299. sb.append(deszyfrujZnak(password.substring(i,i+1),key));
  300. }
  301. break;
  302. }
  303.  
  304. return sb.toString();
  305. }
  306.  
  307. public static String szyfrujZnak(String s, int key){
  308. int index = 0;
  309. String[] alphabet = {"A","B", "C", "D", "E", "F", "G", "H", "I", "J",
  310. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  311.  
  312. for(int i=0;i<alphabet.length;i++){
  313. if(s.equalsIgnoreCase(alphabet[i])) {
  314. index = (i+key)%(alphabet.length);
  315. break;
  316. }
  317. if(s.equals(" ") || s.equals(",")){
  318. return s;
  319. }
  320. }
  321.  
  322. return alphabet[index];
  323. }
  324.  
  325. public static String deszyfrujZnak(String s, int key){
  326. int index = 0;
  327. String[] alphabet = {"A","B", "C", "D", "E", "F", "G", "H", "I", "J",
  328. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  329.  
  330. for(int i=0;i<alphabet.length;i++){
  331.  
  332. if(s.equalsIgnoreCase(alphabet[i])) {
  333. index = i - key;
  334.  
  335. if(index <0){
  336. index = alphabet.length + index;
  337. }
  338. break;
  339. }
  340. if(s.equals(" ") || s.equals(",")){
  341. return s;
  342. }
  343. }
  344.  
  345. return alphabet[index];
  346. }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement