【问题标题】:How can we perform AES file encryption using Crypto++?我们如何使用 Crypto++ 执行 AES 文件加密?
【发布时间】:2021-07-30 12:50:27
【问题描述】:

我是 Crypto++ 的新手。 我已阅读 Crypto++ 网站上有关 AES 加密的文档,我想使用它执行 AES 文件加密。 他们的示例代码如下所示:

    #include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    HexEncoder encoder(new FileSink(std::cout));

    SecByteBlock key(AES::DEFAULT_KEYLENGTH);
    SecByteBlock iv(AES::BLOCKSIZE);

    prng.GenerateBlock(key, key.size());
    prng.GenerateBlock(iv, iv.size());

    std::string plain = "CBC Mode Test";
    std::string cipher, recovered;

    std::cout << "plain text: " << plain << std::endl;

    /*********************************\
    \*********************************/

    try
    {
        CBC_Mode< AES >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true, 
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) // StreamTransformationFilter
        ); // StringSource
    }
    catch(const Exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    /*********************************\
    \*********************************/

    std::cout << "key: ";
    encoder.Put(key, key.size());
    encoder.MessageEnd();
    std::cout << std::endl;

    std::cout << "iv: ";
    encoder.Put(iv, iv.size());
    encoder.MessageEnd();
    std::cout << std::endl;

    std::cout << "cipher text: ";
    encoder.Put((const byte*)&cipher[0], cipher.size());
    encoder.MessageEnd();
    std::cout << std::endl;
    
    /*********************************\
    \*********************************/

    try
    {
        CBC_Mode< AES >::Decryption d;
        d.SetKeyWithIV(key, key.size(), iv);

        StringSource s(cipher, true, 
            new StreamTransformationFilter(d,
                new StringSink(recovered)
            ) // StreamTransformationFilter
        ); // StringSource

        std::cout << "recovered text: " << recovered << std::endl;
    }
    catch(const Exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    return 0;
}

我也阅读了 filesinkfilesource 文档,但我实际上无法在上面给出的代码中应用它。 任何帮助,将不胜感激。 https://www.cryptopp.com/wiki/Advanced_Encryption_Standard

【问题讨论】:

    标签: c++ encryption aes crypto++


    【解决方案1】:

    从简单开始。加密很复杂,需要包含许多额外的位。对于第一次试验,您需要一个密钥、一个模式(CBC 相当简单)和一个 IV(初始化向量)。设置它,测试它并让它工作。然后,您可以添加密钥派生等内容,并尝试 GCM 等更复杂的模式。

    【讨论】:

    • 是的,我会试试的。谢谢:)
    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多