Rail Fence Cipher Algorithm Program in C/C++

In cryptography, a cipher (or cypher) is an algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure. An alternative, less common term is encipherment. To encipher or encode is to convert information into cipher or code. In common parlance, “cipher” is synonymous with “code“, as they are both a set of steps that encrypt a message; however, the concepts are distinct in cryptography, especially classical cryptography.

Codes generally substitute different length strings of character in the output, while ciphers generally substitute the same number of characters as are input. There are exceptions and some cipher systems may use slightly more, or fewer, characters when output versus the number that was input.

In this post, we will discuss the Rail Fence Cipher. The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded. In the rail fence cipher, the plain text is written downwards and diagonally on successive “rails” of an imaginary fence, then moving up when the bottom rail is reached. When the top rail is reached, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows. For example, if 3 “rails” and the message “HELLOWORLD” is used, the cipherer writes out:

H . . . O . . . L .
. E . L . W . R . D
. . L . . . O . . . 

Then reads off to get the ciphertext:

HOLELWRDLO

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Rail Fence Cipher algorithm in C++, although, it’s very similar to C.

Encryption

INPUT:
line 1: message
line 2: key

OUTPUT:
line 1: Encrypted message

The following is the Rail Fence Cipher encryption algorithm program in C++.

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t,n,m,i,j,k,sum=0;
    string s;
    cout<<"Enter the message"<<'\n';
    cin>>s;
    cout<<"Enter key"<<'\n';
    cin>>n;
    vector<vector<char>> a(n,vector<char>(s.size(),' '));
    j=0;
    int flag=0;
    for(i=0;i<s.size();i++){
        a[j][i] = s[i];
         if(j==n-1){
            flag=1;
        }
        else if(j==0)
            flag=0;

        if(flag==0){
            j++;
        }
        else j--;
    }
    for(i=0;i<n;i++){
        for(j=0;j<s.size();j++){
            if(a[i][j]!=' ')
                cout<<a[i][j];
        }
    }
    cout<<'\n';   
    return 0;
}

OUTPUT:

Enter the message
HELLOWORLD 
Enter key
3
HOLELWRDLO

Decryption

INPUT:
line 1: message
line 2: key

OUTPUT:
line 1: decrypted message

The following is the Rail Fence Cipher decryption algorithm program in C++.

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t,n,m,i,j,k,sum=0;
    string s;
    cout<<"Enter the message to decrypt"<<'\n';
    cin>>s;
    cout<<"Enter key"<<'\n';
    cin>>n;

    vector<vector<char>> a(n,vector<char>(s.size(),' '));

    j=0;
    int flag=0;
    for(i=0;i<s.size();i++){
        a[j][i] = '0';
         if(j==n-1){
            flag=1;
        }
        else if(j==0)
            flag=0;

        if(flag==0){
            j++;
        }
        else j--;
    }
    int temp =0;
    for(i=0;i<n;i++){
        for(j=0;j<s.size();j++){
                if(a[i][j]=='0')
                    a[i][j]= s[temp++];
        }
    }
    flag=0;
    j=0;
    for(i=0;i<s.size();i++){
        cout<<a[j][i];
         if(j==n-1){
            flag=1;
        }
        else if(j==0)
            flag=0;

        if(flag==0){
            j++;
        }
        else j--;
    }
    cout<<'\n';    
    return 0;
}

OUTPUT:

Enter the message to decrypt
HOLELWRDLO
Enter key
3
HELLOWORLD

Let us know in the comments if you are having any questions regarding this cryptography cipher Algorithm.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Default image
Jazib
Android Developer | Competitive Programmer

Leave a Reply