Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C#
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.
Posted
Updated 17-Nov-13 5:09am
v2

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900