Index: crypto/encryptor_win.cc |
diff --git a/crypto/encryptor_win.cc b/crypto/encryptor_win.cc |
index 088c9e56d680f9a0a9dddd2f666077f690bbd448..1c71f12f6dbaa34ef0ccd7150e08334691278235 100644 |
--- a/crypto/encryptor_win.cc |
+++ b/crypto/encryptor_win.cc |
@@ -4,8 +4,7 @@ |
#include "crypto/encryptor.h" |
-#include <vector> |
- |
+#include "base/string_util.h" |
#include "crypto/symmetric_key.h" |
namespace crypto { |
@@ -83,17 +82,21 @@ bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
std::string* ciphertext) { |
DWORD data_len = plaintext.size(); |
DWORD total_len = data_len + block_size_; |
+ CHECK_GT(total_len, data_len); |
// CryptoAPI encrypts/decrypts in place. |
- std::vector<BYTE> tmp(total_len); |
- memcpy(&tmp[0], plaintext.data(), data_len); |
- |
- BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], |
- &data_len, total_len); |
- if (!ok) |
+ char* ciphertext_data = WriteInto(ciphertext, total_len + 1); |
+ plaintext.copy(ciphertext_data, plaintext.size(), 0); |
wtc
2011/07/07 01:03:12
Note: This comment also applies to the ciphertext.
|
+ |
+ BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, |
+ reinterpret_cast<BYTE*>(ciphertext_data), &data_len, |
+ total_len); |
+ if (!ok) { |
+ ciphertext->clear(); |
return false; |
+ } |
- ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
+ ciphertext->resize(data_len); |
return true; |
} |
@@ -103,16 +106,18 @@ bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
if (data_len == 0) |
return false; |
- std::vector<BYTE> tmp(data_len); |
- memcpy(&tmp[0], ciphertext.data(), data_len); |
+ // CryptoAPI encrypts/decrypts in place. |
+ char* plaintext_data = WriteInto(plaintext, data_len + 1); |
+ ciphertext.copy(plaintext_data, ciphertext.size(), 0); |
- BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); |
- if (!ok) |
+ BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, |
+ reinterpret_cast<BYTE*>(plaintext_data), &data_len); |
+ if (!ok) { |
+ plaintext->clear(); |
return false; |
+ } |
- DCHECK_GT(tmp.size(), data_len); |
- |
- plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
+ plaintext->resize(data_len); |
return true; |
} |