1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/util/data_encryption_win.h"
12 #include "base/logging.h"
14 #pragma comment(lib, "crypt32.lib")
16 // TODO(akalin): Merge this with similar code in
17 // chrome/browser/password_manager/encryptor_win.cc. Preferably, all
18 // this stuff would live in crypto/.
22 std::vector
<uint8
> EncryptData(const std::string
& data
) {
23 DATA_BLOB unencrypted_data
= { 0 };
24 unencrypted_data
.pbData
= (BYTE
*)(data
.data());
25 unencrypted_data
.cbData
= data
.size();
26 DATA_BLOB encrypted_data
= { 0 };
28 if (!CryptProtectData(&unencrypted_data
, L
"", NULL
, NULL
, NULL
, 0,
30 LOG(ERROR
) << "Encryption fails: " << data
;
32 std::vector
<uint8
> result(encrypted_data
.pbData
,
33 encrypted_data
.pbData
+ encrypted_data
.cbData
);
34 LocalFree(encrypted_data
.pbData
);
38 bool DecryptData(const std::vector
<uint8
>& in_data
, std::string
* out_data
) {
39 DATA_BLOB encrypted_data
, decrypted_data
;
40 encrypted_data
.pbData
=
41 (in_data
.empty() ? NULL
: const_cast<BYTE
*>(&in_data
[0]));
42 encrypted_data
.cbData
= in_data
.size();
45 if (!CryptUnprotectData(&encrypted_data
, &descrip
, NULL
, NULL
, NULL
, 0,
47 LOG(ERROR
) << "Decryption fails: ";
50 out_data
->assign(reinterpret_cast<const char*>(decrypted_data
.pbData
),
51 decrypted_data
.cbData
);
52 LocalFree(decrypted_data
.pbData
);