📄 encfile.cpp
字号:
/* * @(#)encfile.c 2006-06-18 01:44:44 * Copyright 2006 HUST Software School. All rights reserved. */#include "stdafx.h"/* #include <stdlib.h> */#include <stdio.h>/* #include <time.h> */#include "aes.h"#define BLOCK_SIZE 16int AES_file_encrypt(const char* fr, const char* fw, byte key[16]){ byte buf[BLOCK_SIZE]; FILE *fp_r, *fp_w; int i, r_size; if ((fp_r = fopen(fr, "rb")) == NULL) { fprintf(stderr, "Error reading file."); return 1; } if ((fp_w = fopen(fw, "wb")) == NULL) { fprintf(stderr, "Error reading file."); fclose(fp_r); return 1; } r_size = fread(buf, sizeof(byte), BLOCK_SIZE, fp_r); while (r_size == BLOCK_SIZE) { AES_jiami(buf, buf, key); fwrite(buf, sizeof(byte), BLOCK_SIZE, fp_w); r_size = fread(buf, sizeof(byte), BLOCK_SIZE, fp_r); } if (!feof(fp_r)) { fprintf(stderr, "Error reading file in feof."); fclose(fp_r); fclose(fp_w); return 1; } for (i = r_size; i < BLOCK_SIZE; i++) buf[i] = 0; AES_jiami(buf, buf, key); fwrite(buf, sizeof(byte), BLOCK_SIZE, fp_w); *((int *)buf) = ftell(fp_r); AES_jiami(buf, buf, key); fwrite(buf, sizeof(byte), BLOCK_SIZE, fp_w);/* for (i = 0; i < BLOCK_SIZE; i++) *//* printf("%02x ", buf[i]); *//* printf("\n"); *//* printf("last count:%d\n", r_size); */ fclose(fp_r); fclose(fp_w); return 0;}int AES_file_decrypt(const char* fr, const char* fw, byte key[16]){ byte buf1[BLOCK_SIZE], buf2[BLOCK_SIZE], buf3[BLOCK_SIZE], *p, *q, *r, *t; FILE *fp_r, *fp_w; int r_size, diff; if ((fp_r = fopen(fr, "rb")) == NULL) { fprintf(stderr, "Error reading file."); return 1; } if ((fp_w = fopen(fw, "wb")) == NULL) { fprintf(stderr, "Error reading file."); fclose(fp_r); return 1; } p = buf1, q = buf2, r = buf3; r_size = fread(p, sizeof(byte), BLOCK_SIZE, fp_r); if (r_size != BLOCK_SIZE) { fprintf(stderr, "Decrypt file failed"); fclose(fp_r); fclose(fp_w); return 1; } r_size = fread(q, sizeof(byte), BLOCK_SIZE, fp_r); if (r_size != BLOCK_SIZE) { fprintf(stderr, "Decrypt file failed"); fclose(fp_r); fclose(fp_w); return 1; } r_size = fread(r, sizeof(byte), BLOCK_SIZE, fp_r); while (r_size == BLOCK_SIZE) { AES_jiemi(p, p, key); fwrite(p, sizeof(byte), BLOCK_SIZE, fp_w); t = p; p = q; q = r; r = t; r_size = fread(r, sizeof(byte), BLOCK_SIZE, fp_r);/* for (i = 0; i < BLOCK_SIZE; i++) *//* printf("%02x ", p[i]); *//* printf("\n"); *//* for (i = 0; i < BLOCK_SIZE; i++) *//* printf("%02x ", q[i]); *//* printf("\n"); *//* for (i = 0; i < BLOCK_SIZE; i++) *//* printf("%02x ", r[i]); *//* printf("\n"); */ } if (!feof(fp_r)) { fprintf(stderr, "Error reading file in feof."); fclose(fp_r); fclose(fp_w); return 1; } AES_jiemi(p, p, key); AES_jiemi(q, q, key); diff = 32 + *(int *)q - ftell(fp_r); if (diff > 0 && diff < 16) fwrite(p, sizeof(byte), diff, fp_w); fclose(fp_r); fclose(fp_w); return 0;}/* int main() *//* { *//* byte ckey[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, *//* 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; *//* clock_t start, finish; *//* double duration; *//* start = clock(); *//* //AES_file_encrypt("123.rar", "123.rar.enc", ckey); *//* AES_file_decrypt("123.rar.enc", "123.rar.dec", ckey); *//* /\* AES_file_encrypt("aes.o", "aes.o.enc", ckey); *\/ *//* /\* AES_file_decrypt("aes.o.enc", "aes.o.dec", ckey); *\/ */ /* finish = clock(); *//* duration = (double)(finish - start) / CLOCKS_PER_SEC; *//* printf("%lf", duration); *//* return 0; *//* } */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -