Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@WhozCraig: thank you so much for help! It explained a lot to me! But theres just one more issue. I changed static arrays into dynamic ones. When I did it, some erros occured. But they occure only when I give a huge inputs size, take a look at valgrind output: <a href="http://pastie.org/private/bzofrrtgrlzr0doyb3g" rel="nofollow">http://pastie.org/private/bzofrrtgrlzr0doyb3g</a>. Error occurs only when I pass a huge input, when I pass a small size (like in your example, 10) its ok. Everything else is working perfectly.</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) { size_t inputslength = 0; printf("Give an input's length:\n"); scanf("%lu", &amp;inputslength); int keylength; printf("Give a key length [only 128 or 192 or 256!]:\n"); scanf("%d", &amp;keylength); // generate a key with a given length unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8)); memset(aes_key, 0, keylength/8); RAND_bytes(aes_key, keylength/8); // generate input with a given length unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength)); memset(aes_input, 'X', sizeof(aes_input)); // init vectors unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE)); unsigned char *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE)); // iv_dec == iv_enc RAND_bytes(iv_enc, AES_BLOCK_SIZE); memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); // buffers for encryption and decryption const size_t length = (((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE); unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) * (length)); unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength)); // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 AES_KEY encKey, decKey; AES_set_encrypt_key(aes_key, keylength, &amp;encKey); AES_cbc_encrypt(aes_input, enc_out, inputslength, &amp;encKey, iv_enc, AES_ENCRYPT); AES_set_decrypt_key(aes_key, keylength, &amp;decKey); AES_cbc_encrypt(enc_out, dec_out, length, &amp;decKey, iv_dec, AES_DECRYPT); 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)); free(aes_key); aes_key = NULL; free(aes_input); aes_input = NULL; free(iv_enc); iv_enc = NULL; free(iv_dec); iv_dec = NULL; free(enc_out); enc_out = NULL; free(dec_out); dec_out = NULL; return 0; } </code></pre> <p><strong>EDIT:</strong></p> <p>Ok, something was wrong with the prev code I posted, heres a new one, working perfectly, even for a huge inputs. Cheers once again for helping me!:)</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; printf("Give a key length [only 128 or 192 or 256!]:\n"); scanf("%d", &amp;keylength); /* generate a key with a given length */ unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char) * (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 = (unsigned char*)malloc(sizeof(unsigned char) *inputslength); memset(aes_input, 'X', inputslength); /* init vector */ unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE); RAND_bytes(iv_enc, AES_BLOCK_SIZE); memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE); // buffers for encryption and decryption const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) *encslength); unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *inputslength); memset(enc_out, 0, sizeof(enc_out)); memset(dec_out, 0, sizeof(dec_out)); // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256 AES_KEY enc_key, dec_key; AES_set_encrypt_key(aes_key, keylength, &amp;enc_key); AES_cbc_encrypt(aes_input, enc_out, inputslength, &amp;enc_key, iv_enc, AES_ENCRYPT); AES_set_decrypt_key(aes_key, keylength, &amp;dec_key); AES_cbc_encrypt(enc_out, dec_out, encslength, &amp;dec_key, iv_dec, AES_DECRYPT); printf("original:\t"); hex_print(aes_input, inputslength); printf("encrypt:\t"); hex_print(enc_out, encslength); printf("decrypt:\t"); hex_print(dec_out, inputslength); // free memory here return 0; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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