为什么我会遇到类型不匹配错误Crypto ++ AES

时间:2014-05-01 06:14:13

标签: c++ algorithm cryptography aes crypto++

我正在尝试在我的程序中实现AES加密算法。我正在测试Crypto ++中不同算法的速度。我有Blowfish和3DES工作,但我不能在那里获得AES。奇怪的是当我将AES分离到它自己的文件中并运行它时,它可以工作。以下是DOES的工作原理:g++ aes.cpp -o aes -lcryptopp -lpthread -L.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <vector>
#include <sstream>
#include <utility>
#include <stdio.h>
#include <string.h>

#include "cryptoplusplus/osrng.h"
using CryptoPP::AutoSeededRandomPool;

#include "cryptoplusplus/cryptlib.h"
using CryptoPP::Exception;

#include "cryptoplusplus/hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include "cryptoplusplus/modes.h"
using CryptoPP::CBC_Mode_ExternalCipher;
using CryptoPP::CBC_Mode;

#include "cryptoplusplus/filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::AuthenticatedEncryptionFilter;
using CryptoPP::AuthenticatedDecryptionFilter;
using CryptoPP::StreamTransformationFilter;

#include "cryptoplusplus/aes.h"
using CryptoPP::AES;

#include "crypto++/eax.h"
using CryptoPP::EAX;

#include "cryptoplusplus/secblock.h"
using CryptoPP::SecByteBlock;

#include "cryptoplusplus/files.h"
using CryptoPP::FileSource;
using CryptoPP::FileSink;

using namespace std;
int main( int argc, char* argv[] ) {
    // Declaring variables
    string ifilename, cipher, temp, rfilename; 

    AutoSeededRandomPool prng_aes;
    SecByteBlock key_aes( AES::DEFAULT_KEYLENGTH ); // Generate a random key
    prng_aes.GenerateBlock( key_aes, key_aes.size() ); 
    byte iv_aes[AES::BLOCKSIZE * 16]; // Generate a random initialization vector
    prng_aes.GenerateBlock( iv_aes, sizeof( iv_aes) );

    ifilename = "sample_files/1MB.png";
    cipher = "sample_files/1MB.png.enc";
    rfilename = "sample_files/recovered_sample_files/1MB.png";

    // Encrypts the plaintext
    EAX<AES>::Encryption e_aes;
    e_aes.SetKeyWithIV( key_aes, key_aes.size(), iv_aes, sizeof( iv_aes ) );
    FileSource fs1_aes( ifilename.c_str(), true, new AuthenticatedEncryptionFilter(e_aes, new FileSink( cipher.c_str() ) ) );
    // Decrypts the cipher
    EAX<AES>::Decryption d_aes;
    d_aes.SetKeyWithIV(key_aes, key_aes.size(), iv_aes, sizeof( iv_aes ) );
    FileSource fs3_aes( cipher.c_str(), true, new AuthenticatedDecryptionFilter(d_aes, new StringSink( rfilename ), AuthenticatedDecryptionFilter::THROW_EXCEPTION ) ); 
    return 0;
}

现在,当我尝试将这段代码实现到我的循环程序时,我得到了这些错误:

encryption_tests.cpp: In function ‘int main(int, char**)’:
encryption_tests.cpp:155:14: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T_BlockCipher> struct CryptoPP::EAX’
encryption_tests.cpp:155:14: error:   expected a type, got ‘AES’
encryption_tests.cpp:155:28: error: expected initializer before ‘e_aes’
encryption_tests.cpp:156:7: error: ‘e_aes’ was not declared in this scope
encryption_tests.cpp:159:14: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T_BlockCipher> struct CryptoPP::EAX’
encryption_tests.cpp:159:14: error:   expected a type, got ‘AES’
encryption_tests.cpp:159:28: error: expected initializer before ‘d_aes’
encryption_tests.cpp:160:7: error: ‘d_aes’ was not declared in this scope

现在这是实施尝试

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <vector>
#include <sstream>
#include <utility>
#include <stdio.h>
#include <string.h>

#include "cryptoplusplus/osrng.h"
using CryptoPP::AutoSeededRandomPool;

#include "cryptoplusplus/cryptlib.h"
using CryptoPP::Exception;

#include "cryptoplusplus/hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include "cryptoplusplus/modes.h"
using CryptoPP::CBC_Mode_ExternalCipher;
using CryptoPP::CBC_Mode;

#include "cryptoplusplus/filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::AuthenticatedEncryptionFilter;
using CryptoPP::AuthenticatedDecryptionFilter;
using CryptoPP::StreamTransformationFilter;

#include "cryptoplusplus/blowfish.h"
using CryptoPP::Blowfish;

#include "cryptoplusplus/aes.h"
using CryptoPP::AES;

#include "cryptoplusplus/des.h"
using CryptoPP::DES_EDE3;

#include "crypto++/eax.h"
using CryptoPP::EAX;

#include "cryptoplusplus/secblock.h"
using CryptoPP::SecByteBlock;

#include "cryptoplusplus/files.h"
using CryptoPP::FileSource;
using CryptoPP::FileSink;


using std::setw;
using namespace std;

/*
 *Send a string like 10MB.txt, and this will return 10MB
 */
string get_file_size( string size) {
    return size.substr( 0, size.find(".") );
}

/*
 *Send a string like 10MB.txt, and this will return txt
 */
string get_file_type( string type) {
    return type.substr( type.find(".") + 1 );
}

int main( int argc, char* argv[] ) {
    // Declaring variables
    const int NUMBER_OF_RUNS = 1;
    const int NUMBER_OF_FILES = 2;
    const int NUMBER_OF_ALGORITHMS = 3;

    const int BLOWFISH = 0;
    const int AES = 1;
    const int TRIPLE_DES = 2;
    const int GOST = 3;


    string algorithm_name[NUMBER_OF_ALGORITHMS] = { "blowfish", "aes", "triple_des" };
    string file_names_txt[NUMBER_OF_FILES] = {  /*"1MB.txt",*/ "1MB.jpg", "1MB.png"/*, "5MB.txt", "5MB.jpg", "5MB.png",*//* "10MB.txt", "20MB.txt", "30MB.txt", "40MB.txt",  "3MB.jpg", "4MB.avi", "10MB.avi"*/ };

    double time_data [NUMBER_OF_RUNS];
    double average_time_data [NUMBER_OF_ALGORITHMS][NUMBER_OF_FILES];
    string ifilename, cipher, encoded, rfilename; 
    string initial_cpp_time_data = "";
    clock_t time_start, time_stop;
    double run_time, time_difference, time_average = 0;


    /*blowfish*/
    AutoSeededRandomPool prng_blowfish; // This class seeds itself using an operating system provided RNG
    SecByteBlock key_blowfish(Blowfish::DEFAULT_KEYLENGTH ); // Generate a random key
    prng_blowfish.GenerateBlock(key_blowfish, key_blowfish.size() ); 
    byte iv_blowfish[Blowfish::BLOCKSIZE]; // Generate a random initialization vector
    prng_blowfish.GenerateBlock( iv_blowfish, sizeof( iv_blowfish ) );

    /*aes*/
    AutoSeededRandomPool prng_aes;
    SecByteBlock key_aes( AES::DEFAULT_KEYLENGTH ); // Generate a random key
    prng_aes.GenerateBlock( key_aes, key_aes.size() ); 
    byte iv_aes[AES::BLOCKSIZE * 16]; // Generate a random initialization vector
    prng_aes.GenerateBlock( iv_aes, sizeof( iv_aes) );

    /*triple_des*/
    AutoSeededRandomPool prng_triple_des;
    SecByteBlock key_triple_des(0x00, DES_EDE3::DEFAULT_KEYLENGTH ); // Generate a random key
    prng_triple_des.GenerateBlock(key_triple_des, key_triple_des.size() ); 
    byte iv_triple_des[DES_EDE3::BLOCKSIZE]; // Generate a random initialization vector
    prng_triple_des.GenerateBlock( iv_triple_des, sizeof( iv_triple_des ) );

    try {
        for ( int k = 0 ; k < NUMBER_OF_ALGORITHMS ; k++ ) {
            //This loop is for running different files
            cout << "algorithm_name: " << algorithm_name[k] << endl;
            for( int j = 0; j < NUMBER_OF_FILES; j++ ) {

                // ifilename = "sample_files/" + file_names_txt[j];
                // cipher = file_names_txt[j] + ".enc";
                // rfilename = "sample_files/recovered_" + file_names_txt[j];
                ifilename = "sample_files/1MB.png";
                cipher = "sample_files/1MB.png.enc";
                rfilename = "sample_files/recovered_sample_files/1MB.png";

                time_average = 0;
                //This loop is for running multiple times for a bigger sample size
                cout << file_names_txt[j] << ": " << endl;
                for( int i = 0 ; i < NUMBER_OF_RUNS; i++ ) {
                    time_start = clock();

                    if ( strcmp( algorithm_name[k].c_str(), "blowfish") == 0 ) {
                        /*blowfish*/
                        // Encrypts the plaintext
                        EAX<Blowfish>::Encryption e_blowfish;
                        e_blowfish.SetKeyWithIV( key_blowfish, key_blowfish.size(), iv_blowfish, sizeof( iv_blowfish ) );
                        FileSource fs1_blowfish( ifilename.c_str(), true, new AuthenticatedEncryptionFilter( e_blowfish, new FileSink( cipher.c_str() ) ) );
                        // Decrypts the cipher
                        EAX<Blowfish>::Decryption d_blowfish;
                        d_blowfish.SetKeyWithIV( key_blowfish, key_blowfish.size(), iv_blowfish, sizeof( iv_blowfish ) );
                        FileSource fs2_blowfish( cipher.c_str(), true, new AuthenticatedDecryptionFilter( d_blowfish, new StringSink( rfilename ), AuthenticatedDecryptionFilter::THROW_EXCEPTION ) ); 

                    } else if ( strcmp( algorithm_name[k].c_str(), "aes") == 0 ) {
                        /*aes*/
                        // Encrypts the plaintext
                        EAX<AES>::Encryption e_aes;
                        e_aes.SetKeyWithIV( key_aes, key_aes.size(), iv_aes, sizeof( iv_aes ) );
                        FileSource fs1_aes( ifilename.c_str(), true, new AuthenticatedEncryptionFilter(e_aes, new FileSink( cipher.c_str() ) ) );
                        // Decrypts the cipher
                        EAX<AES>::Decryption d_aes;
                        d_aes.SetKeyWithIV(key_aes, key_aes.size(), iv_aes, sizeof( iv_aes ) );
                        FileSource fs3_aes( cipher.c_str(), true, new AuthenticatedDecryptionFilter(d_aes, new StringSink( rfilename ), AuthenticatedDecryptionFilter::THROW_EXCEPTION ) ); 

                    } else if ( strcmp( algorithm_name[k].c_str(), "triple_des") == 0 ) {
                        /*triple_des*/
                        // Encrypts the plaintext
                        CBC_Mode<DES_EDE3>::Encryption e_triple_des;
                        e_triple_des.SetKeyWithIV( key_triple_des, key_triple_des.size(), iv_triple_des, sizeof( iv_triple_des) );
                        // The StreamTransformationFilter adds padding as required. ECB and CBC Mode must be padded to the block size of the cipher.
                        FileSource fs1_triple_des( ifilename.c_str(), true, new StreamTransformationFilter(e_triple_des, new FileSink( cipher.c_str() ) ) );
                        encoded.clear();
                        FileSource fs2_triple_des( cipher.c_str(), true, new HexEncoder( new StringSink( encoded ) ) );
                        // Decrypts the cipher
                        CBC_Mode<DES_EDE3>::Decryption d_triple_des;
                        d_triple_des.SetKeyWithIV(key_triple_des, key_triple_des.size(), iv_triple_des);
                        // The StreamTransformationFilter removes padding as required.
                        FileSource fs3_triple_des( cipher.c_str(), true, new StreamTransformationFilter(d_triple_des, new StringSink( rfilename ) ) ); 
                    } else {
                        cout << "No algorithm matches!" << endl;
                    }

                    // Stop the clock, calculate the time difference, turn to milliseconds
                    time_stop = clock();
                    time_difference = time_stop - time_start;
                    run_time = time_difference / ( CLOCKS_PER_SEC / 1000 );
                    time_data[i] = run_time;
                    cout << "time_data[" << i << "]: " << time_data[i] << " milliseconds" << endl;
                    time_average = time_average + time_data[i];
                }

                time_average = time_average / NUMBER_OF_RUNS;
                average_time_data[k][j] = time_average;
                cout << "Average[" << j << "]: " << average_time_data[k][j] << " milliseconds" << endl;
            }
        }
    } catch (const Exception& ex) {
        cerr << ex.what() << endl;
    }

    //Grab the data from the old file
    ifstream initial_cpp_time_data_file ( "cpp_time_data.txt" );
    if (initial_cpp_time_data_file.is_open() ) {
        while ( getline( initial_cpp_time_data_file, line_contents ) ) {
            initial_cpp_time_data = initial_cpp_time_data + line_contents;
            initial_cpp_time_data.push_back('\n');
        }
            initial_cpp_time_data_file.close();
    } else {
        initial_cpp_time_data = "";
    }

    // Created a new file
    ofstream time_data_file;
    time_data_file.open("cpp_time_data.txt");

    // Insert old data first
    if( !initial_cpp_time_data.empty() ) {
        time_data_file << initial_cpp_time_data << endl;
    }


    // Show the file the test ran on and insert the new data
    time_data_file << setw(10) << "#Bytes" << setw(10) << algorithm_name[BLOWFISH] << setw(10) << algorithm_name[AES] << setw(10) << algorithm_name[TRIPLE_DES] << '\n';
    cout << setw(10) << "#Bytes" << setw(10) << algorithm_name[BLOWFISH] << setw(10) << algorithm_name[AES] << setw(10) << algorithm_name[TRIPLE_DES] <<'\n';
    for( int j = 0; j < NUMBER_OF_FILES; j++ ) {
        cout << setw(10) << file_names_txt[j] << setw(10) << average_time_data[BLOWFISH][j] << setw(10) << average_time_data[AES][j] << setw(10) << average_time_data[TRIPLE_DES][j] << '\n';
        time_data_file << setw(10) << file_names_txt[j] << setw(10) << average_time_data[BLOWFISH][j] << setw(10) << average_time_data[AES][j] << setw(10) << average_time_data[TRIPLE_DES][j] << '\n';
    }
    time_data_file.close();
    cout << "Done!\n";

    return 0;
}

我知道这有点厚,但我真的很感激一些帮助。我一直被困在这一段时间,而我在智慧结束时。任何帮助,将不胜感激。为什么会这样?

1 个答案:

答案 0 :(得分:3)

看起来你的const名为AES,与CryptoPP库中名为AES的类型相冲突。