From 8dc374a1bb5f219f4eaacbf94b8c3a0dde9d4b41 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Tue, 13 Dec 2016 23:48:02 +0000 Subject: [PATCH] fix aes decryption --- source/detail/xlsx_crypto.cpp | 56 +++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/source/detail/xlsx_crypto.cpp b/source/detail/xlsx_crypto.cpp index abc998c1..7df0dd55 100644 --- a/source/detail/xlsx_crypto.cpp +++ b/source/detail/xlsx_crypto.cpp @@ -126,46 +126,50 @@ struct crypto_helper static std::vector aes(const std::vector &key, const std::vector &iv, - const std::vector &encrypted, + const std::vector &source, cipher_chaining chaining, cipher_direction direction) { - std::vector destination(encrypted.size(), 0); + std::vector destination(source.size(), 0); 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(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::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data()); - CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); - stfEncryptor.Put(reinterpret_cast(encrypted.data()), encrypted.size()); - stfEncryptor.MessageEnd(); + CryptoPP::ArraySource as(source.data(), source.size(), true, + new CryptoPP::StreamTransformationFilter(cbcEncryption, + 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) { CryptoPP::AES::Encryption aesEncryption(key.data(), key.size()); CryptoPP::ECB_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv.data()); - CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); - stfEncryptor.Put(reinterpret_cast(encrypted.data()), encrypted.size()); - stfEncryptor.MessageEnd(); + CryptoPP::ArraySource as(source.data(), source.size(), true, + new CryptoPP::StreamTransformationFilter(cbcEncryption, + new CryptoPP::ArraySink(destination.data(), destination.size()), + CryptoPP::BlockPaddingSchemeDef::NO_PADDING)); } else if (direction == cipher_direction::decryption && chaining == cipher_chaining::ecb) { - CryptoPP::AES::Encryption aesEncryption(key.data(), key.size()); - CryptoPP::ECB_Mode_ExternalCipher::Decryption cbcEncryption(aesEncryption, iv.data()); + CryptoPP::AES::Decryption aesDecryption(key.data(), key.size()); + CryptoPP::ECB_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data()); - CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::ArraySink(destination.data(), destination.size())); - stfEncryptor.Put(reinterpret_cast(encrypted.data()), encrypted.size()); - stfEncryptor.MessageEnd(); + CryptoPP::ArraySource as(source.data(), source.size(), true, + new CryptoPP::StreamTransformationFilter(cbcDecryption, + new CryptoPP::ArraySink(destination.data(), destination.size()), + CryptoPP::BlockPaddingSchemeDef::NO_PADDING)); } return destination; @@ -176,7 +180,7 @@ struct crypto_helper CryptoPP::Base64Decoder decoder; decoder.Put(reinterpret_cast(encoded.data()), encoded.size()); decoder.MessageEnd(); - + std::vector decoded(decoder.MaxRetrievable(), 0); decoder.Get(decoded.data(), decoded.size()); @@ -188,7 +192,7 @@ struct crypto_helper CryptoPP::Base64Decoder encoder; encoder.Put(reinterpret_cast(decoded.data()), decoded.size()); encoder.MessageEnd(); - + std::vector encoded(encoder.MaxRetrievable(), 0); encoder.Get(encoded.data(), encoded.size()); @@ -199,7 +203,7 @@ struct crypto_helper const std::vector &input) { std::vector digest; - + if (algorithm == hash_algorithm::sha512) { CryptoPP::SHA512 sha512; @@ -212,7 +216,7 @@ struct crypto_helper digest.resize(CryptoPP::SHA1::DIGESTSIZE, 0); sha1.CalculateDigest(digest.data(), input.data(), input.size()); } - + return digest; }