kcrypt.c
最終更新:2009/12/16
001: /* kcrypt.c 002: * 暗号化・復号のサンプル 003: */ 004: 005: #define USAGE "Usage: kcrypt <cipher_alg> <enc | dec> <input_file> <output_file>" 006: 007: #include "apr_general.h" 008: #include "apr_file_io.h" 009: #include "apr_errno.h" 010: #include "apr_strings.h" 011: #include "apr_buckets.h" 012: 013: #include "mycipher.h" 014: #include "mybb.h" 015: #include "mystab.h" 016: 017: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) { 018: 019: int error_flag = 0; 020: apr_status_t rv; 021: char error_buf[256]; 022: 023: 024: char *cipher_alg = NULL; 025: char *encdec = NULL; 026: char *input_file = NULL; 027: char *output_file = NULL; 028: int enc = 0; 029: 030: if (ac < 5) { 031: apr_file_printf(astderr, "%s\n", USAGE); 032: error_flag = 1; 033: goto _FINALLY_; 034: } 035: 036: cipher_alg = av[1]; 037: encdec = av[2]; 038: input_file = av[3]; 039: output_file = av[4]; 040: 041: if (!strcmp(encdec, "enc")) { 042: enc = 1; 043: } 044: 045: init_mycipher(); 046: 047: if (1) { 048: apr_bucket_alloc_t *ba = NULL; 049: apr_bucket_brigade *bbin = NULL; 050: apr_bucket_brigade *bbout = NULL; 051: apr_file_t *fpin = NULL; 052: apr_file_t *fpout = NULL; 053: apr_finfo_t finfo; 054: if (APR_SUCCESS != (rv = apr_file_open(&fpin, input_file, APR_FOPEN_READ|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { 055: apr_file_printf(astderr, "ERROR: apr_file_open: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf))); 056: error_flag = 1; 057: goto _FINALLY_; 058: } 059: if (APR_SUCCESS != (rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fpin))) { 060: apr_file_printf(astderr, "ERROR: apr_file_info_get: %s ", apr_strerror(rv, error_buf, sizeof(error_buf))); 061: apr_file_close(fpin); 062: error_flag = 1; 063: goto _FINALLY_; 064: } 065: if (APR_SUCCESS != (rv = apr_file_open(&fpout, output_file, 066: APR_FOPEN_WRITE|APR_FOPEN_CREATE|APR_FOPEN_TRUNCATE|APR_FOPEN_BINARY, APR_OS_DEFAULT, pool))) { 067: apr_file_close(fpin); 068: apr_file_printf(astderr, "ERROR: apr_file_open: %s ", apr_strerror(rv, error_buf, sizeof(error_buf))); 069: error_flag = 1; 070: goto _FINALLY_; 071: } 072: 073: ba = apr_bucket_alloc_create(pool); 074: bbin = apr_brigade_create(pool, ba); 075: bbout = apr_brigade_create(pool, ba); 076: 077: apr_brigade_insert_file(bbin, fpin, 0, finfo.size, pool); 078: 079: #ifdef DEBUG 080: printf("#finfo.size=[%d]\n", finfo.size); 081: #endif 082: 083: /* メモ:鍵やIVは乱数にするのが普通だけど…。 */ 084: 085: if(kencdec_from_bb_to_bb(cipher_alg, "0123456789ABCDEF", 16, "This is a pencil", 16, (apr_size_t)finfo.size, bbin, bbout, enc)) { 086: write_from_bb_to_fp (bbout, fpout); 087: } else { 088: error_flag = 1; 089: } 090: 091: apr_file_close(fpout); 092: fpin = NULL; 093: apr_file_close(fpout); 094: fpout = NULL; 095: apr_brigade_destroy(bbin); 096: bbin = NULL; 097: apr_brigade_destroy(bbout); 098: bbout =NULL; 099: apr_bucket_alloc_destroy(ba); 100: ba = NULL; 101: } 102: 103: _FINALLY_: 104: 105: if (error_flag) { 106: apr_file_printf(astderr, "failed!\n"); 107: return 1; /* 異常終了 */ 108: } 109: 110: apr_file_printf(astdout, "\ndone.\n"); 111: return 0; /* 正常終了 */ 112: 113: } /* end of main */ 114:
KAKU PROJECT (2009) |