Click here to Skip to main content
Click here to Skip to main content

Scrambler

, 6 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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. Wink | ;)

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){
            // 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";

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': // 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;}
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){

    // 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. Smile | :)

License

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

Share

About the Author

Leo Koach
Engineer Koach Software
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberFabrice Vergnenegre17-May-10 13:04 
GeneralRe: My vote of 1 PinmemberLeo Koach17-May-10 17:34 
GeneralMy vote of 2 PinmemberArchAngel12311-May-10 3:38 
GeneralRe: My vote of 2 PinmemberLeo Koach11-May-10 5:13 
General:) PinmemberLeo Koach7-May-10 9:43 
GeneralRe: :) PinmemberLeo Koach7-May-10 9:47 
This is a looong scrambled message... actual message is not that long though. Let's see if anyone would decode this Smile | :) All you have to do is
 
1-Download and run the code
2-paste the scrambled text into "Scrambled string" box and click "Scramble to text" button.
 
Leo
QuestionRegarding the codes I have tried so far... PinmemberLeo Koach6-May-10 4:28 
AnswerRe: Regarding the codes I have tried so far... Pinmemberhaindl6-May-10 5:01 
GeneralRe: Regarding the codes I have tried so far... PinmemberLeo Koach6-May-10 6:48 
GeneralRe: Regarding the codes I have tried so far... PinmemberLeo Koach7-May-10 16:52 
GeneralFunny PinmemberJordy "Kaiwa" Ruiter5-May-10 22:23 
GeneralRe: Funny PinmemberLeo Koach5-May-10 22:31 
GeneralRe: Funny PinmemberJordy "Kaiwa" Ruiter6-May-10 0:05 
AnswerRe: Funny Pinmemberhaindl6-May-10 2:41 
GeneralRe: Funny [modified] PinmemberJordy "Kaiwa" Ruiter6-May-10 2:47 
GeneralRe: Funny Pinmemberhaindl6-May-10 3:30 
GeneralRe: Funny PinmemberJordy "Kaiwa" Ruiter6-May-10 3:34 
GeneralRe: Funny Pinmemberhaindl6-May-10 3:50 
GeneralRe: Funny PinmemberJordy "Kaiwa" Ruiter6-May-10 3:52 
GeneralRe: Funny PinmemberLeo Koach6-May-10 6:50 
GeneralRe: Funny PinmemberJordy "Kaiwa" Ruiter6-May-10 7:00 
QuestionWhat's the point? Pinmembersupercat95-May-10 11:46 
AnswerRe: What's the point? PinmemberLeo Koach5-May-10 12:07 
GeneralAn improvement PinmemberSotaio5-May-10 11:40 
GeneralRe: An improvement PinmemberLeo Koach5-May-10 12:13 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 6 May 2010
Article Copyright 2010 by Leo Koach
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid