provaは、 hello i am a pcを含むプレーンテキスト ファイルです。
暗号化:
FILE *fp = fopen("prova", "r+");
FILE *fpout = fopen("out", "w+");
while(!feof(fp)){
memset(plain_text, 0, sizeof(plain_text));
retval = fread(plain_text, 1, 16, fp);
txtLenght = sizeof(plain_text);
encBuffer = malloc(txtLenght);
algo = gcry_cipher_map_name(name);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
gcry_cipher_encrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
fwrite(encBuffer, 1, 16, fpout);
}
復号化:
FILE *fp = fopen("out", "r+");
FILE *fpout = fopen("origdec", "w+");
while(!feof(fp)){
memset(plain_text, 0, sizeof(plain_text));
retval = fread(plain_text, 1, 16, fp);
txtLenght = sizeof(plain_text);
encBuffer = malloc(txtLenght);
algo = gcry_cipher_map_name(name);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
gcry_cipher_decrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
fwrite(encBuffer, 1, 16, fpout);
}
どこ:
char key[32] = {0x80};
char iniVector[16] = {0};
char plain_text[16];
char *encBuffer = NULL;
問題: 暗号化されたファイルを解読すると、ファイルorigdecにはプレーン テキストとランダムな無用で読み取り不能な文字が含まれています。