Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SquareChiffer : MonoBehaviour {
- public string input;
- public string output;
- string test = "";
- void OnGUI ()
- {
- GUI.Label(new Rect(Screen.width/2-35,Screen.height/2,100,25),"Text: ");
- input = GUI.TextField(new Rect(Screen.width/2,Screen.height/2,100,25),input.ToLower());
- if(GUI.Button(new Rect(Screen.width/2 + 110,Screen.height/2,100,25),"Encrypt"))
- {
- Encrypt(input);
- }
- GUI.Label(new Rect(Screen.width/2-95,Screen.height/2 + 35,100,25),"Encrypted text: ");
- output = GUI.TextField(new Rect(Screen.width/2,Screen.height/2 + 35,100,25),output.ToLower());
- if(GUI.Button(new Rect(Screen.width/2 + 110,Screen.height/2 + 35,100,25),"Decrypt"))
- {
- Decrypt(output);
- }
- int square = Mathf.CeilToInt(Mathf.Sqrt(test.Length));
- int fakk = 0;
- for(int i = 0; i < square;i++)
- {
- for(int x = 0; x < square;x++)
- {
- GUI.Button(new Rect(x*50,i*50,50,50),test[fakk].ToString());
- if(fakk <= test.Length)
- {
- fakk++;
- }
- }
- }
- }
- void Encrypt(string input)
- {
- int square = Mathf.CeilToInt(Mathf.Sqrt(input.Length)); // ALL MY TESTS SAY THIS ONE WORKS :^)
- string temp = "";
- for(int x = 0; x < square; x++)
- {
- for(int i = 0; i < square; i++)
- {
- if(i*square+x < input.Length)
- {
- temp += input[i*square+x];
- test = temp;
- }
- }
- }
- output = temp;
- }
- void Decrypt(string output)
- {
- int square = Mathf.CeilToInt(Mathf.Sqrt(output.Length)); // THIS WORKS SOMETIMES :^)
- string temp = "";
- for(int x = 0; x < square; x++)
- {
- for(int i = 0; i < square; i++)
- {
- if(i*square+x < output.Length)
- {
- temp += output[i*square+x];
- test = temp;
- }
- }
- }
- input = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement