Social networks are becoming increasingly used as a medium of communication. Adam and some of his geek friends, intensive users of these tools, decided to exchange messages amongst themselves using their preferred social network, but they do not want that anyone outside their geek circle understands the messages. For that purpose, they invented a new cryptographic algorithm, with which they will encipher their messages, so that only friends holding the key used for the cipher can decipher the messages.
Since the numeric codes of the characters are in the range [0..255], they decided to use the following function to scramble the numeric values involved in the computations:
F:[0..255] → [0..255] |
|
In these expressions the plus sign represents addition modulus 256, and likewise for the minus sign. For example F(6) = 252, F(253) = 1.
The result of encrypting a string of characters x= (x1, x2, …, xm) is a string of integers y= (y1, y2, …, ym), computed as follows:
y1 = F(G(x1) ⊕ key) |
yi+1 = F(G(xi+1) ⊕ yi) |
In these expressions G(c) stands for the numeric value of the character c, a value in the range [0..255], ⊕ represents the bitwise exclusive or (the XOR operation in computer speak), and key is the secret key, also a value in the range [0..255].
The final step of the encrypting consists in creating a string of characters (z1, z2, …, z2n) where the pair of characters, z2i−1, z2i is the hexadecimal representation of the yi. This hexadecimal string is then posted on the social network.
To exemplify the process let us suppose that Adam would like to cipher the following word “Lisboa” and uses the key 103. Then, applying the ciphering function he obtains the string of integers y= (47,60,83,53,80,53). Finally, the byte string is transformed in its hexadecimal representation “2f3c53355035” which could be posted.
Your task is to create an application that converts the ciphered text back into the original text. To achieve this, you should reverse the encryption process.
The input has 2 lines. The first line represents the key. The second line contains the ciphered text in hexadecimal notation.
The output consists in a single line with the original text.
103 2f3c53355035
Lisboa
0 385b30064c345f2d4267
Boa Prova!
142 cfb2bce1c596acdda6cbef8d03582f4056187b0f3336572a4e2f5f
Esta e uma frase top secret
This document was translated from LATEX by HEVEA.