【问题标题】:How to separate botan encryption and decryption operation via command line argument?如何通过命令行参数分离botan加密和解密操作?
【发布时间】:2020-11-29 20:34:48
【问题描述】:

我尝试使用 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)

如果我做错了什么,请告诉我,很高兴向大家学习。

【问题讨论】:

  • 您在命令行中输入的究竟是哪个命令?
  • @Gupta 最初我执行 ./a.out -e ,然后我执行 ./a.out -d ,然后在抛出 'Botan::Decoding_Error' 实例后调用终止什么( ): 显示无效的 CBC 填充 Aborted (core dumped)。我已附上 pastebin pastebin.com/vfs70PBw

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


【解决方案1】:

问题是你试图解密加密明文的十六进制编码。

您加密明文并将加密的十六进制编码写入文件。对于解密,您将十六进制编码读入encordedText,然后根据encodedText 中包含的数据构造Botan::secure_vector tmpPlainText。十六进制编码在此步骤中并未撤消,因此tmpPlainText 仍保留十六进制编码。然后密码拒绝此数据,因为它不具有预期的格式。

要解决此问题,您必须撤消十六进制编码。将构造 Botan::secure_vector 的行替换为:

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

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多