1
我試圖使用AES 128 CBC加密字符串,我一直在這裏跟隨MSDN例如: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2MemoryStream-> ToArray的()返回null
但是當我做:
encKey = msEncrypt->ToArray();
調試時,我的數組長度爲零。爲什麼encKey陣列沒有加密輸出? 執行代碼:
ICryptoTransform^ encryptor = encAES->CreateEncryptor();
MemoryStream^ msEncrypt = gcnew MemoryStream();
CryptoStream^ csEncrypt = gcnew CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write);
StreamWriter^ swEncrypt = gcnew StreamWriter(csEncrypt);
swEncrypt->Write(publicKey);
encKey = msEncrypt->ToArray();
感謝您的任何幫助。
解決方案:
ICryptoTransform^ encryptor = encAES->CreateEncryptor();
MemoryStream^ msEncrypt = gcnew MemoryStream();
CryptoStream^ csEncrypt = gcnew CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write);
StreamWriter^ swEncrypt = gcnew StreamWriter(csEncrypt);
swEncrypt->Write(publicKey);
swEncrypt->Close();
csEncrypt->Close();
encKey = msEncrypt->ToArray();
msEncrypt->Close();
嗨,碳,您是否在閱讀我的答案後解決了問題? –
我相信它!謝謝,我想知道爲什麼這個例子沒有包含flush。 編輯:抱歉,我不能upvote你。 –
在'encKey = msEncrypt-> ToArray()'之前,我用'swEncrypt-> Close()'替換了'swEncrypt-> Flush()''好像Flush沒有寫入填充信息。 –