Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read first 256 bits from a file to store them in two arrays
    primarykey
    data
    text
    <p>I am coding a cipher, for which key file is 256 bit long. Out of which 128 bits have to go in key and 128 into IV. I am explicitly defining the sizes of those arrays and then reading, and still output I get is Key having like 148 bits set while IV has lesser thus. and in the end it gives stack smashing error.</p> <pre><code>int main(int argc,char *argv[]) { if(argc &lt; 4 || !argv[1] || !argv[2]) { cout &lt;&lt; "Usage: Encoding: " &lt;&lt; argv[0] &lt;&lt; " [plaintext-file] [key-file] [cipher-file]" &lt;&lt; endl; cout &lt;&lt; "Usage: Decoding: " &lt;&lt; argv[0] &lt;&lt; " [cipher-file] [key-file] [plaintext-file]" &lt;&lt; endl; return 0; } unsigned char key[16], iv[16]; unsigned long i; // read input file and size etc FILE* in_file = fopen(argv[1],"rb"); if(!in_file) { cout &lt;&lt; "Error opening read file" &lt;&lt; endl; return 0; } fseek(in_file,0,SEEK_END); unsigned int msglength = ftell(in_file); cout &lt;&lt; "Msglength:" &lt;&lt; msglength &lt;&lt; endl; unsigned char * mem_ptr = (unsigned char*)malloc(msglength); if(!mem_ptr) { cout &lt;&lt; "Error allocating memory" &lt;&lt; endl; fclose(in_file); return 0; } rewind(in_file); fread(mem_ptr,1,msglength,in_file); fclose(in_file); // read keyfile etc FILE* key_file = fopen(argv[2],"rb"); if(!key_file) { cout &lt;&lt; "Error opening key file" &lt;&lt; endl; return 0; } fseek(key_file,0,SEEK_END); unsigned int key_size = ftell(key_file); cout &lt;&lt; "Key Size:" &lt;&lt; key_size &lt;&lt; endl; unsigned char * key_ptr = (unsigned char*)malloc(key_size); if(!key_ptr) { cout &lt;&lt; "Error allocating memory" &lt;&lt; endl; fclose(key_file); return 0; } rewind(key_file); fread(key_ptr,1,key_size,key_file); cout &lt;&lt; "sizeof key"&lt;&lt;sizeof(key)&lt;&lt;" sizeof iv" &lt;&lt; sizeof(iv) &lt;&lt; endl; for (i=0; i&lt;256; i++) { if (i&lt;128) key[i] = key_ptr[i]; else iv[i-128] = key_ptr[i]; } cout &lt;&lt; key_ptr &lt;&lt; endl; cout &lt;&lt; "Key:" &lt;&lt; key &lt;&lt; endl &lt;&lt; "IV:" &lt;&lt; iv &lt;&lt; endl; } </code></pre> <p>Output</p> <pre><code>Msglength:15 Key Size:257 sizeof key16 sizeof iv16 abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678 Key:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678 IV:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678 *** stack smashing detected ***: ./a.out terminated Segmentation fault (core dumped) </code></pre> <p>Any idea where am I going wrong?</p>
    singulars
    1. This table or related slice is empty.
    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