繁体   English   中英

AES加密器不起作用

[英]AES encryptor not working

我正在尝试使此AES示例代码正常工作。 但是我什么都没有返回到我的cipherText变量。 我没有收到错误,只是什么也没有返回。 我在这里做错了什么?

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;
                }
            }
        }
    };


}

三种选择,它们都具有相同的作用,

调用csEncrypt.Close()或使用csEncrypt.FlushFinalBlock()将加密的数据刷新到内存流-在cipertext = msEncrypt.ToArray()之前调用它。

或者,移动cipher = msEncrypt.ToArray(); return cipertext; cipher = msEncrypt.ToArray(); return cipertext; 在您正在写入加密流的using块之外。

注意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

public override void Flush() 
{
     return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM