zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    2021.5.16 Android聊天功能
    2021.5.15 Android Gestures示例
    2021.5.14 程序员修炼之路:从小工到专家阅读笔记02
    KL 散度和交叉熵
    UBOOT学习
    UCOSII学习
    cortex-M3/M4体系学习
    一步步写RTOS
    38 操作系统-中断处理与特权级转移
    MDK、IAR将变量定义到指定位置
  • 原文地址:https://www.cnblogs.com/larkin-cn/p/9546672.html
Copyright © 2011-2022 走看看