我正在尝试使用 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-2
但是当我这样做时:
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();