简体   繁体   English

如何通过命令行参数分离botan加密和解密操作?

[英]How to separate botan encryption and decryption operation via command line argument?

i try to explore c++ cryptography using botan.我尝试使用 botan 探索 c++ 密码学。 From the provided example,the method of encrypt and decrypt a plaint text is shown below从提供的示例中,加密和解密明文的方法如下所示

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;
}  

I am curious is it possible to create command line argument to separate the operation?我很好奇是否可以创建命令行参数来分隔操作? The idea we store the encrypted text into a textfile, than we run the program using decryption argument and we read from the encrypted text to decrypt it into plaintext.我们将加密文本存储到文本文件中的想法,然后使用解密参数运行程序并从加密文本中读取以将其解密为明文。

#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;
}

The program has no issue with encryption(e argument) side but when i try to run this program with decryption side(d argument) i have encountered the following error:该程序在加密(e 参数)端没有问题,但是当我尝试使用解密端(d 参数)运行该程序时,我遇到了以下错误:

terminate called after throwing an instance of 'Botan::Decoding_Error'
  what():  Invalid CBC padding
Aborted (core dumped)

Let me knw if i do anything wrong, happy to learn from you all.如果我做错了什么,请告诉我,很高兴向大家学习。

The problem is that you are trying to decrypt the hex-encoding of the encrypted plaintext.问题是您正在尝试解密加密明文的十六进制编码。

You encrypt the plaintext and write the hex-encoding of the encryption to the file.您加密明文并将加密的十六进制编码写入文件。 For decryption, you read the hex-encoding into encordedText , and then you construct a Botan::secure_vector tmpPlainText from the data contained in encodedText .对于解密,您将十六进制编码读入encordedText ,然后从 encodedText 中包含的数据构造一个Botan::secure_vector tmpPlainText encodedText The hex-encoding is not undone in this step, so tmpPlainText still holds the hex-encoding.十六进制编码在此步骤中并未撤消,因此tmpPlainText仍保留十六进制编码。 The cipher then rejects this data, because it does not have the expected format.然后密码拒绝此数据,因为它不具有预期的格式。

To fix this problem, you have to undo the hex-encoding.要解决此问题,您必须撤消十六进制编码。 Replace the line where you construct the Botan::secure_vector with:将构造Botan::secure_vector的行替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM