【问题标题】:Error encrypting files with Botan and Qt使用 Botan 和 Qt 加密文件时出错
【发布时间】:2014-01-01 22:59:26
【问题描述】:

我正在尝试使用“Botan”来加密和解密文件 (AES 256)。将库集成到 Qt 中已成功完成。我遵循了很多我在互联网上找到的类似 tutorial 的例子,但我得到了以下

error: 
class Botan::S2K' has no member named 'set_iterations'

我发现创建本教程的 Botan 版本已经过时,并且我使用的版本 (1.10.5) 不兼容。

我的问题是:
在哪里可以找到新版本的教程?如果不存在,我在哪里可以下载以前版本(1.8 或 1.9)的 windows 安装程序?

到目前为止,这是我的代码: (加密)

string file = "...";
string fileEncrypted = "...";

Botan::LibraryInitializer init;

string passphrase = "password";
AutoSeeded_RNG rng;
S2K* s2k = get_s2k("PBKDF2(SHA-256)");
s2k->set_iterations(4049);

SecureVector<byte> key_and_IV = s2k->derive_key(48, passphrase).bits_of();
SymmetricKey key(key_and_IV, 32);
InitializationVector iv(key_and_IV +32, 16);

std::ifstream in(file, std::ios::binary);
std::ofstream out(fileEncrypted, std::ios::binary);

Pipe pipe(get_cipher("AES-256/CBC", key, iv,ENCRYPTION),new DataSink_Stream(out));
pipe.start_msg();
in >> pipe;
pipe.end_msg();

【问题讨论】:

    标签: c++ qt cryptography botan


    【解决方案1】:

    你可以从here获取1.9版本,但是新版本的使用恐怕有两个问题:

    • get_s2k() 已过时,您应该改用get_pbkdf()

    • 当使用 PBKDF 而不是已弃用的 S2k 时,您可以将迭代次数传递给版本中的派生密钥,而不是使用 mutator 方法设置迭代。

    例如参见他们的 encrypt2 示例:

    ...
    PKCS5_PBKDF2 pbkdf2(new HMAC(new SHA_160));
    
    const u32bit PBKDF2_ITERATIONS = 8192;
    
    SecureVector<byte> salt(8);
    rng.randomize(&salt[0], salt.size());
    
    SecureVector<byte> master_key = pbkdf2.derive_key(48, passphrase,
                                                     &salt[0], salt.size(),
                                                     PBKDF2_ITERATIONS).bits_of()
    ...
    

    您可以查看doc/examples 文件夹中的更多示例以获取详细信息,然后获取它们的版本并解压缩。

    【讨论】:

    • 感谢答案。我按照 encrypt2 示例构建了我的程序,但是当我运行它时它崩溃了!我想 Botan 与 Qt 的集成毕竟不是没有错误的。
    • 我最终使用了这个站点的源代码:voidrealms.com/index.php?r=source/view&id=1 它使用了 Botan 的合并构建,并且与 Qt 5.2 完美配合(只需更改 toLatin1() 中已弃用的 toAscii() 函数)
    猜你喜欢
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2021-05-28
    • 1970-01-01
    • 2011-11-23
    • 2014-02-17
    • 2017-10-11
    相关资源
    最近更新 更多