1 // Copyright 2014 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/os_crypt/os_crypt.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "crypto/wincrypt_shim.h"
12 #pragma comment(lib, "crypt32.lib")
14 bool OSCrypt::EncryptString16(const base::string16
& plaintext
,
15 std::string
* ciphertext
) {
16 return EncryptString(base::UTF16ToUTF8(plaintext
), ciphertext
);
19 bool OSCrypt::DecryptString16(const std::string
& ciphertext
,
20 base::string16
* plaintext
) {
22 if (!DecryptString(ciphertext
, &utf8
))
25 *plaintext
= base::UTF8ToUTF16(utf8
);
29 bool OSCrypt::EncryptString(const std::string
& plaintext
,
30 std::string
* ciphertext
) {
32 input
.pbData
= const_cast<BYTE
*>(
33 reinterpret_cast<const BYTE
*>(plaintext
.data()));
34 input
.cbData
= static_cast<DWORD
>(plaintext
.length());
37 BOOL result
= CryptProtectData(&input
, L
"", NULL
, NULL
, NULL
,
43 ciphertext
->assign(reinterpret_cast<std::string::value_type
*>(output
.pbData
),
46 LocalFree(output
.pbData
);
50 bool OSCrypt::DecryptString(const std::string
& ciphertext
,
51 std::string
* plaintext
) {
53 input
.pbData
= const_cast<BYTE
*>(
54 reinterpret_cast<const BYTE
*>(ciphertext
.data()));
55 input
.cbData
= static_cast<DWORD
>(ciphertext
.length());
58 BOOL result
= CryptUnprotectData(&input
, NULL
, NULL
, NULL
, NULL
,
63 plaintext
->assign(reinterpret_cast<char*>(output
.pbData
), output
.cbData
);
64 LocalFree(output
.pbData
);