Click here to Skip to main content
11,648,094 members (62,046 online)
Click here to Skip to main content

Railfence cipher encoder

, 2 Jan 2007 CPOL 24.7K 452 3
Rate this:
Please Sign up or sign in to vote.
What it says on the tin.

Tester screenshot

Introduction

The rail fence cipher is where you have some text like joejoe and you split it on two lines like:

j e o
o j e

Now you combine the two lines and get: jeooje

Encoding

At school, I have been taught how to encode stuff into the rail fence cipher. Here's how:

/// <summary>
/// Encodes stuff
/// </summary>
/// <param name="input">The string to encode</param>
/// <returns>Encoding result</returns>
public static string Encode(string input)
{
    char[] inputc = input.ToCharArray();
    string pasA = "";
    string pasB = "";
    bool x = true;
    foreach(char ch in inputc)
    {
        if(x == true)
        {
            pasA += ch;
            x = false;
        }
        else
        {
            x = true;
            pasB += ch;
        }
    }
    return pasA + pasB;
}

Decoding

This is how to decode the stuff:

/// <summary>
/// Decodes stuff into plain text
/// </summary>
/// <param name="input">The string to decode</param>
/// <returns>The decoded result</returns>
public static string DeCode(string input)
{
    int lnl = (int)Math.Round((decimal)input.Length / 2);
    //Write Strings
    char[] ln1, ln2;
    ln1 = input.Substring(0, lnl).ToCharArray();
    ln2 = input.Substring(lnl).ToCharArray();
#if DEBUG
    Debug.WriteLine("Line 1: " + input.Substring(0, lnl));
    Debug.WriteLine("Line 2: " + input.Substring(lnl, input.Length - lnl));
#endif
    //Writen strings, now seperate into one
    bool top = true;
    string output = "";
    int x, topx, botx;
    x = 1; topx = 0; botx = 0;
    while( x != input.Length + 1)
    {
        if(top == true)
        {
            if(topx > ln1.Length - 1)
            {
                return output;
            }
            output += ln1[topx]; topx += 1; top = false;
#if DEBUG
            Debug.WriteLine("topx: " + topx + " ln1 length: " + ln1.Length);
            #endif
        }
        else
        {
            output += ln2[botx]; botx += 1; top = true;
        }
        x++;
    }
    return output;
}

License

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

Share

About the Author

joejoeiscool
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionplease help Pin
Member 1003096810-May-13 19:27
memberMember 1003096810-May-13 19:27 
Frown | :( i'm downloads rail fench cipher encode.........but nt ........compiler....this error came


Error 1 Error generating Win32 resource: Error reading icon 'C:\Users\HP\Desktop\XP Folders\The Icons\Red Folder.ico' -- The system cannot find the path specified. C:\Users\HP\Desktop\New folder\Tester\obj\Debug\Tester.exe Tester



please help Wink | ;)
GeneralHelp. Pin
Andrei74-Jan-07 10:32
memberAndrei74-Jan-07 10:32 
GeneralRe: Help. Pin
joejoeiscool5-Jan-07 7:17
memberjoejoeiscool5-Jan-07 7:17 
GeneralRe: Help. Pin
Andrei76-Jan-07 4:35
memberAndrei76-Jan-07 4:35 
GeneralRe: Help. Pin
joejoeiscool8-Jan-07 6:57
memberjoejoeiscool8-Jan-07 6:57 
GeneralHelp Me Please. Pin
Andrei73-Jan-07 15:27
memberAndrei73-Jan-07 15:27 
GeneralRe: Help Me Please. Pin
joejoeiscool4-Jan-07 6:29
memberjoejoeiscool4-Jan-07 6:29 
GeneralSuggestions Pin
Jason McBurney2-Jan-07 11:07
memberJason McBurney2-Jan-07 11:07 
GeneralRe: Suggestions Pin
joejoeiscool3-Jan-07 7:02
memberjoejoeiscool3-Jan-07 7:02 
GeneralRe: Suggestions Pin
joejoeiscool4-Jan-07 6:42
memberjoejoeiscool4-Jan-07 6:42 

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 | Terms of Use | Mobile
Web03 | 2.8.150804.4 | Last Updated 2 Jan 2007
Article Copyright 2007 by joejoeiscool
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid