# Find the cipher algorithm and decode the message

Sam is a computer expert. He works in a Security Company. His son Tim just turned thirteen and is new to C++ programming and ciphertexts . He decides to write a C++ program to make a cipher algorithm. His program has some limitations and can encrypt only one word at a time. He encrypts a message using his program and sends that message as an email to his Dad. Below is the coded message:-

Hj. i(o pmc|msm sjpfi mptqmsm5 Wigq wjno ypw gfv bben hpoh2 Ao{ze~y3 cbp ypw bskqk mf spohxmouo oo ypwu wb{ bben hpoh2

Here's the link to the program he wrote. Password is suinegmai (or something like that :P )

Can you decrypt the message, find the encryption algorithm and if possible write a C++ Program similar to his?

NOTE:- For Non-Windows Users - Download DosBox to run it. If you are not good with command line, then download a GUI frontend such as DappleGray for MacOSX.

• I got the file, but I can't run it. It says that it's because I'm on a 64-bit system, so would you mind making one to fit 64-bit? – MisterEman22 Jun 16 '15 at 4:13
• I ended up just downloading DosBox and running it through that – MisterEman22 Jun 16 '15 at 4:42
• Nice one. I've made a very similar cipher in this puzzle. – BmyGuest Jun 16 '15 at 9:43

The text decodes to:

Hi, i'm playing since morning. When will you get back home. Anyways, can you bring me something on your way back home.

Explanation:

First, to get the encryption program, you have to use the link given, and the password "iamgenius" which is "suinegmai" backwards. Once you have the program, you can figure out that the encryption process is to take each character of the word and increment its ASCII value by the index it is at. The first character is incremented by 0, second incremented by 1, etc. For example, "AAA" goes to "ABC". A + 0 = A, A + 1 = B, A + 2 = C. The way to decode this would be just decreasing the value of the character by its index value in the word.

I wrote up a java program to do this for me (sorry I don't know C++):


import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

while (true) {
System.out.print("Enter a sentence to decode, or nothing to exit:");
String sentence = s.nextLine();
String decoded = "";
if (sentence.length() == 0) { break; }

String[] words = sentence.split(" ");

for (String word : words) {
decoded += decode(word) + " ";
}
System.out.println("This decoded is: " + decoded);

}
}

public static String decode(String word) {
String decoded = "";

for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
c -= i;
decoded += Character.toString(c);
}
return decoded;
}
}


• The algorithm (idea) is actually the same as in this puzzle of mine. – BmyGuest Jun 16 '15 at 9:41

Although MisterEman22's answer is completely right, here is a C++ version:

#include <iostream>
#include <string>

bool is_whitespace(const char c){
return c == ' ' || c == '\n' || c == '\t';
}

std::size_t decode_word_inplace(std::string &sentence, std::size_t wordstart){
for(std::size_t i = wordstart; i < sentence.size(); ++i){
if(is_whitespace(sentence[i])){
return i;
}
sentence[i] -= (i - wordstart);
}
return sentence.size() - 1;
}

void decode_inplace(std::string &sentence){
for(std::size_t i = decode_word_inplace(sentence, 0); i < sentence.size(); ++i){
if(is_whitespace(sentence[i])){
i = decode_word_inplace(sentence, i+1) - 1;
}
}
}

int main(){
std::string sentence;
std::getline(std::cin, sentence);
decode_inplace(sentence);
std::cout << sentence << std::endl;

return 0;
}