Encryption program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gcrypt.h>
#define GCRYPT_VERSION "1.5.0"
int main(void){
if(!gcry_check_version(GCRYPT_VERSION)){
fputs("libgcrypt version mismatch\n", stderr);
exit(2);
}
//gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
//gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
int algo = -1;
char key[32] = {0x80};
char iniVector[16] = {0};
char plain_text[16];
char *encBuffer = NULL;
const char *name = "aes256";
size_t blkLength, keyLength, txtLenght = 16, retval = 0;
blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES256);
keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES256);
FILE *fp = fopen("prova", "r+");
FILE *fpout = fopen("out", "w+");
algo = gcry_cipher_map_name(name);
encBuffer = malloc(txtLenght);
gcry_cipher_hd_t hd;
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
fseek(fp, 0, SEEK_SET);
/* AGGIUNGERE CIFRATURA DIMENSIONE FILE INIZIALE NEL FILE CIFRATO */
while(!feof(fp)){
memset(plain_text, 0, sizeof(plain_text));
retval = fread(plain_text, 1, 16, fp);
if(!retval) break;
gcry_cipher_encrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
fwrite(encBuffer, 1, 16, fpout);
}
gcry_cipher_close(hd);
free(encBuffer);
fclose(fp);
fclose(fpout);
return 0;
}
decryption program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gcrypt.h>
#define GCRYPT_VERSION "1.5.0"
int main(void){
if(!gcry_check_version(GCRYPT_VERSION)){
fputs("libgcrypt version mismatch\n", stderr);
exit(2);
}
//gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
//gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
int algo = -1;
char key[32] = {0x80};
char iniVector[16] = {0};
char plain_text[16];
char *encBuffer = NULL;
const char *name = "aes256";
size_t blkLength, keyLength, txtLenght = 16, retval = 0;
blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES256);
keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES256);
FILE *fp = fopen("out", "r+");
FILE *fpout = fopen("origdec", "w+");
algo = gcry_cipher_map_name(name);
encBuffer = malloc(txtLenght);
gcry_cipher_hd_t hd;
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
fseek(fp, 0, SEEK_SET);
/* MIGLIORARE NOME VARIABILI E AGGIUNGERE CIFRATURA DIMENSIONE FILE INIZIALE NEL FILE CIFRATO */
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);
fwrite(encBuffer, 1, 16, fpout); // così il file di output sarà della stessa dimensione di quello di input
}
gcry_cipher_close(hd);
free(encBuffer);
fclose(fp);
fclose(fpout);
return 0;
}
The first program encrypt the file prova with aes256 in cbc mode. The encrypted file is called out
The second program decrypt the file out and the output file is origdec
I know that these 2 programs are not well coded (i'm planning new features soon and code cleanup soon) but before doing these things i prefer to develop e GOOD encryption program :-)
So my question is: i want to add the PKCS#7 removable padding, how can i do it? Any suggestion please? :-)
ps: what is pkcs#7 removable padding?
the file is encrypted block by block where each block is 16 bytes. The last block may not be 16 bytes long so there's the need to "fill" it with something. The pkcs#7 standard expects that the block is filled with the number of "non filled" position. So if in a block of 16 bytes only 4 bytes are occupied the last 12 bytes are filled with 0x12