Scrambler in C#


Scrambler!

scrambler1.gif

Introduction:

Probably all of us tried to write a code to scramble text, so we could pass secret messages around. C# has encrypting ability built in but it's not fun to use something already is there. I had some extra(!) time last couple of days, so I wrote my own text scrambler.

This program gets you different output each time... very difficult to crack. You have to know where to look, I don't think anyone who doesn't know the code could can find the encrypted text in the scrambled version, but who knows... Some of us very smart ;)

The Idea:

This program has two section of scrambling. First section, it transfers text into binary code. The scrambler is a class, so I think it should work as fast as a normal encryptor. 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 no one would actually know which character set 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){
            // Exit the program
            if (Application.MessageLoop){
                // Use this since we are a WinForms app
                Application.Exit();}
            else{
                // Use this since we are a console app
                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";

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 determen which one will be processed. Either Binary to Text, or Scrambled to text. If from scrambled selected, 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 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 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 doing instead of line-by-line tutorial.

private string OneAndZero(int Rept, string sOZ): I used this method to enter random charcter with random amount of 3 to between the binary numbers. This method 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': // I wrote a method to make things simpler.
                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));}
    // When return, the first and the last character is random to confuse people. Before that two characters are our guys
    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));}

// This method called a few times to enter random amount of characters
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 contains 1-extra character at the end, 2-Binary "1" 3-Binary "0" characters that we will need to replace. Very important portion of the code is the finding out which characters are "1" and "0" which sits 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){
    // Cut last 9 characters of the text
    // Last 9 characters contain the extra character with 1 & 0 characters
    string[] strOneToZero = new string[3];
    string ScrChrs = AllScrChrs;
    int ii;
    string LastNine = rvsString(Uscr.Substring(Uscr.Length - 9));
    // A unique way to find unique characters once ;)
    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;}
    // We need array "1" and "2". "0" is extra
    ScrChrs = ScrChrs.Replace(strOneToZero[1], "");
    ScrChrs = ScrChrs.Replace(strOneToZero[2], "");
    for (ii = 0; ii < ScrChrs.Length; ii++){
        Uscr = Uscr.Replace(ScrChrs.Substring(ii, 1), "");}
    // I wrote a method to make things simpler.
    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));} // Last two was our guys remember?

// Replace dublicate characters with single
private string SingleString(string MultiString, string StrFull){
    while (StrFull.IndexOf(MultiString + MultiString) != -1){
        StrFull = StrFull.Replace(MultiString + MultiString, MultiString);}
    return StrFull;}

// I wrote this reverser for the last 9 characters of the scrambled text
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 anough to publis it. I hope you find it interesting as well :)


Recommended Free Ebook
Similar Articles