Note that there are some explanatory texts on larger screens.

plurals
  1. POECB, CFB, OFB cipher modes in Openssl
    text
    copied!<p>I know that when I use CBC mode with Openssl, I can give as an input which is a multiple of a block size. But how about other modes? ECB, CFB, OFB? I saw a <a href="https://www.openssl.org/docs/crypto/des_modes.html" rel="nofollow">doc</a> but its not all clear to me. Should I call them in a loop? </p> <p>Lets say, ECB. It encrypts 64 bits at a time. So a pseudocode would look like this (should look like this)?</p> <pre><code>int len = 512, c = 0; unsigned char in[len], out[len]; while(c &lt; len) { Aes_ecb_encrypt(in+c, out+c, &amp;enckey, AES_ENCRYPT); c += 8; } </code></pre> <p>But with the above code it doesnt encrpyt good. When I change <code>c += 8;</code> into <code>c += 16;</code> its ok then. Whats is good way of doing this? I mean, we all know that 8x8 = 64 bits so this should be correct, but it isnt, the encryption/decryption is working only when I have <code>c += 16;</code>. </p> <p>What about other cipher modes?</p> <p>Sample for ECB mode (notice, that the question is also about other modes too;)):</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;openssl/aes.h&gt; #include &lt;openssl/rand.h&gt; // a simple hex-print routine. could be modified to print 16 bytes-per-line static void hex_print(const void* pv, size_t len) { const unsigned char * p = (const unsigned char*)pv; if (NULL == pv) printf("NULL"); else { size_t i = 0; for (; i&lt;len;++i) printf("%02X ", *p++); } printf("\n"); } // main entrypoint int main(int argc, char **argv) { int keylength = 256; unsigned char aes_key[keylength/8]; memset(aes_key, 0, keylength/8); if (!RAND_bytes(aes_key, keylength/8)) exit(-1); size_t inputslength = 0; printf("Give an input's length:\n"); scanf("%lu", &amp;inputslength); /* generate input with a given length */ unsigned char aes_input[inputslength]; memset(aes_input, 'X', inputslength); // buffers for encryption and decryption const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; unsigned char paddedinput[encslength]; memset(paddedinput, 0, encslength); memcpy(paddedinput, aes_input, inputslength); unsigned char enc_out[encslength]; unsigned char dec_out[inputslength]; memset(enc_out, 0, sizeof(enc_out)); memset(dec_out, 0, sizeof(dec_out)); AES_KEY enc_key, dec_key; AES_set_encrypt_key(aes_key, keylength, &amp;enc_key); long c = 0; while(c &lt; encslength) { AES_ecb_encrypt(paddedinput+c, enc_out+c, &amp;enc_key, AES_ENCRYPT); c += 8; } c = 0; AES_set_decrypt_key(aes_key, keylength, &amp;dec_key); while(c &lt; encslength) { AES_ecb_encrypt(enc_out+c, dec_out+c, &amp;dec_key, AES_DECRYPT); c += 8; } printf("original:\t"); hex_print(aes_input, sizeof(aes_input)); printf("encrypt:\t"); hex_print(enc_out, sizeof(enc_out)); printf("decrypt:\t"); hex_print(dec_out, sizeof(dec_out)); return 0; } </code></pre>
 

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