我的解密显示错误:Buffer不能为null。参数名称:buffer。请帮我解决这个问题。 [英] My decryption show that error : Buffer cannot be null. Parameter name: buffer. Please help me to solve this problem.

查看:222
本文介绍了我的解密显示错误:Buffer不能为null。参数名称:buffer。请帮我解决这个问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;//use to run the encryption
using System.Text;// using ASCII language

public partial class About : System.Web.UI.Page
{
    string cipherData;
    byte[] cipherByte;
    byte[] textByte1;
    byte[] textByte2;
    byte[] key;
    
    

    SymmetricAlgorithm desObj;

    protected void Page_Load(object sender, EventArgs e)
    {
        desObj = Rijndael.Create();//when the page is load to let the function can run.
    }
    protected void btnEncryption_Click(object sender, EventArgs e)
    {
        cipherData = txtST.Text;
        textByte1 = Encoding.ASCII.GetBytes(cipherData);
        key = Encoding.ASCII.GetBytes("0123456789abcdef");//fix the size from oringinal data? dk want to 16bit or not
        desObj.Key = key;

        desObj.Mode = CipherMode.CBC;//can either choose one
        desObj.Padding = PaddingMode.PKCS7;//typd of encryption
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(textByte1, 0, textByte1.Length);
        cs.Close();
        cipherByte = ms.ToArray();
        ms.Close();
        txtEnc.Text = Encoding.ASCII.GetString(cipherByte);
    }
    protected void btnDecryption_Click(object sender, EventArgs e)
    {

        System.IO.MemoryStream ms1 = new System.IO.MemoryStream(cipherByte);
        CryptoStream cs1 = new CryptoStream(ms1, desObj.CreateDecryptor(), CryptoStreamMode.Read);

        cs1.Read(cipherByte, 0, cipherByte.Length);
        textByte2 = ms1.ToArray();
        cs1.Close();
        ms1.Close();

        txtDec.Text = Encoding.ASCII.GetString(textByte2);

    }
}





错误:



Error:

Buffer cannot be null. Parameter name: buffer.





请帮我解决这个问题。



Please help me to solve this problem.

推荐答案

问题在于您似乎不了解网站应用程序中发生的情况。

它与桌面应用程序不同 - 当页面已发送到客户端时,服务器应用程序可以关闭,并在用户按下按钮时重新启动(或执行导致交互的其他操作)。此时,将再次调用Page_Load事件处理程序,然后再调用相应按钮的Click事件。



这意味着您无法在应用程序中保留值在两次按钮单击之间 - 当在加密按钮按下后页面完全加载到客户端时将丢弃类级别变量,并在为Decrytpion按钮单击重新创建页面时重新创建。



如果你需要保留值,你可能希望将它们保存在会话中 - 这是在页面加载之间保持的。

见这里:http://msdn.microsoft.com/en-us/library /system.web.httpcontext.session(v=vs.110).aspx [ ^ ]
The problem is that you don't seem to understand what is happening in an website application.
It's not the same as a desktop app - when the page has been sent to the client, the server application can shut down and will be re-started when the user presses a button (or does something else that causes an interaction). At that point, the Page_Load event handler will be called again, then the Click event for the appropriate button.

What that means is that you cannot preserve values in you application between two button clicks - as the class level variables will be discarded when the page is completely loaded to the client after the Encryption button press, and created anew when the page is re-created for the Decrytpion button click.

If you need to preserve values, they you probably want to save them in the Session - which is maintained between page loads.
See here: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.session(v=vs.110).aspx[^]


这篇关于我的解密显示错误:Buffer不能为null。参数名称:buffer。请帮我解决这个问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆