Home

Decode it

Cryptography is a combination of Cryptology and Cryptanalysis. Cryptology deals with the encryption and decryption of messages. Cryptanalysis deals with analyzing how the message is encrypted and cracking the logic of encryption to decrypt it. At this instant you should play the role of cryptanalyst of VISIT ;P Your task is to analyze the given messages and crack the logic or pattern behind it, then write a code to decrypt it. Analyze the below inputs.

Hint:
First, think about how they are encrypted and then go for decryption.
hello -----------> leolh
hacker --------------> rkaech
think ----------------> nhkit
twice ----------------> cweit
algorithm ----------> hiolmtrga
aaaabbbb ----------> bbaabbaa

Example:

Input:  s = "hiolmtrga"
Output: algorithm

Approach

C++

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

string decodeIt(string s)
{

    int n = s.size();
    string str = "";
    int i = n - 1;
    while (true)
    {
        if (i < 0)
        {
            if (n % 2 == 0)
                i = n - 1 + i;
            else
                i = n + i;
            str += s[i];
            i = i - (1 + n) / 2;
        }
        else
        {
            str += s[i];
            i = i - (1 + n) / 2;
        }
        if (str.size() == n)
            break;
    }
    return str;
}
int main()
{

    string s = "hiolmtrga";

    cout << decodeIt(s<< "\n";

    return 0;
}


No comments:

Post a Comment