Ⅰ. はじめに
タイトルの通り「C++でAESの暗号化と複合を行う方法」です。
Ⅱ. やり方
1. botanをインストールする
vcpkg install botan
2. サンプルプログラムを書く
#include <iostream> #include <botan/pipe.h> #include <botan/key_filt.h> #include <botan/hex_filt.h> #include <botan/b64_filt.h> // #include <botan/hex.h> #include <botan/base64.h> using namespace Botan; std::string Encrypt(const std::string str) { // key: "1234567812345678" // iv: "8765432187654321" // SymmetricKey key(hex_decode("31323334353637383132333435363738")); // InitializationVector iv(hex_decode("38373635343332313837363534333231")); SymmetricKey key(base64_decode("MTIzNDU2NzgxMjM0NTY3OA==")); InitializationVector iv(base64_decode("ODc2NTQzMjE4NzY1NDMyMQ==")); // Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, ENCRYPTION), new Hex_Encoder); Pipe pipe(get_cipher("AES-128/CBC/PKCS7", key, iv, ENCRYPTION), new Base64_Encoder); pipe.process_msg(str); return pipe.read_all_as_string(); } std::string Decrypt(const std::string str) { // key: "1234567812345678" // iv: "8765432187654321" // SymmetricKey key(hex_decode("31323334353637383132333435363738")); // InitializationVector iv(hex_decode("38373635343332313837363534333231")); SymmetricKey key(base64_decode("MTIzNDU2NzgxMjM0NTY3OA==")); InitializationVector iv(base64_decode("ODc2NTQzMjE4NzY1NDMyMQ==")); // Pipe pipe(new Hex_Decoder, get_cipher("AES-128/CBC/PKCS7", key, iv, DECRYPTION)); Pipe pipe(new Base64_Decoder, get_cipher("AES-128/CBC/PKCS7", key, iv, DECRYPTION)); pipe.process_msg(str); return pipe.read_all_as_string(); } int main() { std::string str("Hello World"); str = Encrypt(str); std::cout << str << std::endl; str = Decrypt(str); std::cout << str << std::endl; return 0; }
実行結果
WbJILhgGP3zOYnFBSPMBow== Hello World