From 62b4ff7dd21227c7c03a7533dc232fdd976cd186 Mon Sep 17 00:00:00 2001 From: Yanghello Date: Mon, 1 Jun 2020 22:06:18 +0800 Subject: [PATCH] Aes_cipher_test and cipher_utils_test failed fixed (#24816) --- CMakeLists.txt | 2 +- cmake/external/cryptopp.cmake | 3 ++- paddle/fluid/framework/io/crypto/aes_cipher.cc | 13 +++++-------- paddle/fluid/framework/io/crypto/cipher_utils.cc | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae2a9161eb..d79f345886 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ${WITH_DISTRIBUTE} option(SANITIZER_TYPE "Choose the type of sanitizer, options are: Address, Leak, Memory, Thread, Undefined" OFF) option(WITH_LITE "Compile Paddle Fluid with Lite Engine" OFF) option(WITH_NCCL "Compile PaddlePaddle with NCCL support" ON) -option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" OFF) +option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" ON) # PY_VERSION if(NOT PY_VERSION) diff --git a/cmake/external/cryptopp.cmake b/cmake/external/cryptopp.cmake index 70e3954c90..a3ca8d69a4 100644 --- a/cmake/external/cryptopp.cmake +++ b/cmake/external/cryptopp.cmake @@ -68,7 +68,8 @@ ExternalProject_Add( SOURCE_DIR ${CRYPTOPP_SOURCE_DIR} PATCH_COMMAND COMMAND ${CMAKE_COMMAND} -E remove_directory "/cmake/" - COMMAND git clone -b ${CRYPTOPP_TAG} https://github.com/noloader/cryptopp-cmake "/cmake" + COMMAND git clone https://github.com/noloader/cryptopp-cmake "/cmake" + COMMAND cd "/cmake" && git checkout tags/${CRYPTOPP_TAG} -b ${CRYPTOPP_TAG} COMMAND ${CMAKE_COMMAND} -E copy_directory "/cmake/" "/" INSTALL_DIR ${CRYPTOPP_INSTALL_DIR} CMAKE_ARGS ${CRYPTOPP_CMAKE_ARGS} diff --git a/paddle/fluid/framework/io/crypto/aes_cipher.cc b/paddle/fluid/framework/io/crypto/aes_cipher.cc index c5b1f1d74a..18c0091b65 100644 --- a/paddle/fluid/framework/io/crypto/aes_cipher.cc +++ b/paddle/fluid/framework/io/crypto/aes_cipher.cc @@ -60,7 +60,6 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext, CryptoPP::member_ptr m_cipher; CryptoPP::member_ptr m_filter; bool need_iv = false; - std::string iv = ""; const unsigned char* key_char = reinterpret_cast(&(key.at(0))); BuildCipher(true, &need_iv, &m_cipher, &m_filter); @@ -77,7 +76,7 @@ std::string AESCipher::EncryptInternal(const std::string& plaintext, m_filter->Attach(new CryptoPP::StringSink(ciphertext)); CryptoPP::StringSource(plaintext, true, new CryptoPP::Redirector(*m_filter)); if (need_iv) { - ciphertext = iv_.append(ciphertext); + return iv_ + ciphertext; } return ciphertext; @@ -88,7 +87,6 @@ std::string AESCipher::DecryptInternal(const std::string& ciphertext, CryptoPP::member_ptr m_cipher; CryptoPP::member_ptr m_filter; bool need_iv = false; - std::string iv = ""; const unsigned char* key_char = reinterpret_cast(&(key.at(0))); BuildCipher(false, &need_iv, &m_cipher, &m_filter); @@ -115,7 +113,6 @@ std::string AESCipher::AuthenticatedEncryptInternal( CryptoPP::member_ptr m_cipher; CryptoPP::member_ptr m_filter; bool need_iv = false; - std::string iv = ""; const unsigned char* key_char = reinterpret_cast(&(key.at(0))); BuildAuthEncCipher(&need_iv, &m_cipher, &m_filter); @@ -143,7 +140,6 @@ std::string AESCipher::AuthenticatedDecryptInternal( CryptoPP::member_ptr m_cipher; CryptoPP::member_ptr m_filter; bool need_iv = false; - std::string iv = ""; const unsigned char* key_char = reinterpret_cast(&(key.at(0))); BuildAuthDecCipher(&need_iv, &m_cipher, &m_filter); @@ -266,14 +262,15 @@ std::string AESCipher::Decrypt(const std::string& ciphertext, void AESCipher::EncryptToFile(const std::string& plaintext, const std::string& key, const std::string& filename) { - std::ofstream fout(filename); - fout << this->Encrypt(plaintext, key); + std::ofstream fout(filename, std::ios::binary); + std::string ciphertext = this->Encrypt(plaintext, key); + fout.write(ciphertext.data(), ciphertext.size()); fout.close(); } std::string AESCipher::DecryptFromFile(const std::string& key, const std::string& filename) { - std::ifstream fin(filename); + std::ifstream fin(filename, std::ios::binary); std::string ciphertext{std::istreambuf_iterator(fin), std::istreambuf_iterator()}; fin.close(); diff --git a/paddle/fluid/framework/io/crypto/cipher_utils.cc b/paddle/fluid/framework/io/crypto/cipher_utils.cc index b8a2c5419b..e0c653e001 100644 --- a/paddle/fluid/framework/io/crypto/cipher_utils.cc +++ b/paddle/fluid/framework/io/crypto/cipher_utils.cc @@ -43,7 +43,7 @@ std::string CipherUtils::GenKeyToFile(int length, const std::string& filename) { // CryptoPP::byte key[length]; prng.GenerateBlock(reinterpret_cast(&(rng.at(0))), rng.size()); - std::ofstream fout(filename); + std::ofstream fout(filename, std::ios::binary); PADDLE_ENFORCE_EQ(fout.is_open(), true, paddle::platform::errors::Unavailable( "Failed to open file : %s, " -- GitLab