2012-09-06 52 views
37

He estado buscando en Internet para buenas C++ AES código de ejemplo/tutorial que enseña los fundamentos de la tecnología de encriptación y el uso de la biblioteca, pero hasta ahora no he tenido suerte conseguir decente material.Ejemplo de AES usando Crypto ++

bien: Fácil de entender (solo los conceptos básicos para el estudio sobre la marcha).

+0

¿Quieres entender cómo utilizar el biblioteca o la base del algoritmo? –

+0

@MatteoItalia Necesito usar AES para mi proyecto, así que aprender la biblioteca es imprescindible (debido a los plazos del proyecto). Pero si pudiera obtener algunos conocimientos en el camino, ¡sería genial! – Yohannes

+0

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

Respuesta

60

Documento oficial de Crypto++ AES es un buen comienzo. Y de mi archivo, una implementación básica de AES es la siguiente:

Consulte here con más explicaciones, le recomiendo que primero comprenda el algorithm y luego intente comprender cada línea paso a paso.

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

Para detalles de la instalación:

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

+1

agregué cryptlib.lib en las dependencias adicionales y traté de compilarlo en VS2010 pero obtuve 47 errores de enlace la mayoría son como los siguientes: - error LNK2005: _tolower ya definido en MSVCRTD.lib (MSVCR100D.dll), alguna ayuda en eso? – Yohannes

+0

@ user1470033, acabo de agregar algunos enlaces de instalación. – berkay

+0

Accidentalmente hice clic en el icono de la bandera en su último comentario. ¡Espero que no haya dañado tus puntos! Lo siento. –