1. 程式人生 > >Crypto++ AES 加密解密流程

Crypto++ AES 加密解密流程

color 程序 pla ESS dem cde inb def 必須

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

Crypto++ AES 加密解密流程