Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is not possible (in general case, for affine cipher, see update below). That's why module operation is so frequently used in security algorithms - it is not reversible. But, why don't we try?</p> <pre><code>result = (k1 * input + k2) % 27 (*1) </code></pre> <p>Let's take the first letter:</p> <pre><code>result = (7 * 8 + 5) % 27 = 7 </code></pre> <p>That's cool. Now, because we said, that:</p> <pre><code>result = (k1 * input + k2) % 27 </code></pre> <p>the following is also true:</p> <pre><code>k1 * input + k2 = 27 * div + result (*2) </code></pre> <p>where</p> <pre><code>div = (k1 * input + k2) / 27 (integral division) </code></pre> <p>It is quite obvious (if a % b = c, then a = b*n + c, where n is the result of integer division a/b).</p> <p>You know the values of k1 (which is 7), k2 (5) and result (7). So, when we put these values to (*2), we get the following:</p> <pre><code>7 * input + 5 = 27 * div + 7 //You need to solve this </code></pre> <p>As you can see, it is impossible to solve this, because you would need to know also the result of the integral division - translating this to your function's language, you would need the value of</p> <pre><code>numberback / 27 </code></pre> <p>which is unknown. So answer is: <strong>you cannot reverse your function's results, using only output it returns</strong>.</p> <hr> <p>** UPDATE **</p> <hr> <p>I focused too much on the question's title, so the answer above is not fully correct. I decided not to remove it, however, but write an update.</p> <p>So, the answer <strong>for your particular case</strong> (affine cipher) is: YES, you can reverse it.</p> <p>As you can see on the wiki, decryption function for affine cipher for the following encrytption function:</p> <pre><code>E(input) = a*input + b mod m </code></pre> <p>is defined as:</p> <pre><code>D(enc) = a^-1 * (enc - b) mod m </code></pre> <p>The only possible problem here can be computation of a^-1, which is modular multiplicative inverse.</p> <p>Read about it on <a href="http://en.wikipedia.org/wiki/Modular_multiplicative_inverse" rel="nofollow">wiki</a>, I will provide only example.</p> <p>In your case a = k1 = 7 and m = 27. So:</p> <pre><code>7^-1 = p mod 27 7p = 1 mod 27 </code></pre> <p>In other words, you need to find p, which satisfies the following: 7p % 27 = 1. p can be computed using extended euclidean algorithm and I computed it to be 4 (4 * 7 = 28, 28 % 27 = 1).</p> <p>Check, if can decipher your output now:</p> <pre><code>E(8) = 7*8 + 5 mod 27 = 7 D(7) = 4 * (7 - 5) mod 27 = 8 </code></pre> <p>Hope that helps :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload