I am trying to encrypt and decrypt a file (a text or whatsoever), so I decided to use the Crypto++. Below is my code.
crypt.h:
#ifndef CRYPT_HPP_
# define CRYPT_HPP_
# include <crypto++/aes.h>
# include <crypto++/osrng.h>
# include <crypto++/blowfish.h>
# include <crypto++/eax.h>
# include <crypto++/files.h>
# include <iostream>
using namespace CryptoPP;
using namespace std;
class Crypt
{
public:
Crypt() {};
~Crypt() {};
int init();
int encrypt(const string &file);
int decrypt(const string &file, int const);
AutoSeededRandomPool _randomGenerator;
SecByteBlock _aesKey, _aesIV;
};
#endif /* !CRYPT_HPP_ */
crypt.cpp:
int Crypt::init()
{
try
{
_aesKey.New(Blowfish::DEFAULT_KEYLENGTH);
_aesIV.New(Blowfish::BLOCKSIZE);
_randomGenerator.GenerateBlock(_aesKey, _aesKey.size());
_randomGenerator.GenerateBlock(_aesIV, _aesIV.size());
}
catch (CryptoPP::Exception &e)
{
cerr << e.what() << endl;
return (-1);
}
return (0);
}
The encryption and decryption is performed with:
int Crypt::encrypt(const string &fileToEncrypt)
{
EAX< Blowfish >::Encryption e1;
e1.SetKeyWithIV(_aesKey, _aesKey.size(), _aesIV);
string encryptedFile = "crypt.txt";
FileSource fs1(fileToEncrypt.c_str(), true,
new AuthenticatedEncryptionFilter(e1,
new FileSink(encryptedFile.c_str())));
return (0);
}
int Crypt::decrypt(const string &fileToDecrypt)
{
EAX< Blowfish >::Decryption e1;
e1.SetKeyWithIV(_aesKey, _aesKey.size(), _aesIV);
string finalFile = "decrypt.txt";
FileSource fs1(fileToDecrypt.c_str(), true,
new AuthenticatedEncryptionFilter(e1,
new FileSink(finalFile.c_str())));
return (0);
}
The problem is that when I decrypt and get the final file, I actually get the right output PLUS some weird binary stuff. Like this (encrypt then decrypt a Makefile, I skipped to the end as this is the most interesting part) :
fclean: clean
$(RM) $(NAME)
$(RM) $(TEST)
$(RM) -R $(OBJDIR)
re: fclean all
�ٌ[�MT̨z���,�o%
Did someone already face this problem ? Or can someone help me please ?
Thank you !