fix aes decryption

This commit is contained in:
Thomas Fussell 2016-12-13 23:48:02 +00:00
parent b9a02916cf
commit 8dc374a1bb

View File

@ -126,46 +126,50 @@ struct crypto_helper
static std::vector<std::uint8_t> aes(const std::vector<std::uint8_t> &key, static std::vector<std::uint8_t> aes(const std::vector<std::uint8_t> &key,
const std::vector<std::uint8_t> &iv, const std::vector<std::uint8_t> &iv,
const std::vector<std::uint8_t> &encrypted, const std::vector<std::uint8_t> &source,
cipher_chaining chaining, cipher_direction direction) cipher_chaining chaining, cipher_direction direction)
{ {
std::vector<std::uint8_t> destination(encrypted.size(), 0); std::vector<std::uint8_t> destination(source.size(), 0);
if (direction == cipher_direction::encryption && chaining == cipher_chaining::cbc) if (direction == cipher_direction::encryption && chaining == cipher_chaining::cbc)
{
CryptoPP::AES::Decryption aesEncryption(key.data(), key.size());
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data());
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size()));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(encrypted.data()), encrypted.size());
stfEncryptor.MessageEnd();
}
else if (direction == cipher_direction::decryption && chaining == cipher_chaining::cbc)
{ {
CryptoPP::AES::Encryption aesEncryption(key.data(), key.size()); CryptoPP::AES::Encryption aesEncryption(key.data(), key.size());
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data()); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data());
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); CryptoPP::ArraySource as(source.data(), source.size(), true,
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(encrypted.data()), encrypted.size()); new CryptoPP::StreamTransformationFilter(cbcEncryption,
stfEncryptor.MessageEnd(); new CryptoPP::ArraySink(destination.data(), destination.size()),
CryptoPP::BlockPaddingSchemeDef::NO_PADDING));
}
else if (direction == cipher_direction::decryption && chaining == cipher_chaining::cbc)
{
CryptoPP::AES::Decryption aesDecryption(key.data(), key.size());
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data());
CryptoPP::ArraySource as(source.data(), source.size(), true,
new CryptoPP::StreamTransformationFilter(cbcDecryption,
new CryptoPP::ArraySink(destination.data(), destination.size()),
CryptoPP::BlockPaddingSchemeDef::NO_PADDING));
} }
else if (direction == cipher_direction::encryption && chaining == cipher_chaining::ecb) else if (direction == cipher_direction::encryption && chaining == cipher_chaining::ecb)
{ {
CryptoPP::AES::Encryption aesEncryption(key.data(), key.size()); CryptoPP::AES::Encryption aesEncryption(key.data(), key.size());
CryptoPP::ECB_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data()); CryptoPP::ECB_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data());
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); CryptoPP::ArraySource as(source.data(), source.size(), true,
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(encrypted.data()), encrypted.size()); new CryptoPP::StreamTransformationFilter(cbcEncryption,
stfEncryptor.MessageEnd(); new CryptoPP::ArraySink(destination.data(), destination.size()),
CryptoPP::BlockPaddingSchemeDef::NO_PADDING));
} }
else if (direction == cipher_direction::decryption && chaining == cipher_chaining::ecb) else if (direction == cipher_direction::decryption && chaining == cipher_chaining::ecb)
{ {
CryptoPP::AES::Encryption aesEncryption(key.data(), key.size()); CryptoPP::AES::Decryption aesDecryption(key.data(), key.size());
CryptoPP::ECB_Mode_ExternalCipher::Decryption cbcEncryption(aesEncryption, iv.data()); CryptoPP::ECB_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data());
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); CryptoPP::ArraySource as(source.data(), source.size(), true,
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(encrypted.data()), encrypted.size()); new CryptoPP::StreamTransformationFilter(cbcDecryption,
stfEncryptor.MessageEnd(); new CryptoPP::ArraySink(destination.data(), destination.size()),
CryptoPP::BlockPaddingSchemeDef::NO_PADDING));
} }
return destination; return destination;