Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SquareChiffer : MonoBehaviour {
  5.  
  6. public string input;
  7. public string output;
  8. string test = "";
  9.  
  10. void OnGUI ()
  11. {
  12. GUI.Label(new Rect(Screen.width/2-35,Screen.height/2,100,25),"Text: ");
  13. input = GUI.TextField(new Rect(Screen.width/2,Screen.height/2,100,25),input.ToLower());
  14. if(GUI.Button(new Rect(Screen.width/2 + 110,Screen.height/2,100,25),"Encrypt"))
  15. {
  16. Encrypt(input);
  17. }
  18. GUI.Label(new Rect(Screen.width/2-95,Screen.height/2 + 35,100,25),"Encrypted text: ");
  19. output = GUI.TextField(new Rect(Screen.width/2,Screen.height/2 + 35,100,25),output.ToLower());
  20. if(GUI.Button(new Rect(Screen.width/2 + 110,Screen.height/2 + 35,100,25),"Decrypt"))
  21. {
  22. Decrypt(output);
  23. }
  24.  
  25. int square = Mathf.CeilToInt(Mathf.Sqrt(test.Length));
  26. int fakk = 0;
  27. for(int i = 0; i < square;i++)
  28. {
  29. for(int x = 0; x < square;x++)
  30. {
  31. GUI.Button(new Rect(x*50,i*50,50,50),test[fakk].ToString());
  32. if(fakk <= test.Length)
  33. {
  34. fakk++;
  35. }
  36.  
  37. }
  38. }
  39. }
  40. void Encrypt(string input)
  41. {
  42. int square = Mathf.CeilToInt(Mathf.Sqrt(input.Length)); // ALL MY TESTS SAY THIS ONE WORKS :^)
  43. string temp = "";
  44. for(int x = 0; x < square; x++)
  45. {
  46. for(int i = 0; i < square; i++)
  47. {
  48. if(i*square+x < input.Length)
  49. {
  50. temp += input[i*square+x];
  51. test = temp;
  52. }
  53. }
  54. }
  55.  
  56. output = temp;
  57.  
  58. }
  59.  
  60. void Decrypt(string output)
  61. {
  62.  
  63. int square = Mathf.CeilToInt(Mathf.Sqrt(output.Length)); // THIS WORKS SOMETIMES :^)
  64. string temp = "";
  65. for(int x = 0; x < square; x++)
  66. {
  67. for(int i = 0; i < square; i++)
  68. {
  69. if(i*square+x < output.Length)
  70. {
  71. temp += output[i*square+x];
  72. test = temp;
  73. }
  74. }
  75. }
  76.  
  77. input = temp;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement