Do-Program的专栏

学习笔记

openssl aes128 应用程序

本文记录 使用 openssl aes128 进行文件加密的应用程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ENCRYPT_MODULE_DBG
#ifndef ENCRYPT_MODULE_DBG
#define DBG(s...)   ((void) 0)
#else
#define DBG(fmt, arg...)    printf("[encrypt_module %d] "fmt, __LINE__, ##arg)
#endif

#define ERR(fmt, arg...)                                    \
    do {                                                    \
        printf("[encrypt_module %d] "fmt, __LINE__, ##arg); \
        exit(-1);                                           \
    } while (0)

#define GET_PADDING(size, align)    \
    ((((size) + (align) - 1) & ~((align) - 1)) - size)

#define PATH_MAX    1024
char command[PATH_MAX] = {0};
char path[PATH_MAX] = {0};
char file[PATH_MAX] = {0};

int main(int argc, char *argv[])
{
    DBG(" =================================== \n");
    DBG("|       *encrypt module tool*       |\n");
    DBG("|      argv[0]:encrypt_module       |\n");
    DBG("|      argv[1]:input file name      |\n");
    DBG("|      argv[2]:output file name     |\n");
    DBG(" =================================== \n");

    if (argc != 3) {
        ERR("ERROR:Usage: ./encrypt_module input_file output_file\n");
        exit(-1);
    }

    DBG("input file: %s\n", argv[1]);

    /*
     * TODO: You can customize your encryption algorithm,
     * the following is the sample code(AES128).
     */
    // 1.open input file
    FILE *fpin;
    long size = 0;
    fpin = fopen(argv[1], "rb");
    if (!fpin) {
        ERR("can't open %s\n", argv[1]);
        exit(-1);
    }
    fseek(fpin, 0, SEEK_END);
    size = ftell(fpin);
    if (size == 0) {
        ERR("%s size is zero!\n", argv[1]);
        exit(-1);
    }
    fseek(fpin, 0, SEEK_SET);

    // 2.create tmp file xxx.padding.aligned
    // *******************************************
    //      xxx.padding.aligned syntax
    //  syntax                          bits
    //  body length                      32
    //  body data                   argv[1] size
    //  padding
    // *******************************************
    char *tmp_str;
    memset(path, 0x00, sizeof(path));
    tmp_str = strrchr(argv[1], '/');
    if (tmp_str != NULL) {
        strncpy(path, (char *)&argv[1][0], (tmp_str - (char *)&argv[1][0]) + 1);
    } else {
        strncpy(path, "./", 2);
        tmp_str = argv[1];
    }
    memset(file, 0x00, sizeof(path));
    snprintf(file, PATH_MAX, "%s%s.padding.aligned", path, tmp_str);
    FILE *fpout;
    if ((fpout = fopen(file,"wb+")) == NULL) {
        ERR("create tmp %s.padding.aligned fail!\n", tmp_str);
        exit(-1);
    }

    // 3.insert 4 bytes of body length at the beginning of the file xxx.padding.aligned
    DBG("%s size: 0x%x\n", argv[1], size);
    unsigned char byte[4] = {0};
    byte[0] = (unsigned char)(size >> 24);
    byte[1] = (unsigned char)(size >> 16);
    byte[2] = (unsigned char)(size >> 8);
    byte[3] = (unsigned char)size;
    int i = 0;
    for (i = 0; i < 4; i++) {
        // DBG("byte = 0x%x\n", byte[i]);
        fputc(byte[i], fpout);
    }
    char *buf = (char *)malloc(size);
    if (buf == NULL) {
        ERR("malloc fail!\n");
        exit(-1);
    }
    memset(buf, 0x00, size);
    long count = 0;
    while ((count = fread(buf, 1, size, fpin)) != 0) {
        fwrite(buf, 1, count, fpout);
    }
    if (buf != NULL) {
        free(buf);
        buf = NULL;
    }

    // 4.padding: because the encryption algorithm(AES128) requires 16-byte alignment
    int padding = GET_PADDING((size + 4), 16);
    DBG("16-byte alignment requires %d bytes to be filled\n", padding);
    for (i = 0; i < padding; i++) {
        fputc(0xFF, fpout);
    }

    fclose(fpin);
    fclose(fpout);

    // 5.AES128 encrypt
    int err = 0;
    snprintf(command, PATH_MAX, "openssl enc -e -aes-128-cbc -K 82BDAC1428B29011971567477A10A091 -iv 82BDAC1428B29011971567477A10A091 -nosalt -nopad -in %s.padding.aligned -out %s", argv[1], argv[2]);
    err = system(command);
    if ((err == -1) || (WEXITSTATUS(err) != 0)) {
        DBG("err = %d\n", err);
        DBG("WEXITSTATUS(err) = %d\n", WEXITSTATUS(err));
        exit(-1);
    }

    DBG("output file: %s\n", argv[2]);

    return 0;
}

阅读更多
个人分类: C
上一篇后台PHP与手机APP接口开发(API接口开发)
想对作者说点什么? 我来说一句

AES-128 ECB pkcs5padding

2016年03月16日 87KB 下载

没有更多推荐了,返回首页

关闭
关闭