Crypto++ AES 加密解密流程

// aesdemo.cpp : 定义控制台应用程序的入口点。
//

#include <stdio.h>
#include <tchar.h>
#include <iostream>


#include "aes.h"

using namespace std;
using namespace CryptoPP;


int main()
{
    cout << "aes demo "<< AES::StaticAlgorithmName() << endl;
    unsigned char aesKey[AES::DEFAULT_KEYLENGTH] = "aes"; //密钥

    AESEncryption aesEncryptor; //加密器     
    aesEncryptor.SetKey(aesKey, AES::DEFAULT_KEYLENGTH); //设定加密密钥

    char* srcData = "123456789abcdefghi987654321";

    cout << "will be encode:" << srcData << endl;

    unsigned char xorBlock[AES::BLOCKSIZE]; //必须设定为全零
    memset(xorBlock, 0, AES::BLOCKSIZE); //置零
    unsigned char inBlock[AES::BLOCKSIZE]; //要加密的数据块
    unsigned char outBlock[AES::BLOCKSIZE]; //加密后的密文块

    AESDecryption aesDecryptor;
    aesDecryptor.SetKey(aesKey, AES::DEFAULT_KEYLENGTH);
    unsigned char plainText[AES::BLOCKSIZE];

    int srclen = strlen(srcData);
    char* dstData = (char*)calloc(srclen, sizeof(char));
    int pos = 0;
    do 
    {
        int relaysize = srclen - pos; 
        int cpsize = relaysize > AES::BLOCKSIZE ? AES::BLOCKSIZE : relaysize;
        memset(inBlock, 0, AES::BLOCKSIZE);
        memset(outBlock, 0, AES::BLOCKSIZE);
        memset(plainText, 0, AES::BLOCKSIZE);
        memcpy(inBlock, srcData + pos, cpsize);
        aesEncryptor.ProcessAndXorBlock(inBlock, xorBlock, outBlock); //加密
        aesDecryptor.ProcessAndXorBlock(outBlock, xorBlock, plainText);
        memcpy(dstData + pos, plainText, cpsize);
        pos += cpsize;

    } while (pos < srclen-1);
    cout << "after encode and decode :" << dstData << endl;
    free(dstData);
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/larkin-cn/p/9546672.html