title: c aes256 padding with pkcs#7 standard
body: <p>encrypt:</p>
  
  <pre><code>while(!feof(fp)){
      memset(plain_text, 0, sizeof(plain_text));
      retval = fread(plain_text, 1, 16, fp);
      if(!retval) break;
      for(i=0; i&lt;16; i++){
          if(plain_text[i] == 0){
              for(j=i; j&lt;16; j++){
                  plain_text[j] = 0x0i;
              }
              break;
          }
      }
      gcry_cipher_encrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
      fwrite(encBuffer, 1, 16, fpout);
  }
  </code></pre>
  
  <p>decrypt:</p>
  
  <pre><code>while(!feof(fp)){
      memset(plain_text, 0, sizeof(plain_text));
      retval = fread(plain_text, 1, 16, fp);
      if(!retval) break;
      gcry_cipher_decrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
      for(i=0; i&lt;16; i++){
          if(encBuffer[i] == 0x0i){
              j = 0;
              j += i;
              if(encBuffer[++i] == 0x0j){
                  last = 1;
                  i--;
                  j=i;
                  printf("found a %d\n", i);
                  break;
              }
              else i--;
          }
      }
      //printf("%d\n", j);
      if(last == 1) fwrite(encBuffer, 1, j, fpout);
      else fwrite(encBuffer, 1, 16, fpout);
  }
  </code></pre>
  
  <p>i'm trying to add the removable padding from the pkcs#7 standard but i have a problem.<br>
  If i work with txt files my program work perfectly but if i try to <strong>decrypt</strong> some tar.gz or pdf file the decrypt program stop at half of file size!<br>
  For example let's take a tar.gz archive which size is 28272 bytes (prova is the original file, out is the encrypted file and origdec is the decrypted file):<br></p>
  
  <pre><code>28272   prova
  28272   out
  12147   origdec
  </code></pre>
  
  <p>i'm using libgcrypt on gnu/linux!</p>