1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// NOTE: this file is Winodws specific.
#include "sync/util/data_encryption_win.h"
#include <windows.h>
#include <wincrypt.h>
#include <cstddef>
#include <string>
#include <vector>
#include "base/logging.h"
#pragma comment(lib, "crypt32.lib")
// TODO(akalin): Merge this with similar code in
// chrome/browser/password_manager/encryptor_win.cc. Preferably, all
// this stuff would live in crypto/.
using std::string;
using std::vector;
vector<uint8> EncryptData(const string& data) {
DATA_BLOB unencrypted_data = { 0 };
unencrypted_data.pbData = (BYTE*)(data.data());
unencrypted_data.cbData = data.size();
DATA_BLOB encrypted_data = { 0 };
if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0,
&encrypted_data))
LOG(ERROR) << "Encryption fails: " << data;
vector<uint8> result(encrypted_data.pbData,
encrypted_data.pbData + encrypted_data.cbData);
LocalFree(encrypted_data.pbData);
return result;
}
bool DecryptData(const vector<uint8>& in_data, string* out_data) {
DATA_BLOB encrypted_data, decrypted_data;
encrypted_data.pbData =
(in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0]));
encrypted_data.cbData = in_data.size();
LPWSTR descrip = L"";
if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0,
&decrypted_data)) {
LOG(ERROR) << "Decryption fails: ";
return false;
} else {
out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData),
decrypted_data.cbData);
LocalFree(decrypted_data.pbData);
return true;
}
}
|