[英]AES encryptor not working
I am attempting to get this AES sample code working. 我正在尝试使此AES示例代码正常工作。 However I am not getting anything returned to my cipherText variable.
但是我什么都没有返回到我的cipherText变量。 I am not getting errors, just nothing returned.
我没有收到错误,只是什么也没有返回。 What am I doing wrong here?
我在这里做错了什么?
public byte[] key { get; set; }
public byte[] IV { get; set; }
public byte[] ciphertext { get; set; }
public string plainText { get; set; }
public byte[] Encrypt(string InputPlaintext)
{
InputPlaintext = "attack at dawn";
using (AesCryptoServiceProvider AESEncryptor = new AesCryptoServiceProvider())
{
////using the AesCryptoServiceProvider to generate the IV and Key
key = AESEncryptor.Key;
IV = AESEncryptor.IV;
ICryptoTransform encryptor = AESEncryptor.CreateEncryptor(key, IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(InputPlaintext);
ciphertext = msEncrypt.ToArray();
return ciphertext;
}
}
}
};
}
Three options, all of which do the same thing, 三种选择,它们都具有相同的作用,
Either call csEncrypt.Close()
or use csEncrypt.FlushFinalBlock()
to flush the encrypted data to the memory stream - call it before cipertext = msEncrypt.ToArray()
. 调用
csEncrypt.Close()
或使用csEncrypt.FlushFinalBlock()
将加密的数据刷新到内存流-在cipertext = msEncrypt.ToArray()
之前调用它。
Or, move cipher = msEncrypt.ToArray(); return cipertext;
或者,移动
cipher = msEncrypt.ToArray(); return cipertext;
cipher = msEncrypt.ToArray(); return cipertext;
outside the using block where you're writing to the crypto stream. 在您正在写入加密流的using块之外。
Note csEncrypt.Flush()
which might be the first guess does nothing.. 注意
csEncrypt.Flush()
可能是第一个猜测,它什么也不做。
http://reflector.webtropy.com/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/clr/src/BCL/System/Security/Cryptography/CryptoStream@cs/1/CryptoStream@cs http://reflector.webtropy.com/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/clr/src/BCL/System/Security/Cryptography/CryptoStream@cs/1/CryptoStream@ cs
public override void Flush()
{
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.