📄 encfile.cpp
字号:
/* * @(#)encfile.cpp 2006-06-18 01:44:44 * Copyright 2006 HUST Software School. All rights reserved. * Author: 翟黎 软件0309 01012003026327 Email:mrzhaili@gmail.com */#include "stdafx.h"/* #include <stdlib.h> */#include <stdio.h>/* #include <time.h> */#include "aes.h"//定义文件加密的块大小(也就是加密算法输入的块大小)#define BLOCK_SIZE 16//AES文件加密函数/* 文件加密采用ECB模式,也就是电话簿模式,别的模式有待实现 最后在文件的尾部写入加密文件的大小(当然也是加密过的数据) @param fr 需要加密的文件 @param fw 加密后输出的文件 @param key 加密所需的密钥*/int AES_file_encrypt(const char* fr, const char* fw, const 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_encrypt(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; } //写入最后一块,不够16位的补零 for (i = r_size; i < BLOCK_SIZE; i++) buf[i] = 0; AES_encrypt(buf, buf, key); fwrite(buf, sizeof(byte), BLOCK_SIZE, fp_w); *((int *)buf) = ftell(fp_r); AES_encrypt(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;}//AES文件解密密函数/* 文件解密密同样采用ECB模式 @param fr 需要解密的文件 @param fw 解密后输出的文件 @param key 解密所需的密钥*/int AES_file_decrypt(const char* fr, const char* fw, const 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_decrypt(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_decrypt(p, p, key); AES_decrypt(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 + -