Today your goal is to decrypt a secret message using AES. Given a ciphertext and key you will decrypt and print the message.

  1. Your program may be in any language. It will be run with input on stdin, and its output on stdout will be checked for correctness.

  2. The first line of input on stdin will be the 16 byte key, encoded in hexadecimal. The second line of input on stdin will be the 16 byte ciphertext, encoded in hexadecimal.

  3. The output of the program must be the 16 byte message after decrypting the ciphertext using AES-128 with the given key. You must output the result interpreted as ASCII octets. You may assume that any result is valid ASCII after decrypting.

  4. You may not use any library/built-in features that implement AES. You may use such features to convert between hex/binary/ASCII encodings.

Shortest code in bytes wins.

Example input and output:


Attack tomorrow.

And another:




1 Answer 1


Python, 661 chars

I=lambda x:[int(x[2*i:2*i+2],16)for i in R(16)]
for i in R(512):P+=[x];x^=x*2^(x>>7)*0x11b
S=[255&(99^j^(j>>4)^(j>>5)^(j>>6)^(j>>7))for i in R(256)for j in[(P[255-P.index(i)]if i else 0)*257]]
for r in R(10):
 for i in R(4):k+=[k[-16]^S[k[-3-(i>2)*4]]]
 for i in R(12):k+=[k[-16]^k[-4]]
for r in R(11):
 c=[x^y for x,y in zip(k[160-16*r:],c)]
 if r>9:break
 for i in[0,4,8,12]*(r>0):c[i:i+4]=[x^y^z^w for j in R(4)for x,y,z,w in[[P[a+P.index(v)]if v else 0 for a,v in zip((2*[104,238,199,223])[3-j:],c[i:i+4])]]]

k is the key, c is the ciphertext. I build P, the powers of 3 in the field, then S, the sbox. Then k is extended with the key schedule. Finally we do the AES decryption. Mixcolumns is the hard phase, all the other phases are pretty straightforward.

  • \$\begingroup\$ Maybe you should also do one in Pyth, otherwise I'll guarantee you someone will sweep along and translate it, beating you :) \$\endgroup\$
    – orlp
    Mar 23, 2015 at 0:46
  • \$\begingroup\$ As a quick test I generated another testcase, but your solution fails for it. I added the second testcase to the question so you can debug. \$\endgroup\$
    – orlp
    Mar 23, 2015 at 4:34
  • \$\begingroup\$ @orip: fixed. It was a bug in multiplying by zero. \$\endgroup\$ Mar 23, 2015 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.