|
Encryption
After the door opens, you rush into the secret base like a
well-organized special ops unit. However as soon as you entered you
notice that the place is completely empty apart from some pizza boxes
and soda cans (but hey...it is at least 50 Ft each, so totally worth
it). The terrorists left the place days ago and tried to remove every
clue as to where. However at the back of one of the pizza boxes you
notice some kind of a cipher which can maybe help. Your task is to
break the encryption!
Input
The input contains two lines, each terminated by an end of line
character:
- The first line is a string of arbitrary length consisting of the small and capital
letters of the english alphabet and the space character (total of
53 possible characters).
- The second line contains 4 characters out of the 53 possible,
separated by a single space character.
Output
The output is a single line which is the decrypted text.
Rules
The creation of the encrypted (input) text is the following:
- We number the possible 53 characters by:
a=0,b=1,...,z=25,A=26,B=27,...,Z=51," "=52.
- We multiply each character (as a number) of the original
message by the same K number and then add the
same L number to it. Finally we reduce the
achieved n number mod 53, which in programming
simply means n%53.
Example: if K=3 and L=4, then from x=23 we get 3*23 + 4
= 73%53 = 20 = "u".
To get back the decrypted text (which is the required output) from
the encrypted text (which is the first line of the input) we do the
following:
- We subtract L from each character (as
number) of the encrypted (input) text.
- Then we "divide" this number by K. However
since every operation must be performed with integers between 0
and 52, then the division is not the classical division. The
so-called "modular division" is done by first finding the
number K' such that (K*K')%53 = 1. Then the
division by K is equivalent to the multiplication by K',
reduced of course modulo 53 (in programming %53).
Example: at the above example we can get back "x"
from "u=20" by:
- "u"-L = 20-L = 16
- K' = 18, since (K*K')%53 = (3*18)%53 = 54%53 = 1
- So ((20-L)*18)%53 = (16*18)%53 = 288%53 = 23 = "x"
The only problem with the above algorithm is that we have no idea
what are the values of K and L.
However based on the second line of the input, we can calculate
them!
- The second line of the input contains four characters,
which give us 2 decrypted-encrypted character pairs.
- For example the "h n A z" line means that
the unencrypted "h" has the
encrypted pair of "n". Similarly the
unencrypted "A" has the
encrypted pair of "z".
- Based on this information we can calculate K and L by using
a "system of equations", since the encryption is basically K*x
+ L = y, where x and y are the given unencrypted-encrypted pair
and K and L are the unknowns. Since we have 2
unencrypted-encrypted letter pairs and we also have 2 unknowns,
we can solve this system of equation. The only difficulty is
that now every calculation is done modulo 53, but apart from
the division (see above), every other mathematical operation is
done exactly the same way as in the regular integers. We simply
need to reduce the result by %53.
Example
|
|