일부 자습서를 읽은 후에이 코드를 비대칭 암호화로 작성했습니다.
#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();
}
}
[ "암호화"] (http://botan.randombit.net/#encryption) 페이지에서 계속 읽지 않으시겠습니까? –