Index: crypto/encryptor_win.cc |
diff --git a/crypto/encryptor_win.cc b/crypto/encryptor_win.cc |
index 088c9e56d680f9a0a9dddd2f666077f690bbd448..081ea6fbea4a901b39b1c66f5387f5281665aae1 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 { |
@@ -85,15 +84,18 @@ bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
DWORD total_len = data_len + block_size_; |
// 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) |
+ plaintext.CopyToString(ciphertext); |
+ |
+ BOOL ok = CryptEncrypt( |
+ capi_key_.get(), NULL, TRUE, 0, |
+ reinterpret_cast<BYTE*>(WriteInto(ciphertext, total_len+1)), |
wtc
2011/06/29 00:46:41
Add spaces around '+'.
It is safer to do things i
|
+ &data_len, total_len); |
+ if (!ok) { |
+ ciphertext->resize(0); |
return false; |
+ } |
- ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
+ ciphertext->resize(data_len); |
return true; |
} |
@@ -103,16 +105,19 @@ 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); |
- |
- BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); |
- if (!ok) |
+ // CryptoAPI encrypts/decrypts in place. |
+ ciphertext.CopyToString(plaintext); |
+ |
+ BOOL ok = CryptDecrypt( |
+ capi_key_.get(), NULL, TRUE, 0, |
+ reinterpret_cast<BYTE*>(WriteInto(plaintext, data_len+1)), |
+ &data_len); |
+ if (!ok) { |
+ plaintext->resize(0); |
return false; |
+ } |
- DCHECK_GT(tmp.size(), data_len); |
- |
- plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
+ plaintext->resize(data_len); |
return true; |
} |