1

i try to explore c++ cryptography using botan. 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:

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.

KJ L
  • 115
  • 2
  • 14
  • Which command you enter in command line exactly? – TonySalimi Aug 10 '20 at 10:14
  • @Gupta initially i execute ./a.out -e , after that i execute ./a.out -d , than the terminate called after throwing an instance of 'Botan::Decoding_Error' what(): Invalid CBC padding Aborted (core dumped) is shown. I have attached the pastebin https://pastebin.com/vfs70PBw – KJ L Aug 10 '20 at 13:20

1 Answers1

1

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. The hex-encoding is not undone in this step, so tmpPlainText still holds the hex-encoding. 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<uint8_t> tmpPlainText(Botan::hex_decode_locked(encordedText));