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.