1 // Copyright (c) 2006-2008 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 "components/webdata/encryptor/encryptor.h"
9 #include "base/strings/utf_string_conversions.h"
11 #pragma comment(lib, "crypt32.lib")
13 bool Encryptor::EncryptString16(const base::string16
& plaintext
,
14 std::string
* ciphertext
) {
15 return EncryptString(base::UTF16ToUTF8(plaintext
), ciphertext
);
18 bool Encryptor::DecryptString16(const std::string
& ciphertext
,
19 base::string16
* plaintext
) {
21 if (!DecryptString(ciphertext
, &utf8
))
24 *plaintext
= base::UTF8ToUTF16(utf8
);
28 bool Encryptor::EncryptString(const std::string
& plaintext
,
29 std::string
* ciphertext
) {
31 input
.pbData
= const_cast<BYTE
*>(
32 reinterpret_cast<const BYTE
*>(plaintext
.data()));
33 input
.cbData
= static_cast<DWORD
>(plaintext
.length());
36 BOOL result
= CryptProtectData(&input
, L
"", NULL
, NULL
, NULL
,
42 ciphertext
->assign(reinterpret_cast<std::string::value_type
*>(output
.pbData
),
45 LocalFree(output
.pbData
);
49 bool Encryptor::DecryptString(const std::string
& ciphertext
,
50 std::string
* plaintext
) {
52 input
.pbData
= const_cast<BYTE
*>(
53 reinterpret_cast<const BYTE
*>(ciphertext
.data()));
54 input
.cbData
= static_cast<DWORD
>(ciphertext
.length());
57 BOOL result
= CryptUnprotectData(&input
, NULL
, NULL
, NULL
, NULL
,
62 plaintext
->assign(reinterpret_cast<char*>(output
.pbData
), output
.cbData
);
63 LocalFree(output
.pbData
);