Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Scrambler

Rate me:
Please Sign up or sign in to vote.
3.57/5 (7 votes)
6 May 2010CPOL3 min read 78.9K   407   10   30
Create Encrypted text that changes each time for the same message!

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.

C#
{
    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.

C#
private string AllScrChrs = 
	"!@#$%&*abcdefghiklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ1234567890";

There are three public methods in the class:

  • C#
    public string BinaryString(string MainString)

    Passes the text from the textbox and returns binary string

  • C#
    public string ScrambledString(string MainString)

    Passes the text from the textbox and returns scrambled string

  • C#
    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.

C#
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;}
C#
private string BinaryWork(string WhatToWorkOn)

This method is called to create binary code of the text entered.

C#
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');}
C#
private string DecodeBinary(string PassingString)

From this method, characters are created from the binary string.

C#
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);}
C#
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.

C#
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.

C#
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 returned, the first and the last character are 
    // 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 is called a few times to enter random number of characters
private string OneAndZero(int Rept, string sOZ){
    int ii;
    for (ii = 0; ii < Rept; ii++){
        sOZ += sOZ.Substring(0, 1);}
    return sOZ;}
C#
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.

C#
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.

C#
private string SingleString(string MultiString, string StrFull)

All the duplicated characters will be singled.

C#
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 duplicate 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 enough to publish. I hope you find it interesting as well. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Koach Software
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Fabrice Vergnenegre17-May-10 13:04
Fabrice Vergnenegre17-May-10 13:04 
GeneralRe: My vote of 1 Pin
Leo Koach17-May-10 17:34
Leo Koach17-May-10 17:34 
GeneralMy vote of 2 Pin
ArchAngel12311-May-10 3:38
ArchAngel12311-May-10 3:38 
What's the point of this?
GeneralRe: My vote of 2 Pin
Leo Koach11-May-10 5:13
Leo Koach11-May-10 5:13 
General:) Pin
Leo Koach7-May-10 9:43
Leo Koach7-May-10 9:43 
GeneralRe: :) Pin
Leo Koach7-May-10 9:47
Leo Koach7-May-10 9:47 
QuestionRegarding the codes I have tried so far... Pin
Leo Koach6-May-10 4:28
Leo Koach6-May-10 4:28 
AnswerRe: Regarding the codes I have tried so far... Pin
haindl6-May-10 5:01
haindl6-May-10 5:01 
GeneralRe: Regarding the codes I have tried so far... Pin
Leo Koach6-May-10 6:48
Leo Koach6-May-10 6:48 
GeneralRe: Regarding the codes I have tried so far... Pin
Leo Koach7-May-10 16:52
Leo Koach7-May-10 16:52 
GeneralFunny Pin
Jordy "Kaiwa" Ruiter5-May-10 22:23
Jordy "Kaiwa" Ruiter5-May-10 22:23 
GeneralRe: Funny Pin
Leo Koach5-May-10 22:31
Leo Koach5-May-10 22:31 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 0:05
Jordy "Kaiwa" Ruiter6-May-10 0:05 
AnswerRe: Funny Pin
haindl6-May-10 2:41
haindl6-May-10 2:41 
GeneralRe: Funny [modified] Pin
Jordy "Kaiwa" Ruiter6-May-10 2:47
Jordy "Kaiwa" Ruiter6-May-10 2:47 
GeneralRe: Funny Pin
haindl6-May-10 3:30
haindl6-May-10 3:30 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 3:34
Jordy "Kaiwa" Ruiter6-May-10 3:34 
GeneralRe: Funny Pin
haindl6-May-10 3:50
haindl6-May-10 3:50 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 3:52
Jordy "Kaiwa" Ruiter6-May-10 3:52 
GeneralRe: Funny Pin
Leo Koach6-May-10 6:50
Leo Koach6-May-10 6:50 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 7:00
Jordy "Kaiwa" Ruiter6-May-10 7:00 
QuestionWhat's the point? Pin
supercat95-May-10 11:46
supercat95-May-10 11:46 
AnswerRe: What's the point? Pin
Leo Koach5-May-10 12:07
Leo Koach5-May-10 12:07 
GeneralAn improvement Pin
Sotaio5-May-10 11:40
Sotaio5-May-10 11:40 
GeneralRe: An improvement Pin
Leo Koach5-May-10 12:13
Leo Koach5-May-10 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.