2012-11-11 176 views
3

我試圖讓此代碼正常工作。它是從Cryptopp AESAES和密鑰長度錯誤

Demonstrates encryption and decryption using AES in CTR

唯一的區別是,我創建函數encryptAESdecryptAES和插入的代碼。 它的工作原理沒有創建這些功能。但是現在我得到了以下錯誤:AES/CTR 4不是有效的密鑰長度,但密鑰長度爲16位。

string encryptAES(const byte key[], const string& plain, const byte iv[]) 
{ 
try 
{ 
    string cipher; 

    CTR_Mode<AES>::Encryption e; 
    e.SetKeyWithIV(key, sizeof(key), iv); 

    // The StreamTransformationFilter adds padding 
    // as required. ECB and CBC Mode must be padded 
    // to the block size of the cipher. 
    StringSource(plain, true, 
     new StreamTransformationFilter(e, 
      new StringSink(cipher) 
     ) // StreamTransformationFilter  
    ); // StringSource 
    return cipher; 
} 
catch(const CryptoPP::Exception& e) 
{ 
    cerr << e.what() << endl; 
    return ""; 
} 
} 

string decryptAES(const byte key[], const string& cipher, const byte iv[]) 
{ 
try 
{ 
    string recovered; 

    CTR_Mode<AES>::Decryption d; 
    d.SetKeyWithIV(key, sizeof(key), iv); 

    // The StreamTransformationFilter removes 
    // padding as required. 
    StringSource s(cipher, true, 
     new StreamTransformationFilter(d, 
      new StringSink(recovered) 
     ) // StreamTransformationFilter 
    ); // StringSource 
    return recovered; 
} 
catch(const CryptoPP::Exception& e) 
{ 
    cerr << e.what() << endl; 
    return ""; 
} 
} 

int main(int argc, char *argv[]) 
{ 
AutoSeededRandomPool prng; 

byte key[AES::DEFAULT_KEYLENGTH]; 
prng.GenerateBlock(key, sizeof(key)); 

byte iv[AES::BLOCKSIZE]; 
prng.GenerateBlock(iv, sizeof(iv)); 

string plain = "CTR Mode Test"; 
string encoded, cipher, recovered; 

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

// Pretty print key 
encoded.clear(); 
StringSource(key, sizeof(key), true, 
    new HexEncoder(
     new StringSink(encoded) 
    ) // HexEncoder 
); // StringSource 
cout << "key: " << encoded << endl; 

// Pretty print iv 
encoded.clear(); 
StringSource(iv, sizeof(iv), true, 
    new HexEncoder(
     new StringSink(encoded) 
    ) // HexEncoder 
); // StringSource 
cout << "iv: " << encoded << endl; 

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

cout << "plain text: " << plain << endl; 
cipher = encryptAES(key, plain, iv); 

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

// Pretty print 
encoded.clear(); 
StringSource(cipher, true, 
    new HexEncoder(
     new StringSink(encoded) 
    ) // HexEncoder 
); // StringSource 
cout << "cipher text: " << encoded << endl; 

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

recovered = decryptAES(key, cipher, iv); 
cout << "recovered text: " << recovered << endl; 

cin.sync(); 
cin.get(); 
} 
+1

您的函數正在計算基於退化指針大小的密鑰大小。即sizeof(key)!=密鑰數組中字節的大小。兩個函數都需要一個key_len附加參數。 – WhozCraig

回答

2

你的功能正在採取const byte key[]參數,基本上是作爲指針處理。因此sizeof(key)是平臺上指針的大小。

string encryptAES(const byte key[], const string& plain, const byte iv[]) 

// sizeof(key) is the size of a pointer 
e.SetKeyWithIV(key, sizeof(key), iv); 

可以使用std::vector<>作爲一個選項,或通過key_len,如:

string encryptAES(const byte key[], size_t key_len, const string& plain, const byte iv[]) 

// using key_len for the length of the key 
e.SetKeyWithIV(key, key_len, iv); 

我希望是有道理的,因爲同樣的錯誤是在幾個地方。

+0

非常感謝。就是這樣。你知道,我對C++很陌生。 ;) – nt2005

相關問題