// cryptoDemo.cpp : Defines the entry point for the console application.
// Windows: cl cryptoDemo.cpp
// Linux: gcc -o cryptoDemo cryptoDemo.cpp -lcrypto
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
#include "openssl/aes.h"
#include <fstream>
#define maxNameLength 50
#define bufferLength 16
#pragma comment(lib,"libeay32.lib")
using namespace std;
char strFileName[maxNameLength];
ifstream infileStream;
void enc( char passwd[], int pwdLen)
{
cout << "正在加密..." << endl;
int i, len;
unsigned char aes_keybuf[32];
AES_KEY aeskey;
// 准备32字节(256位)的AES密码字节
memset(aes_keybuf, 0x90, 32);
if (pwdLen<32) { len = pwdLen; }
else { len = 32; }
for (i = 0; i<len; i++) aes_keybuf[i] = passwd[i];
AES_set_encrypt_key(aes_keybuf, 256, &aeskey);
ofstream outfileStream;
char encFileName[maxNameLength];
strcpy_s(encFileName, strFileName);
char * appendStr = "enc";
strcat_s(encFileName, appendStr);
cout << encFileName << endl;
outfileStream.open(encFileName, ofstream::out | ofstream::binary);
while (!infileStream.eof())
{
unsigned char buf[bufferLength];
unsigned char buf2[bufferLength];
memset(buf, 0, 16);
memset(buf2, 0, 16);
infileStream.read((char *)buf, bufferLength);
int inCount = infileStream.gcount();
AES_encrypt(buf, buf2, &aeskey);
outfileStream.write((char *)buf2, bufferLength);
if (inCount!= bufferLength)//最后一次读出的数据大小是属于[0,15]
{
outfileStream.put(inCount);
}
}
outfileStream.close();
infileStream.close();
cout << "加密完成。" << endl;
}
void dec( char passwd[], int pwdLen)
{
cout << "正在解密..." << endl;
int i,len;
unsigned char aes_keybuf[32];
AES_KEY aeskey;
// 准备32字节(256位)的AES密码字节
memset(aes_keybuf, 0x90, 32);
if (pwdLen<32) { len = pwdLen; }
else { len = 32; }
for (i = 0; i<len; i++) aes_keybuf[i] = passwd[i];
// 密文串的解密
AES_set_decrypt_key(aes_keybuf, 256, &aeskey);
ofstream outfileStream;
char previousFileName[maxNameLength];
strcpy_s(previousFileName, strFileName);
char * appendStr = "1";
strcat_s(previousFileName, appendStr);
cout << previousFileName << endl;
outfileStream.open(previousFileName, ofstream::out | ofstream::binary);
unsigned char buf[bufferLength];
unsigned char buf2[bufferLength];
memset(buf, 0, 16);
memset(buf2, 0, 16);
infileStream.seekg(-1, infileStream.end);//将当前定位到文件末端-1
cout << infileStream.tellg() << endl;
char lastInCount = -1;
infileStream.get(lastInCount);
infileStream.seekg(0, infileStream.beg);//重新指向文件开始
while (!infileStream.eof())
{
infileStream.read((char *)buf, bufferLength);
AES_decrypt(buf, buf2, &aeskey);
infileStream.read((char *)buf, bufferLength);
int inCount = infileStream.gcount();
cout << inCount << endl;
if (inCount == 1)//最后一次读出的数据1
{
outfileStream.write((char *)buf2, lastInCount);
}
if (inCount == bufferLength)
{
infileStream.seekg(0- bufferLength, ios::cur);
outfileStream.write((char *)buf2, bufferLength);
}
}
outfileStream.close();
infileStream.close();
cout << "解密完成" << endl;
}
int main(int argc, char* argv[])
{
while (true)
{
cout << "******************************************" << endl;
cout << "输入参数格式:参数1(enc or dec) 参数2(加密文件解密文件文件名) 参数3(加密/解密密码) " << endl;
char strMode[maxNameLength];
char strPassword[maxNameLength];
cin >> strMode >> strFileName >> strPassword;
/*cout << "***" << strMode << "***" << endl;
cout << "***" << strFileName << "***" << endl;
cout << "***" << strPassword << "***" << endl;*/
infileStream.open(strFileName, ifstream::binary| ifstream::in);//二进制打开
if (!infileStream.is_open())
{
cout << "文件打开出错!" << endl;
continue;
}
if (0 == strcmp(strMode, "enc"))//加密
{
enc(strPassword, strlen(strPassword));
}
else if (0 == strcmp(strMode, "dec"))//解密
{
dec(strPassword, strlen(strPassword));
}
else
{
cout << "参数1输入不合法!" << endl;
}
}
return 0;
}
AES算法加密文件
最新推荐文章于 2024-05-15 02:22:09 发布