473,414 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

C# ASP.NET Byte Array NullReferenceException

Hi, I think I am having a null reference with "encrypted". How do I solve this. The code works in windows form with encrypt and decrypt both working.

Expand|Select|Wrap|Line Numbers
  1.     byte[] encrypted;
  2.     protected void BnEncrypt_Click(object sender, EventArgs e)
  3.     {
  4.  
  5.         MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  6.         UTF8Encoding utf8 = new UTF8Encoding();
  7.         TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
  8.         tDES.Key = md5.ComputeHash(utf8.GetBytes(TextBox1.Text));
  9.         tDES.Mode = CipherMode.ECB;
  10.         tDES.Padding = PaddingMode.PKCS7;
  11.         ICryptoTransform trans = tDES.CreateEncryptor();
  12.         encrypted = trans.TransformFinalBlock(utf8.GetBytes(TextBox2.Text), 0, utf8.GetBytes(TextBox2.Text).Length);
  13.         TextBox3.Text = BitConverter.ToString(encrypted);
  14.  
  15.     }
  16.  
  17.     protected void BnDecrypt_Click(object sender, EventArgs e)
  18.     {
  19.  
  20.         MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  21.         UTF8Encoding utf8 = new UTF8Encoding();
  22.         TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
  23.         tDES.Key = md5.ComputeHash(utf8.GetBytes(TextBox4.Text));
  24.         tDES.Mode = CipherMode.ECB;
  25.         tDES.Padding = PaddingMode.PKCS7;
  26.         ICryptoTransform trans = tDES.CreateDecryptor();
  27.         TextBox5.Text = utf8.GetString(trans.TransformFinalBlock(encrypted, 0, encrypted.Length)); //This line throws the exception
  28.  
  29.     }
Jul 7 '14 #1
0 1049

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
3
by: simonc | last post by:
Can you define a property as type Byte array of a specific length? I am trying to pass a byte array which is 3200 bytes in length from one form (in which the bytes are read from a file) to...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.