2012-09-06 19 views
37

我一直在互聯網上尋找 C++ AES代碼示例/教程,教的加密技術的基礎知識和使用圖書館的,但到目前爲止,我還沒有運氣獲得體面材料。例加密+

好:易於理解(只是在旅途中學習的基礎知識)。

+0

你想了解如何使用庫或算法的基礎? –

+0

@MatteoItalia我需要爲我的項目使用AES,所以學習圖書館是必須的(因爲項目的最終期限)但是如果我可以在一路上挖掘一些知識,那將是非常棒的! – Yohannes

+0

http://www.cryptopp.com/wiki/Advanced_Encryption_Standard –

回答

60

Crypto++ AES的正式文件是一個好的開始。從我的存檔中,AES的基本實現如下:

請參考here更多解釋,我建議您先了解algorithm,然後嘗試逐步瞭解每一行。

#include <iostream> 
#include <iomanip> 

#include "modes.h" 
#include "aes.h" 
#include "filters.h" 

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

    //Key and IV setup 
    //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256- 
    //bit). This key is secretly exchanged between two parties before communication 
    //begins. DEFAULT_KEYLENGTH= 16 bytes 
    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ]; 
    memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH); 
    memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE); 

    // 
    // String and Sink setup 
    // 
    std::string plaintext = "Now is the time for all good men to come to the aide..."; 
    std::string ciphertext; 
    std::string decryptedtext; 

    // 
    // Dump Plain Text 
    // 
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl; 
    std::cout << plaintext; 
    std::cout << std::endl << std::endl; 

    // 
    // Create Cipher Text 
    // 
    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH); 
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv); 

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext)); 
    stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length() + 1); 
    stfEncryptor.MessageEnd(); 

    // 
    // Dump Cipher Text 
    // 
    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl; 

    for(int i = 0; i < ciphertext.size(); i++) { 

     std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " "; 
    } 

    std::cout << std::endl << std::endl; 

    // 
    // Decrypt 
    // 
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH); 
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv); 

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext)); 
    stfDecryptor.Put(reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.size()); 
    stfDecryptor.MessageEnd(); 

    // 
    // Dump Decrypted Text 
    // 
    std::cout << "Decrypted Text: " << std::endl; 
    std::cout << decryptedtext; 
    std::cout << std::endl << std::endl; 

    return 0; 
} 

有關安裝的詳細信息:

sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils

+1

我添加cryptlib.lib在附加的依賴關係,並試圖在VS2010上構建它,但得到了47個鏈接錯誤大部分如下: - 錯誤LNK2005:_tolower已經在MSVCRTD.lib(MSVCR100D.dll),有什麼幫助嗎? – Yohannes

+0

@ user1470033,我只是添加了一些安裝鏈接。 – berkay

+0

我不小心點擊了上次評論中的標誌圖標。我希望它不會傷害你的觀點!抱歉。 – 2013-03-11 05:13:35