美文网首页C/CPLUS
c++ botan库AES-128加密

c++ botan库AES-128加密

作者: 一路向后 | 来源:发表于2022-03-09 21:58 被阅读0次

    1.源码实现

    #include <iostream>
    #include <string>
    #include <botan/botan.h>
    
    using namespace std;
    using namespace Botan;
    
    /*加解密*/
    string cryptoAES128(string input, string passphrase, Cipher_Dir opt)
    {
        HashFunction *hash = get_hash("MD5");
        SymmetricKey key = hash->process(passphrase);
        SecureVector<byte> raw_iv = hash->process('0' + passphrase);
        InitializationVector iv(raw_iv);
    
        Pipe pipe(get_cipher("AES-128/CBC", key, iv, opt));
    
        pipe.process_msg(input);
    
        string output = pipe.read_all_as_string();
    
        return output;
    }
    
    int main()
    {
        string input = "你好, 世界!";
        string key = "123456";
        string output;
        string plain;
    
        /*测试AES-128加密*/
        output = cryptoAES128(input, key, ENCRYPTION);
    
        cout << "密文大小: " << output.size() << endl;
    
        /*测试AES-128解密*/
        plain = cryptoAES128(output, key, DECRYPTION);
    
        cout << "明文内容: " << plain << endl;
    
        return 0;
    }
    

    2.编译源码

    $ g++ -o test test.cpp -std=c++11 -I/usr/local/include/botan-2 -Wno-cpp -Wno-deprecated-declarations -lbotan-2
    

    3.运行及其结果

    $ ./test
    密文大小: 16
    明文内容: 你好, 世界!
    

    相关文章

      网友评论

        本文标题:c++ botan库AES-128加密

        本文链接:https://www.haomeiwen.com/subject/hjexdrtx.html