2013-01-10 8 views
0

Botan을 사용하여 해시를 생성하고 AES 256으로 암호화를 수행하며 이제 비대칭 암호화를 수행하려고합니다. 이 페이지 읽기 : http://botan.randombit.net/pubkey.html RSA 암호화를위한 공용 및 개인 키를 생성하는 코드를 만들었지 만 데이터 암호화 및 해독 방법을 이해하지 못해 다른 사람이 나를 도와 줄 수 있습니까? Botan 1.8.8 2009-11-03을 사용하고 있습니다.Botan에서 비대칭 암호화를 수행하는 방법

void generatekey() 
{ 
    LibraryInitializer init; 

    std::ostringstream pub; 
    std::ostringstream priv; 

    int bits = 1024; 

    AutoSeeded_RNG rng; 

    RSA_PrivateKey key(rng, bits); 
    pub << X509::PEM_encode(key); 
    priv << PKCS8::PEM_encode(key); 

    qDebug() << QString(pub.str().c_str()); 
    qDebug() << QString(priv.str().c_str()); 
} 
+1

[ "암호화"] (http://botan.randombit.net/#encryption) 페이지에서 계속 읽지 않으시겠습니까? –

답변

6

일부 자습서를 읽은 후에이 코드를 비대칭 암호화로 작성했습니다.

#include <QDebug> 
#include <botan/botan.h> 
#include <botan/rsa.h> 
#include <botan/look_pk.h> 

using namespace Botan; 

void encryptdata() 
{ 
    try 
    { 
     QString text = "abc"; 

     LibraryInitializer init; 

     AutoSeeded_RNG rng; 

     RSA_PrivateKey key(rng, 1024); 

     std::string pub = X509::PEM_encode(key); 

     std::string priv = PKCS8::PEM_encode(key); 

     DataSource_Memory key_pub(pub); 

     DataSource_Memory key_priv(priv); 

     X509_PublicKey *pub_rsa = X509::load_key(key_pub); 

     PKCS8_PrivateKey *priv_rsa = PKCS8::load_key(key_priv, rng); 

     PK_Encrypting_Key *enckey = dynamic_cast<PK_Encrypting_Key*>(pub_rsa); 

     PK_Decrypting_Key *deckey = dynamic_cast<PK_Decrypting_Key*>(priv_rsa); 

     PK_Encryptor *enc = get_pk_encryptor(*enckey, "EME1(SHA-256)"); 

     PK_Decryptor *dec = get_pk_decryptor(*deckey, "EME1(SHA-256)"); 

     QByteArray array = text.toLatin1(); 

     byte msgtoencrypt[array.count()]; 

     for (int i = 0; i < array.count(); i++) 
     { 
      msgtoencrypt[i] = array[i]; 
     } 

     SecureVector<byte> ciphertext = enc->encrypt(msgtoencrypt, sizeof(msgtoencrypt), rng); 

     SecureVector<byte> plaintext = dec->decrypt(ciphertext, ciphertext.size()); 

     QByteArray encrypted; 

     for (uint i = 0; i < ciphertext.size(); i++) 
     { 
      encrypted[i] = ciphertext[i]; 
     } 

     QByteArray result; 

     for (uint i = 0; i < plaintext.size(); i++) 
     { 
      result[i] = plaintext[i]; 
     } 

     if (array == result) 
     { 
      qDebug() << "Ok"; 
     } 
     else 
     { 
      qDebug() << "Error"; 
     } 

     qDebug() << QString(encrypted); 
     qDebug() << QString(array); 
     qDebug() << QString(result); 
    } 
    catch(std::exception &e) 
    { 
     qDebug() << e.what(); 
    } 
}