AES算法加密文件

// 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值