Check out the program's clip here.
Introduction
Probably all of us tried to write a code to scramble text so that we could pass secret messages around. C# has encrypting ability built in but it's not fun to use something that is already there. I had some extra(!) time last couple of days, so I wrote my own text scrambler.
This program gets you a different output each time... it is very difficult to crack. You have to know where to look, I don't think anyone who doesn't know the code could find the encrypted text in the scrambled version, but who knows... Some of us are very smart. ;)
The Idea
This program has two sections of scrambling. In the first section, it transfers text into binary code. I was able to use bitArray
, but I couldn't find a simple way to reverse it, so I added "character" and "binary" lists instead. The scrambler is a class, so I think it should work as fast as a normal encryptor. The second section of the scrambler is actually doing the heavy work. It does enter random characters and random amount between each binary number. Every time the binary replacements and the amount of the characters between them are random, so that no one would actually know which character set is to be replaced with the binary numbers (1 and 0s).
The Code
(Article format: Explanations followed by their code.)
Form1. Class calls from the button event handlers.
{
public partial class Form1 : Form{
Scrambler.Scrambler NewScr = new Scrambler.Scrambler();
public Form1(){
InitializeComponent();}
private void button2_Click(object sender, EventArgs e){
MessageBox.Show(NewScr.MainString(textBox2.Text ,0));}
private void button3_Click(object sender, EventArgs e){
MessageBox.Show(NewScr.MainString(textBox3.Text, 1));}
private void button1_Click(object sender, EventArgs e){
textBox2.Text = NewScr.BinaryString(textBox1.Text);
textBox3.Text = NewScr.ScrambledString(textBox1.Text);}
private void button4_Click(object sender, EventArgs e){
if (Application.MessageLoop){
Application.Exit();}
else{
Environment.Exit(1);}}}
}
In the Class module...
private string AllScrChrs
: The scrambler characters. You can always change the amount of the characters to make your code more complicated or less complicated.
private string AllScrChrs =
"!@#$%&*abcdefghiklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ1234567890";
There are three public
methods in the class:
-
public string BinaryString(string MainString)
Passes the text from the textbox and returns binary string
-
public string ScrambledString(string MainString)
Passes the text from the textbox and returns scrambled string
-
public string MainString(string PassingStr, byte BinaryOrScrambled)
Passes the Binary or scrambled string, and a Boolean value to determine which one will be processed. Either Binary to Text, or Scrambled to text. If selected from scrambled, code first transfers the string to binary string, and then decodes to text.
public string BinaryString(string MainString){
return BinaryWork(MainString);}
public string ScrambledString(string MainString){
return ScrambstrBinaryext(BinaryWork(MainString));}
public string MainString(string PassingStr, byte BinaryOrScrambled){
string MainText = "";
switch (BinaryOrScrambled){
case 0:
MainText = DecodeBinary(PassingStr);
break;
case 1:
MainText = DecodeBinary((UnScrambstrBinaryext(PassingStr)));
break;}
return MainText;}
private string BinaryWork(string WhatToWorkOn)
This method is called to create binary code of the text entered.
private string BinaryWork(string WhatToWorkOn){
string BinaryResults = "";
foreach (char GetChr in WhatToWorkOn){
BinaryResults += GetBinary(GetChr);}
return BinaryResults;}
private string GetBinary(char strChr){
return Convert.ToString(strChr, 2).PadLeft(8, '0');}
private string DecodeBinary(string PassingString)
From this method, characters are created from the binary string.
private string DecodeBinary(string PassingString){
int ii;
string CharResult="";
for (ii = 0; ii < PassingString.Length; ii+=8){
try{
CharResult += GetCharacter(PassingString.Substring(ii, 8));}
catch (OverflowException) { }}
return CharResult;}
private char GetCharacter(string strBinary){
return (char)Convert.ToInt32(strBinary, 2);}
private string ScrambstrBinaryext(string ScrString)
The main scrambler. Let me just explain what this portion of the code does instead of a line-by-line tutorial.
private string OneAndZero(int Rept, string sOZ)
I used this method to enter random character with random amount of 3 to between the binary numbers. This method is also called before binary numbers start and after binary numbers end. So, not only between binary numbers and binary numbers itself, also two ends are randomly scrambled to make things more complicated.
private string ScrambstrBinaryext(string ScrString){
int rndRep;
Random intRan = new Random();
string newString = "";
string ScrChrs = AllScrChrs;
string chrOne = ScrChrs.Substring(intRan.Next(ScrChrs.Length), 1);
ScrChrs = ScrChrs.Replace(chrOne, "");
string chrZero = ScrChrs.Substring(intRan.Next(ScrChrs.Length), 1);
ScrChrs = ScrChrs.Replace(chrZero, "");
int IntStrLength = ScrString.Length;
foreach (char OZchr in ScrString){
rndRep = intRan.Next(3);
switch (OZchr){
case '1': newString += OneAndZero(rndRep, chrOne) + OneAndZero(rndRep, ";");
break;
case '0':
newString += OneAndZero(rndRep, chrZero) + OneAndZero(rndRep, ":");
break;}
newString += OneAndZero(intRan.Next(1, 3),
ScrChrs.Substring(intRan.Next(ScrChrs.Length), 1));}
return OneAndZero(intRan.Next(1, 3),
ScrChrs.Substring(intRan.Next(ScrChrs.Length), 1))
+ newString + OneAndZero(intRan.Next(3), chrOne) +
OneAndZero(intRan.Next(3), chrZero)
+ OneAndZero(intRan.Next(1, 3),
ScrChrs.Substring(intRan.Next(ScrChrs.Length), 1));}
private string OneAndZero(int Rept, string sOZ){
int ii;
for (ii = 0; ii < Rept; ii++){
sOZ += sOZ.Substring(0, 1);}
return sOZ;}
private string UnScrambstrBinaryext(string Uscr)
Descrambler. Once again, instead of going the code line by line, let me explain what this portion of the code is doing.
private string rvsString(string ReverseThis)
The most, last 9 characters of the code contain 1-extra character at the end, 2-Binary "1" 3-Binary "0" characters that we will need to replace. A very important portion of the code is finding out which characters are "1" and "0" that sit in last three characters of the scrambled text. This way, we can put any character for them each scramble calls.
private string SingleString(string MultiString, string StrFull)
All the duplicated characters will be singled.
private string UnScrambstrBinaryext(string Uscr){
string[] strOneToZero = new string[3];
string ScrChrs = AllScrChrs;
int ii;
string LastNine = rvsString(Uscr.Substring(Uscr.Length - 9));
foreach (char ChrNine in LastNine){
if (strOneToZero[0] == null){
strOneToZero[0] = ChrNine.ToString(); continue;}
if (strOneToZero[0] == ChrNine.ToString()) continue;
if (strOneToZero[1] == null){
strOneToZero[1] = ChrNine.ToString(); continue;}
if (strOneToZero[1] == ChrNine.ToString()) continue;
strOneToZero[2] = ChrNine.ToString();
break;}
ScrChrs = ScrChrs.Replace(strOneToZero[1],"");
ScrChrs = ScrChrs.Replace(strOneToZero[2], "");
for (ii = 0; ii < ScrChrs.Length; ii++){
Uscr = Uscr.Replace(ScrChrs.Substring(ii,1),"");}
Uscr = SingleString(strOneToZero[1], Uscr);
Uscr = SingleString(strOneToZero[2], Uscr);
Uscr = Uscr.Replace(";", "");
Uscr = Uscr.Replace(":", "");
Uscr = Uscr.Replace(strOneToZero[1], "0");
Uscr = Uscr.Replace(strOneToZero[2], "1");
return Uscr.Substring(0,(Uscr.Length-2));}
private string SingleString(string MultiString, string StrFull){
while (StrFull.IndexOf(MultiString + MultiString) != -1){
StrFull = StrFull.Replace(MultiString + MultiString, MultiString);}
return StrFull;}
private string rvsString(string ReverseThis){
string rvSt = "";
int ii;
for (ii = (ReverseThis.Length-1); ii > 0; ii--){
rvSt += ReverseThis.Substring(ii, 1);}
return rvSt;}
This is a very simple code, with simple text and character work. It was a study/practice code which I found interesting enough to publish. I hope you find it interesting as well. :)