2012-09-06 49 views
37

Tôi đã được tìm kiếm trên Internet cho tốt C++ mẫu mã AES/hướng dẫn mà dạy những điều cơ bản của công nghệ mã hóa và việc sử dụng các thư viện nhưng cho đến nay tôi đã không có may mắn nhận được phong nha vật chất.Ví dụ về AES sử dụng Crypto ++

tốt: Dễ hiểu (Chỉ cần những điều cơ bản để đi học).

+0

Bạn có muốn hiểu làm thế nào để sử dụng thư viện hoặc cơ sở của thuật toán? –

+0

@MatteoItalia Tôi cần sử dụng AES cho dự án của mình để học thư viện là phải (vì thời hạn dự án) Nhưng nếu tôi có thể tìm hiểu một số kiến ​​thức theo cách sẽ tuyệt vời! – Yohannes

+0

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

Trả lời

60

Tài liệu chính thức của Crypto++ AES là một khởi đầu tốt. Và từ kho lưu trữ của tôi, việc triển khai cơ bản AES như sau:

Vui lòng tham khảo here giải thích thêm, tôi khuyên bạn trước tiên hãy hiểu algorithm và sau đó cố gắng hiểu từng bước từng bước.

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

Để biết chi tiết cài đặt:

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

+1

tôi đã thêm cryptlib.lib vào các phụ thuộc bổ sung và cố gắng xây dựng nó trên VS2010 nhưng có 47 lỗi liên kết nhất là như sau: - lỗi LNK2005: _tolower đã được định nghĩa trong MSVCRTD.lib (MSVCR100D.dll), có giúp đỡ gì không? – Yohannes

+0

@ user1470033, tôi vừa thêm một số liên kết cài đặt. – berkay

+0

Tôi vô tình nhấp vào biểu tượng Lá cờ trong nhận xét cuối cùng của bạn. Tôi hy vọng nó không gây hại cho điểm của bạn! Lấy làm tiếc. –

Các vấn đề liên quan