Fabcoin Core  0.16.2
P2P Digital Currency
AES.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #include "AES.h"
23 #include <cryptopp/aes.h>
24 #include <cryptopp/filters.h>
25 #include <cryptopp/pwdbased.h>
26 #include <cryptopp/modes.h>
27 #include <cryptopp/sha.h>
28 
29 using namespace std;
30 using namespace dev;
31 using namespace dev::crypto;
32 using namespace CryptoPP;
33 
34 bytes dev::aesDecrypt(bytesConstRef _ivCipher, std::string const& _password, unsigned _rounds, bytesConstRef _salt)
35 {
36  bytes pw = asBytes(_password);
37 
38  if (!_salt.size())
39  _salt = &pw;
40 
41  bytes target(64);
42  CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA256>().DeriveKey(target.data(), target.size(), 0, pw.data(), pw.size(), _salt.data(), _salt.size(), _rounds);
43 
44  try
45  {
46  CryptoPP::AES::Decryption aesDecryption(target.data(), 16);
47  auto cipher = _ivCipher.cropped(16);
48  auto iv = _ivCipher.cropped(0, 16);
49  CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data());
50  std::string decrypted;
51  CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decrypted));
52  stfDecryptor.Put(cipher.data(), cipher.size());
53  stfDecryptor.MessageEnd();
54  return asBytes(decrypted);
55  }
56  catch (exception const& e)
57  {
58  cerr << e.what() << endl;
59  return bytes();
60  }
61 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Class file for modes of operation.
std::hash for asio::adress
Definition: Common.h:323
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:62
Class file for the AES cipher (Rijndael)
std::vector< byte > bytes
Definition: Common.h:75
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
size_t size() const
Definition: vector_ref.h:55
Classes for SHA-1 and SHA-2 family of message digests.
Implementation of BufferedTransformation&#39;s attachment interface.
Password based key derivation functions.
_T * data() const
Definition: vector_ref.h:51
bytes aesDecrypt(bytesConstRef _cipher, std::string const &_password, unsigned _rounds=2000, bytesConstRef _salt=bytesConstRef())
Definition: AES.cpp:34
#define e(i)
Definition: sha.cpp:733