使用AES 256 CBC模式进行文件加密/解密

时间:2015-05-21 17:40:29

标签: c encryption openssl cryptography aes

我正在尝试使用AES 256 CBC模式加密/解密,我仍然坚持你如何使用文件。要求以4096字节的块读取文件并加密/解密并将结果存储回文件。这是我的代码。

以前我能够为随机的十六进制字符串做这件事。该程序可以生成随机密钥和普通十六进制,并完美地加密/解密。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

int main(int argc, char **argv)
{
    int keylength = 256;

    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", &inputslength);

    unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char)*inputslength);
    memset(aes_input, 'X', inputslength);

    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);

    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));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_cbc_encrypt(enc_out, dec_out, encslength, &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;
}

现在我需要以4096字节为增量传递一个文件,并能够将输出写回输出文件。我有以下

int main(int argc, char *argv[])
{
    FILE *fp_in, *fp_out;
    int c;
    funct = argv[1];
    /* Check command-line */
    if (argc != 3) {
        printf("EXAMPLE USAGE: ./mycp enc/dec <INFILEE> <OUTFILE>\n");
        return 1;
    }

    /* Open files */
    if (!(fp_in = fopen(argv[2], "rb"))) {
        perror("input file open error");
        return 1;
    }

    if (!(fp_out = fopen(argv[3], "wb"))) {
        perror("output file open error");
        return 1;
    }

    fseek(storeFile, blocksToRead[i] * 4096, SEEK_SET);
    int n = fread(ptr, 4096, 1, fp_in);
    if (funct == "enc")
    {
        enc();          //encrypt
    }
    else if (funct == "dec")
    {
        dec();          //decrypt
    }

    if (!(funct = argv[1], "wb"))) {
        perror("output file open error");
        return 1;
    }
    if (fwrite(ptr, n, fp_out) != n)
    {
        printf("write error\n");
        return -1;
    }
    fclose(fp_out);
    fclose(fp_in);
    return 0;
}

0 个答案:

没有答案