2012-09-06 2 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

실수로 마지막 코멘트에서 국기 아이콘을 클릭했습니다. 나는 그것이 당신의 요점을 해치지 않았 으면 좋겠다! 죄송합니다. –