Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我们如何使用Crypto++;?_C++_Encryption_Aes_Crypto++ - Fatal编程技术网

C++ 我们如何使用Crypto++;?

C++ 我们如何使用Crypto++;?,c++,encryption,aes,crypto++,C++,Encryption,Aes,Crypto++,我是加密技术的新手。 我已经阅读了Crypto++网站上有关AES加密的文档,我想使用它执行AES文件加密。 他们拥有的示例代码如下所示: #include "cryptlib.h" #include "rijndael.h" #include "modes.h" #include "files.h" #include "osrng.h" #include "hex.h"

我是加密技术的新手。 我已经阅读了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;
}
#包括“cryptlib.h”
#包括“rijndael.h”
#包括“modes.h”
#包括“files.h”
#包括“osrng.h”
#包括“十六进制h”
#包括
#包括
int main(int argc,char*argv[])
{
使用名称空间CryptoPP;
自动进料器和OMPOOL prng;
HexEncoder编码器(新文件链接(std::cout));
SecByteBlock密钥(AES::DEFAULT_KEYLENGTH);
SecByteBlock iv(AES::BLOCKSIZE);
prng.GenerateBlock(key,key.size());
prng.GenerateBlock(iv,iv.size());
std::string plain=“CBC模式测试”;
std::字符串密码,已恢复;

std::cout简单开始。加密非常复杂,需要包含大量额外的位。第一次试用时,您需要一个密钥、一个模式(CBC相当简单)和一个IV(初始化向量)。设置它,测试它并使其工作。然后您可以添加密钥派生之类的内容,并尝试更复杂的模式,如GCM。

是的,我会尝试。谢谢:)