Crypto++ AES 加密解密流程

2018-09-01 05:37:58来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

// 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;
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Too Rich HDU - 5527 (贪心+dfs)

下一篇:华为笔试——C++字符串四则运算的实现