AES算法的加解密示例程序(C++)

版权声明:本文为博主原创文章,商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/liitdar/article/details/88354513

本文主要介绍 AES 算法的加解密方法。本文使用的语言为 C++,调用的 AES 库为:cryptopp。

1 概述

AES 加密算法的介绍如下(摘自 WikiPedia):

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称 Rijndael 加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的 DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,AES 已然成为对称密钥加密中最流行的算法之一。

该算法为比利时密码学家 Joan Daemen 和 Vincent Rijmen 所设计,结合两位作者的名字,以 Rijndael 为名投稿高级加密标准的甄选流程。

2 cryptopp安装

cryptopp:Crypto++ Library is a free C++ class library of cryptographic schemes. 

GitHub地址:https://github.com/weidai11/cryptopp

官方网站: https://cryptopp.com/

我是直接使用 yum 方式进行安装的(在 epel 源中已经包含了 cryptopp),命令如下:

yum install cryptopp-devel.x86_64

3 示例代码

本节介绍在 ECB 模式16字节长度的 keyPKCS7填充方式的场景下,使用 AES 算法进行加解密的示例代码。

示例代码(aes_test1.cpp)如下:

#include <iostream>
#include <string>
#include "hex.h"
#include "aes.h"
#include "modes.h"

using namespace CryptoPP;
using namespace std;

int main(int argc, char* argv[])
{
    // use default key length 16 bytes
    byte key[AES::DEFAULT_KEYLENGTH] = "abcd1234";
    // string to be encrypted
    string strPlain = "ECB Mode Test";
    // cipher string
    string strCipher;
    // encoded string
    string strEncoded;
    // recovered string, should be same with strPlain
    string strRecovered;

    try
    {
        cout << "key is: " << key << endl;
        cout << "plain is: " << strPlain << endl;
        // encrypt with ECB mode
        ECB_Mode< AES >::Encryption e;
        e.SetKey(key, sizeof(key));

        // encrypt here
        // The StreamTransformationFilter adds padding as required, use PKCS_PADDING(PKCS7Padding) default.
        // ECB and CBC Mode must be padded to the block size of the cipher.
        StringSource(strPlain, true, 
            new StreamTransformationFilter(e,
                new StringSink(strCipher) // StringSink
            ) // StreamTransformationFilter
        ); // StringSource
    }
    catch (const Exception& e)
    {
        cerr << e.what() << endl;
        exit(1);
    }

    // print cipher string
    strEncoded.clear();
    StringSource(strCipher, true,
        new HexEncoder( 
            new StringSink(strEncoded) // StringSink
        ) // HexEncoder
    ); // StringSource
    cout << "cipher is: " << strEncoded << endl;

    try
    {
        // decrypt with ECB mode
        ECB_Mode< AES >::Decryption d;
        d.SetKey(key, sizeof(key));

        // The StreamTransformationFilter removes padding as required.
        StringSource s(strCipher, true, 
            new StreamTransformationFilter(d,
                new StringSink(strRecovered) // StringSink
            ) // StreamTransformationFilter
        ); // StringSource
        cout << "recovered string is: " << strRecovered << endl;
    }
    catch(const Exception& e)
    {
        cerr << e.what() << endl;
        exit(1);
    }

    return 0;
}

编译并执行上述代码,结果如下:

在上述结果中可以看到,我们通过调用接口,完成了 AES 算法的加解密操作。

说明:我们可以通过对比“示例代码的加密结果”和“网上一些在线 aes 加解密网站的加密结果”,来验证我们的加密操作是否正确。

猜你喜欢

转载自blog.csdn.net/liitdar/article/details/88354513