C#流读取器ReadToEnd()缺少最后一个字符

C#流读取器ReadToEnd()缺少最后一个字符,c#,streamreader,C#,Streamreader,我正在尝试使用AES解密C#中的字符串: public static string AesDecrypt(byte[] cipherText, byte[] Key, byte[] IV) { string plaintext = null; // Create an Aes object with the specified key and IV using Aes aesAlg = Aes.Create(); aesAlg.Padding = Padding

我正在尝试使用AES解密C#中的字符串:

public static string AesDecrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
    string plaintext = null;

    // Create an Aes object with the specified key and IV
    using Aes aesAlg = Aes.Create();
    aesAlg.Padding = PaddingMode.Zeros;
    aesAlg.Key = Key;
    aesAlg.IV = IV;

    // Create a decryptor to perform the stream transform
    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

    // Create the streams used for decryption
    using MemoryStream msDecrypt = new MemoryStream(cipherText);
    using CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
    using StreamReader srDecrypt = new StreamReader(csDecrypt);

    // Read the decrypted bytes from the decrypting stream and place them in a string
    plaintext = srDecrypt.ReadToEnd();
    return plaintext;
}
编码的数据是JSON,但是当我解密它时,我得到了所有正确的数据,除了JSON内容的结尾
}
丢失

我认为AES本身不是我的问题。我对未来有怀疑

plaintext = srDecrypt.ReadToEnd();
因为只缺少最后一个字符

我不知道是否应该明确地冲洗任何流,但无论如何,这是一个非常奇怪的问题

以下是加密的完整代码:

public static string AesEncrypt(string plainText, byte[] Key, byte[] IV)
{
    // Create an Aes object with the specified key and IV
    using Aes aesAlg = Aes.Create();
    aesAlg.Padding = PaddingMode.Zeros;
    aesAlg.Key = Key;
    aesAlg.IV = IV;

    // Create an encryptor to perform the stream transform
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

    // Create the streams used for encryption
    using MemoryStream msEncrypt = new MemoryStream();
    using CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
    using StreamWriter swEncrypt = new StreamWriter(csEncrypt);

    // Write all data to the stream
    swEncrypt.Write(plainText);
    swEncrypt.Flush();

    return Convert.ToBase64String(msEncrypt.ToArray());
}
这就是我如何调用解密方法的:

public static AuthenticationData ParseAuthenticationToken(string token)
{
    byte[] tokenBytes = Convert.FromBase64String(token);
    string json = AesEncryption.AesDecrypt(tokenBytes, aes.Key, aes.IV);
    return JsonConvert.DeserializeObject<AuthenticationData>(json);
}
公共静态身份验证数据ParseAuthenticationToken(字符串令牌)
{
byte[]tokenBytes=Convert.FromBase64String(令牌);
字符串json=AesEncryption.AESEdecrypt(tokenBytes,aes.Key,aes.IV);
返回JsonConvert.DeserializeObject(json);
}

问题在于您的加密代码。尽管您正在调用
seEncrypt.Flush()
,但您没有调用
csEncrypt.FlushFinalBlock()
。当流被释放时,这会自动发生,但在调用
msEncrypt.ToArray()
之前,您不会这样做。我会将代码改写为:

MemoryStream msEncrypt=newmemoryStream();
使用(CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
{
使用StreamWriter swEncrypt=新StreamWriter(csEncrypt);
书写(纯文本);
//swEncrypt在这里处理,刷新它。然后csEncrypt被处理,
//冲洗最后一块。
}
返回msEncrypt.ToArray();

您确定
密文中包含字符吗?
?我认为这不太可能是
StreamReader
问题。如果你一次只读一个字符,你能看到最后一个字符吗?什么在加密数据?@JonSkeet我的问题中包含了加密代码。