<div dir="ltr">Hello all,<div><br></div><div>I am attempting to solve Cryptopals Challenge 10 (<a href="http://cryptopals.com/sets/2/challenges/10">http://cryptopals.com/sets/2/challenges/10</a>) using OpenSSL. Here's my code:</div><div><br></div><div>-----</div><div><br></div><div><div>#include <openssl/evp.h></div><div>#include <openssl/bio.h></div><div><br></div><div>enum {</div><div><span class="" style="white-space:pre">   </span>BLKSIZ = 16</div><div>};</div><div><br></div><div>int</div><div>main(void)</div><div>{</div><div><span class="" style="white-space:pre">   </span>unsigned char in[BLKSIZ], out[BLKSIZ], vec[BLKSIZ],</div><div><span class="" style="white-space:pre">                </span>key[] = "YELLOW SUBMARINE";</div><div><span class="" style="white-space:pre">      </span>int i, j;</div><div><span class="" style="white-space:pre">  </span>EVP_CIPHER_CTX ctx;</div><div><span class="" style="white-space:pre">        </span>BIO *bio, *b64, *bio_out;</div><div><br></div><div><span class="" style="white-space:pre"> </span>EVP_CIPHER_CTX_init(&ctx);</div><div><span class="" style="white-space:pre">     </span>EVP_CIPHER_CTX_set_padding(&ctx, BLKSIZ);</div><div><span class="" style="white-space:pre">      </span>EVP_DecryptInit_ex(&ctx, EVP_aes_128_ecb(), NULL, key, NULL);</div><div><br></div><div><span class="" style="white-space:pre"> </span>b64 = BIO_new(BIO_f_base64());</div><div><span class="" style="white-space:pre">     </span>bio = BIO_new_fp(stdin, BIO_NOCLOSE);</div><div><span class="" style="white-space:pre">      </span>bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);</div><div><span class="" style="white-space:pre"> </span>BIO_push(b64, bio);</div><div><br></div><div><span class="" style="white-space:pre">       </span>for (i = 0; i < BLKSIZ; i++)</div><div><span class="" style="white-space:pre">            </span>vec[i] = '\x00';</div><div><span class="" style="white-space:pre">   </span>while ((i = BIO_read(b64, in, BLKSIZ)) > 0) {</div><div><span class="" style="white-space:pre">           </span>EVP_DecryptUpdate(&ctx, out, &j, in, i);</div><div><span class="" style="white-space:pre">           </span>for (i = 0; i < j; i++) {</div><div><span class="" style="white-space:pre">                       </span>printf("\nin[%d]:  %02x\nout[%d]: %02x\nvec[%d]: %02x\n", i, in[i], i, out[i], i, vec[i]);</div><div><span class="" style="white-space:pre">                      </span>out[i] ^= vec[i];</div><div><span class="" style="white-space:pre">                  </span>vec[i] = in[i];</div><div><span class="" style="white-space:pre">            </span>}</div><div><span class="" style="white-space:pre">          </span>BIO_write(bio_out, out, j);</div><div><span class="" style="white-space:pre">        </span>}</div><div><span class="" style="white-space:pre">  </span>EVP_DecryptFinal_ex(&ctx, out, &j);</div><div><span class="" style="white-space:pre">        </span>for (i = 0; i < j; i++)</div><div><span class="" style="white-space:pre">         </span>out[i] ^= vec[i];</div><div><span class="" style="white-space:pre">  </span>BIO_write(bio_out, out, j);</div><div><span class="" style="white-space:pre">        </span>BIO_flush(bio_out);</div><div><br></div><div><span class="" style="white-space:pre">       </span>return 0;</div><div>}</div></div><div><br></div><div>-----</div><div><br></div><div>If I change the cipher type to EVP_aes_128_cbc() and remove the XOR, decryption seems to work correctly. However, as written what happens is that starting from the second block, each decrypted byte is the same as the corresponding ciphertext byte from the previous block. So when I XOR the two, I get a null byte. This means only the first 16 characters print out correctly followed by lots of '\0'.</div><div><br></div><div>Can someone clue me in as to what is going on here? Would appreciate the help.</div></div>