Click here to Skip to main content
13,295,643 members (53,026 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 5 May 2010


, 6 May 2010
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.


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(){

        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
                // Use this since we are a console app

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 = 

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);
        case 1:
            MainText = DecodeBinary((UnScrambstrBinaryext(PassingStr)));

    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){
         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, ";");
            case '0':
                newString += OneAndZero(rndRep, chrZero) + OneAndZero(rndRep, ":");
        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();
    // 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. :)


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Fabrice Vergnenegre17-May-10 14:04
memberFabrice Vergnenegre17-May-10 14:04 
GeneralRe: My vote of 1 Pin
Leo Koach17-May-10 18:34
memberLeo Koach17-May-10 18:34 
GeneralMy vote of 2 Pin
ArchAngel12311-May-10 4:38
memberArchAngel12311-May-10 4:38 
GeneralRe: My vote of 2 Pin
Leo Koach11-May-10 6:13
memberLeo Koach11-May-10 6:13 
General:) Pin
Leo Koach7-May-10 10:43
memberLeo Koach7-May-10 10:43 
GeneralRe: :) Pin
Leo Koach7-May-10 10:47
memberLeo Koach7-May-10 10:47 
QuestionRegarding the codes I have tried so far... Pin
Leo Koach6-May-10 5:28
memberLeo Koach6-May-10 5:28 
AnswerRe: Regarding the codes I have tried so far... Pin
haindl6-May-10 6:01
memberhaindl6-May-10 6:01 
GeneralRe: Regarding the codes I have tried so far... Pin
Leo Koach6-May-10 7:48
memberLeo Koach6-May-10 7:48 
GeneralRe: Regarding the codes I have tried so far... Pin
Leo Koach7-May-10 17:52
memberLeo Koach7-May-10 17:52 
GeneralFunny Pin
Jordy "Kaiwa" Ruiter5-May-10 23:23
memberJordy "Kaiwa" Ruiter5-May-10 23:23 
GeneralRe: Funny Pin
Leo Koach5-May-10 23:31
memberLeo Koach5-May-10 23:31 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 1:05
memberJordy "Kaiwa" Ruiter6-May-10 1:05 
AnswerRe: Funny Pin
haindl6-May-10 3:41
memberhaindl6-May-10 3:41 
GeneralRe: Funny [modified] Pin
Jordy "Kaiwa" Ruiter6-May-10 3:47
memberJordy "Kaiwa" Ruiter6-May-10 3:47 
GeneralRe: Funny Pin
haindl6-May-10 4:30
memberhaindl6-May-10 4:30 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 4:34
memberJordy "Kaiwa" Ruiter6-May-10 4:34 
GeneralRe: Funny Pin
haindl6-May-10 4:50
memberhaindl6-May-10 4:50 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 4:52
memberJordy "Kaiwa" Ruiter6-May-10 4:52 
GeneralRe: Funny Pin
Leo Koach6-May-10 7:50
memberLeo Koach6-May-10 7:50 
GeneralRe: Funny Pin
Jordy "Kaiwa" Ruiter6-May-10 8:00
memberJordy "Kaiwa" Ruiter6-May-10 8:00 
QuestionWhat's the point? Pin
supercat95-May-10 12:46
membersupercat95-May-10 12:46 
AnswerRe: What's the point? Pin
Leo Koach5-May-10 13:07
memberLeo Koach5-May-10 13:07 
GeneralAn improvement Pin
Sotaio5-May-10 12:40
memberSotaio5-May-10 12:40 
GeneralRe: An improvement Pin
Leo Koach5-May-10 13:13
memberLeo Koach5-May-10 13: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.

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