Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. using Crosstales.FB;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using System.IO;
  8.  
  9. public class Cipher_Controller : MonoBehaviour {
  10.  
  11. public Laundry_Data laundry_data;
  12. public Menu_Controller menu_controller;
  13.  
  14. public Canvas cipher_canvas;
  15. public Button txt_download_button;
  16. public GameObject left_main;
  17. public GameObject left_txt;
  18. public InputField left_txt_input_field;
  19. public GameObject left_img;
  20. public UnityEngine.UI.Image left_img_img;
  21. public Text title_text;
  22. public UnityEngine.UI.Image image;
  23.  
  24. private Bitmap bitmap;
  25. private int full_sqtr_size;
  26. private int currnet_sqtr_size;
  27.  
  28. private BitArray work_bits;
  29. private BitArray current_bits;
  30. private int number_of_iterations;
  31. private int current_iteration;
  32. private bool end_of_iteration;
  33. private int number_of_blocks;
  34. private int current_block;
  35. private bool end_of_blocks;
  36.  
  37. private List<Math_Functions> picked_functions;
  38. private Cipher_State cipher_state;
  39. private Cipher_Type cipher_type;
  40.  
  41. private string left_txt_output;
  42.  
  43. //=== MANAGE =====================================================================================================================================================
  44.  
  45. public void Ready_up_encryption() {
  46.  
  47. cipher_canvas.gameObject.SetActive(true);
  48. left_main.SetActive(true);
  49. left_txt.SetActive(false);
  50. left_img.SetActive(false);
  51.  
  52. work_bits = laundry_data.Get_raw_bits();
  53. picked_functions = laundry_data.Get_picked_functions();
  54. number_of_iterations = laundry_data.Get_iterations();
  55. current_iteration = 0;
  56. end_of_iteration = false;
  57. number_of_blocks = work_bits.Length / Static_Data.Get_double_segment_size();
  58. current_block = 0;
  59. end_of_blocks = false;
  60. cipher_state = Cipher_State.Encryption;
  61. cipher_type = laundry_data.Get_cipher_type();
  62. Set_current(work_bits, full_sqtr_size);
  63.  
  64. full_sqtr_size = Calculate_sqrt_floor(work_bits.Length);
  65. Create_img(work_bits, full_sqtr_size);
  66. title_text.text = Text_Data.Get_raw_text();
  67. }
  68.  
  69. private void Ready_up_dectyption() {
  70.  
  71. work_bits = laundry_data.Get_work_bits();
  72. Set_current(work_bits, Calculate_sqrt_floor(work_bits.Length));
  73. laundry_data.Set_end_bits(work_bits);
  74. current_iteration = 0;
  75. end_of_iteration = false;
  76. current_block = 0;
  77. end_of_blocks = false;
  78. cipher_state = Cipher_State.Decryption;
  79.  
  80. // klucz tyl do przodu dac
  81. }
  82.  
  83. private int Calculate_sqrt_floor(int number) {
  84.  
  85. return Mathf.FloorToInt(Mathf.Sqrt(number));
  86. }
  87.  
  88. private void Set_current(BitArray bits, int sqtr_size) {
  89.  
  90. current_bits = bits;
  91. currnet_sqtr_size = sqtr_size;
  92.  
  93. if (bits.Length > Static_Data.Get_max_txt_file()) {
  94.  
  95. txt_download_button.interactable = false;
  96. }
  97. else {
  98.  
  99. txt_download_button.interactable = true;
  100. }
  101. }
  102.  
  103. private void Create_img(BitArray bits, int sqtr_size) {
  104.  
  105. bitmap = new Bitmap(sqtr_size, sqtr_size);
  106.  
  107. int counter = 0;
  108.  
  109. for (int i = 0; i < sqtr_size; i++) {
  110.  
  111. for (int j = 0; j < sqtr_size; j++) {
  112.  
  113. if (bits[counter] == true) {
  114.  
  115. bitmap.SetPixel(j, i, System.Drawing.Color.White);
  116. }
  117. else {
  118.  
  119. bitmap.SetPixel(j, i, System.Drawing.Color.Black);
  120. }
  121.  
  122. counter++;
  123. }
  124. }
  125.  
  126. bitmap.Save(Application.persistentDataPath + "/Output.png");
  127. WWW www = new WWW("file:///" + Application.persistentDataPath + "/Output.png");
  128. Sprite sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
  129. sprite.texture.filterMode = FilterMode.Point;
  130. image.sprite = sprite;
  131. }
  132.  
  133. private void Stand_by() {
  134.  
  135. File.Delete(Application.persistentDataPath + "/Output.png");
  136. File.Delete(Application.persistentDataPath + "/img.jpeg");
  137.  
  138. cipher_canvas.gameObject.SetActive(false);
  139. }
  140.  
  141. //=== BUTTONS =====================================================================================================================================================
  142.  
  143. public void On_click_back() {
  144.  
  145. Stand_by();
  146. menu_controller.Restart();
  147. }
  148.  
  149. public void On_click_download_txt() {
  150.  
  151. string path = FileBrowser.SaveFile("Save as txt", string.Empty, "txt_result", "txt");
  152. Converter_Helper.String_to_txt(Converter_Helper.Bit_to_string(current_bits, currnet_sqtr_size), path);
  153. }
  154.  
  155. public void On_click_download_img() {
  156.  
  157. string path = FileBrowser.SaveFile("Save as img", string.Empty, "img_result", "jpeg");
  158. Converter_Helper.Bitmap_to_img(bitmap, path);
  159. }
  160.  
  161. public void On_click_block_button() {
  162.  
  163. if (!end_of_iteration) {
  164.  
  165. if (!end_of_blocks) {
  166.  
  167. Show_block();
  168. }
  169. else {
  170.  
  171. Show_iteration();
  172. }
  173. }
  174. else {
  175.  
  176. Show_end();
  177. }
  178. }
  179.  
  180. public void On_click_iteration_button() {
  181.  
  182. if (!end_of_iteration) {
  183.  
  184. Calculate_iteration();
  185. }
  186. else {
  187.  
  188. Show_end();
  189. }
  190. }
  191.  
  192. public void On_click_end_button() {
  193.  
  194. Calculate_end();
  195. }
  196.  
  197. public void On_click_left_txt_refresh() {
  198.  
  199. left_txt_input_field.text = left_txt_output;
  200. }
  201.  
  202. public void On_click_left_txt_download_txt() {
  203.  
  204. string path = FileBrowser.SaveFile("Save as txt", string.Empty, "txt_result", "txt");
  205. Converter_Helper.String_to_txt(left_txt_output, path);
  206. }
  207.  
  208. public void On_click_left_img_download_img() {
  209.  
  210. string path = FileBrowser.SaveFile("Save as img", string.Empty, "img_result", "jpeg");
  211. File.Copy(Application.persistentDataPath + "/img.jpeg", path);
  212. }
  213.  
  214. //=== CIPHER =====================================================================================================================================================
  215.  
  216. private void Show_block() {
  217.  
  218. int temp_round = current_iteration + 1;
  219. int temp_block = current_block + 1;
  220.  
  221. if (cipher_state == Cipher_State.Encryption) {
  222.  
  223. title_text.text = Text_Data.Get_encrypted_text() + Text_Data.Get_round_text() + temp_round.ToString() + Text_Data.Get_block_text() + temp_block.ToString();
  224. }
  225. else {
  226.  
  227. title_text.text = Text_Data.Get_decrypted_text() + Text_Data.Get_round_text() + temp_round.ToString() + Text_Data.Get_block_text() + temp_block.ToString();
  228. }
  229.  
  230. BitArray bits_temp = Calculate_block();
  231. Set_current(bits_temp, Calculate_sqrt_floor(bits_temp.Length));
  232. Create_img(current_bits, currnet_sqtr_size);
  233. }
  234.  
  235. private BitArray Calculate_block() {
  236.  
  237. var list_block = laundry_data.Get_bit_block(current_block);
  238. BitArray block1 = list_block[0];
  239. BitArray block2 = list_block[1];
  240.  
  241. BitArray double_block = new BitArray(Static_Data.Get_double_segment_size());
  242.  
  243. // === FUNKCJE + KLUCZ ===
  244.  
  245. if (picked_functions.Contains(Math_Functions.F1)) {
  246.  
  247. for (int i = 0; i < block1.Length; i++) {
  248.  
  249. block1[i] = block1[i] ^ block2[i];
  250. }
  251. }
  252.  
  253. if (picked_functions.Contains(Math_Functions.F2)) {
  254.  
  255. }
  256.  
  257. if (picked_functions.Contains(Math_Functions.F3)) {
  258.  
  259. }
  260.  
  261. if (picked_functions.Contains(Math_Functions.F4)) {
  262.  
  263. }
  264.  
  265. // === XOR ===
  266.  
  267. for (int i = 0; i < block1.Length; i++) {
  268.  
  269. //block1[i] = block1[i] ^ block2[i];
  270. }
  271.  
  272. // === ZAMIANA MIEJSCAMI
  273.  
  274. for (int i = 0; i < Static_Data.Get_segment_size(); i++) {
  275.  
  276. double_block[i] = block2[i];
  277. }
  278.  
  279. int counter = 0;
  280.  
  281. for (int i = Static_Data.Get_segment_size(); i < double_block.Length; i++) {
  282.  
  283. double_block[i] = block1[counter];
  284. counter++;
  285. }
  286.  
  287. // === DOPISANIE GOTOWEGO BLOKU DO PELNIEJ WIADOMOSCI ===
  288.  
  289. laundry_data.Set_bit_block(double_block, current_block);
  290.  
  291. current_block++;
  292.  
  293. if (current_block >= number_of_blocks) {
  294.  
  295. end_of_blocks = true;
  296. }
  297.  
  298. return double_block;
  299. }
  300.  
  301. private void Show_iteration() {
  302.  
  303. int temp_iteration = current_iteration + 1;
  304.  
  305. if (cipher_state == Cipher_State.Encryption) {
  306.  
  307. title_text.text = Text_Data.Get_encrypted_text() + Text_Data.Get_round_text() + temp_iteration.ToString();
  308. }
  309. else {
  310.  
  311. title_text.text = Text_Data.Get_decrypted_text() + Text_Data.Get_round_text() + temp_iteration.ToString();
  312. }
  313.  
  314. work_bits = laundry_data.Get_work_bits();
  315. end_of_blocks = false;
  316. current_block = 0;
  317. current_iteration++;
  318.  
  319. if (current_iteration >= number_of_iterations) {
  320.  
  321. end_of_iteration = true;
  322. }
  323.  
  324. Set_current(work_bits, Calculate_sqrt_floor(work_bits.Length));
  325. Create_img(current_bits, currnet_sqtr_size);
  326. }
  327.  
  328. private void Calculate_iteration() {
  329.  
  330. for (int i = current_block; i < number_of_blocks; i++) {
  331.  
  332. Calculate_block();
  333. }
  334.  
  335. Show_iteration();
  336. }
  337.  
  338. private void Show_end() {
  339.  
  340. if (cipher_state == Cipher_State.Encryption) {
  341.  
  342. Encryption_end();
  343. }
  344. else {
  345.  
  346. Decryption_end();
  347. }
  348. }
  349.  
  350. private void Encryption_end() {
  351.  
  352. title_text.text = Text_Data.Get_encrypted_end_text();
  353.  
  354. work_bits = laundry_data.Get_work_bits();
  355.  
  356. Set_current(work_bits, Calculate_sqrt_floor(work_bits.Length));
  357. Create_img(current_bits, currnet_sqtr_size);
  358.  
  359. Ready_up_dectyption();
  360. }
  361.  
  362. private void Decryption_end() {
  363.  
  364. title_text.text = Text_Data.Get_decrypted_end_text();
  365.  
  366. work_bits = laundry_data.Get_work_bits();
  367.  
  368. Set_current(work_bits, Calculate_sqrt_floor(work_bits.Length));
  369. Create_img(current_bits, currnet_sqtr_size);
  370.  
  371. left_main.SetActive(false);
  372. var bytes = Converter_Helper.Binary_to_byte(current_bits);
  373.  
  374. if (cipher_type == Cipher_Type.Txt) {
  375.  
  376. left_txt.SetActive(true);
  377.  
  378. var txt = Converter_Helper.Bytes_to_string(bytes);
  379. left_txt_output = txt;
  380. left_txt_input_field.text = left_txt_output;
  381. }
  382. else {
  383.  
  384. left_img.SetActive(true);
  385.  
  386. var img = Converter_Helper.Bytes_to_img(bytes);
  387. img.Save(Application.persistentDataPath + "/img.jpeg");
  388. WWW www = new WWW("file:///" + Application.persistentDataPath + "/img.jpeg");
  389. Sprite sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
  390. left_img_img.sprite = sprite;
  391. }
  392. }
  393.  
  394. private void Calculate_end() {
  395.  
  396. for (int i = current_iteration; i < number_of_iterations; i++) {
  397.  
  398. Calculate_iteration();
  399. }
  400.  
  401. Show_end();
  402. }
  403.  
  404. //=== LOOP =====================================================================================================================================================
  405.  
  406. void Start () {
  407.  
  408. }
  409.  
  410. void Update () {
  411.  
  412. }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement