简体   繁体   English

我们如何使用 Crypto++ 执行 AES 文件加密?

[英]How can we perform AES file encryption using Crypto++?

I am a newbie to Crypto++.我是 Crypto++ 的新手。 I have read the documentation on AES encryption on the Crypto++ website and I want to perform AES file encryption using it.我已阅读 Crypto++ 网站上有关 AES 加密的文档,我想使用它执行 AES 文件加密。 The sample code that they have is shown below:他们拥有的示例代码如下所示:

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

I have read the filesink and filesource documentation too but I cant actually get how to apply that in the code given above.我也阅读了文件接收器和文件文档,但实际上我无法在上面给出的代码中应用它。 Any help would be appreciated.任何帮助,将不胜感激。 https://www.cryptopp.com/wiki/Advanced_Encryption_Standard https://www.cryptopp.com/wiki/Advanced_Encryption_Standard

Start simple.从简单开始。 Crypto is complex with lots of extra bits to include.加密很复杂,需要包含许多额外的位。 For a first trial you need a key, a mode (CBC is reasonably simple) and an IV (initialisation vector).对于第一次试验,您需要一个密钥、一个模式(CBC 相当简单)和一个 IV(初始化向量)。 Set that up, test it and get it working.设置它,测试它并让它工作。 Then you can add things like key derivation and try the more complex modes such as GCM.然后您可以添加诸如密钥派生之类的东西并尝试更复杂的模式,例如 GCM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM