c++ - 如何通过命令行参数分隔botan加密和解密操作?

标签 c++ c++11 encryption cryptography botan

我尝试使用Botan探索C++密码学。从提供的示例中,加密和解密纯文本的方法如下所示

include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/cipher_mode.h>
#include <botan/hex.h>
#include <iostream>
int main()
{
    Botan::AutoSeeded_RNG rng;
    const std::string plaintext("Pa$$5523224lkj");
    const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
    const std::vector<uint8_t> decryptkey = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");

    std::unique_ptr<Botan::Cipher_Mode> enc = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::ENCRYPTION);
    std::unique_ptr<Botan::Cipher_Mode> dec = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::DECRYPTION);
    
    enc->set_key(key);
    //generate fresh nonce (IV)
    Botan::secure_vector<uint8_t> iv = rng.random_vec(enc->default_nonce_length());
    // Copy input data to a buffer that will be encrypted
    Botan::secure_vector<uint8_t> pt(plaintext.data(), plaintext.data()+plaintext.length());
    enc->start(iv);
    enc->finish(pt);
    std::cout << "enc->name() "<< enc->name()<<std::endl;
    std::cout << "Botan::hex_encode(iv) "<< Botan::hex_encode(iv) <<std::endl;
    std::cout << "Botan::hex_encode(pt) "<< Botan::hex_encode(pt) <<std::endl;

    dec->set_key(decryptkey);
    dec->start(iv);
    dec->finish(pt);
    std::cout <<pt.data()<<std::endl;  // we will printout Pa$$5523224lkj
    return 0;
}  
我很好奇是否可以创建命令行参数来分开操作?
我们将加密的文本存储到文本文件中的想法,然后使用解密参数运行程序,然后从加密的文本读取以将其解密为纯文本。
#include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/cipher_mode.h>
#include <botan/hex.h>
#include <fstream>
#include <iostream>

int main(int argc, char** argv)
{
    std::unique_ptr<Botan::Cipher_Mode> enc = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::ENCRYPTION);
    std::unique_ptr<Botan::Cipher_Mode> dec = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::DECRYPTION);

    
    const std::string plaintext("(5523224LOMAKDKWJDG#$%)");
    const std::string encText ="A9B7DC28Cdgjlpuy";
    
    Botan::secure_vector<uint8_t> myText(encText.data(), encText.data()+encText.length());
    Botan::secure_vector<uint8_t> iv = myText;
    
    Botan::secure_vector<uint8_t> pt (plaintext.data(), plaintext.data()+plaintext.length());
    std::string encordedText;
    
    const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");

    if(argv[1][1] == 'e')
    {
        std::ofstream myfile;
        myfile.open("encoded.txt");
        
        enc->set_key(key);
        
        enc->start(iv);
        enc->finish(pt);

        std::cout <<"enc->name()"<< enc->name() << " with iv " <<std::endl;
        std::cout<<"Botan::hex_encode(iv)"<<Botan::hex_encode(iv) <<std::endl; 
        std::cout<<"Botan::hex_encode(pt)"<<Botan::hex_encode(pt) << std::endl;
        myfile <<Botan::hex_encode(pt);
        myfile.close();
    }
    else if (argv[1][1] == 'd')
    {
        std::ifstream readfile; 
        readfile.open("encoded.txt");
        
        readfile>>encordedText;
        std::cout<<encordedText<<std::endl;
        Botan::secure_vector<uint8_t> tmpPlainText(encordedText.data(), encordedText.data()+encordedText.length());

        dec->set_key(key);
        dec->start(iv);
        dec->finish(tmpPlainText);
        std::cout<<tmpPlainText.data()<<std::endl;
        readfile.close();
    }   

    return 0;
}
该程序在加密(e参数)方面没有问题,但是当我尝试在解密侧(d参数)中运行此程序时,遇到了以下错误:
terminate called after throwing an instance of 'Botan::Decoding_Error'
  what():  Invalid CBC padding
Aborted (core dumped)
让我知道我做错了什么,很高兴向大家学习。

最佳答案

问题是您正在尝试解密加密明文的十六进制编码。
您对明文进行加密,然后将加密的十六进制编码写入文件。为了解密,您将十六进制编码读取为encordedText,然后从Botan::secure_vector tmpPlainText中包含的数据构造一个encodedText。在此步骤中,十六进制编码未撤消,因此tmpPlainText仍保留十六进制编码。密码然后拒绝此数据,因为它没有预期的格式。
要解决此问题,您必须撤消十六进制编码。将构造Botan::secure_vector的行替换为:

Botan::secure_vector<uint8_t> tmpPlainText(Botan::hex_decode_locked(encordedText));

关于c++ - 如何通过命令行参数分隔botan加密和解密操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63336800/

相关文章:

mysql - 无法在 RDS 实例上启用加密

java - 加密和解密应用程序 key

c++ - 如何获得 C++ 函数的大小?

c++ - 从 C++ 中与 C 灵活数组成员互操作的正确方法是什么?

c++ - 在基于范围的 for 循环中设置 vector 元素

javascript - 只打印字符串中的最后一个字符

c++ - Codeblocks 16.01 中警告被视为错误

C++ 数组处理

c++ - 可变参数函数中的 std::nullptr_t 参数

c++ - 为什么 std::is_function 对简单函数和 lambda 返回 false?