• 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;
    }
  • 相关阅读:
    Tomcat && Servlet
    List,Set,Collections工具类
    多表查询
    常用的API--集合
    msmpeng.exe阻止移动硬盘弹出
    接口400错误解析
    JDBC/Mybatis连接数据库报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    tomcat启动报错:A child container failed during start
    PAT 1019 数字黑洞
    PAT 1017 A除以B
  • 原文地址:https://www.cnblogs.com/larkin-cn/p/9546672.html
Copyright © 2020-2023  润新知