Hi all,
In my project, at one point, I get the result in byte[], which I change to string to display in further textboxes. I can also store the same in the database too.
My question is, is it possible to work directly on the returned byte[] array.
My code is as follows:
byte[] cip = elgamaldll.Class1.Encrypt(TextBox1.Text.ToString());
string cipher = System.Text.ASCIIEncoding.ASCII.GetString(cip);
TextBox2.Text = cipher;
The above works good and I can display as well as store the value in the database.
But, when I do the following:
TextBox12.Text = cip;
or
TextBox12.Text = cip.ToString();
the only thing displayed/stored is "System.Byte[]"
Is it not possible to view the values directly?
But, other functions work fine when I give the byte[] value as an input to them
The reason I want the above functionality is because my Encrypt function returns the output in byte[], and the Decrypt function needs the same byte[] as an input.
Now, I can convert the byte[] to String and save it on the database and during decryption, I can convert it back to byte[] and feed it to the Decrypt() function, but this was giving me some errors. It seems the code I used for converting string to byte[] was incorrect...
Is it possible to work directly on the byte[] values..
Many thanks,
Abhi

6 answers
It looks like bytes above 7F are still getting replaced with 3F which I don't understand at all if you've changed everything to unicode.
However, if you change the database field type to binary with a fixed length of 128 bytes, then this should enable you to save the byte[] array to the database and retrieve it in the same form without any possibility of corruption.
Here's some suggested code:
answered 7 months ago by:
11603
201
Hi Vulpes, Thanks. I am trying this now. Also, what if the cipher generated exceeds the length of 128.. should I for safe side assign more size.. Also, should I use column datatype as binary or nchar? Many thanks, Abhi
11603
If you're using the same algorithm, then the length is likely to be always 128. However, if there's any doubt about it, I'd use varbinary rather than binary and set the maximum length to some appropriate value such as 512.
It's certainly possible to view the byte values directly. The following code will display them in hex:
As you're using the ASCII encoding, there should be as many bytes displayed as there are characters in the string.
It would also be possible to save the byte array directly to the database using a varbinary or (if it's always the same size) binary field. However, in practice, most people would save it as an ASCII string and I'm surprised you're having any problems with this when there's a one to one correspondence between the bytes and the ASCII characters.
answered 7 months ago by:
11603
Hi Vulpes,
Thanks for the answer vulpes. I will try it and get back to you.
Actually, I have two functions Encrypt and Decrypt. Encrypt takes a string input and returns a byte[], whereas, the decrypt takes this byte[] as input and returns a string.
If I do this way, it works.. i.e. Encrypt's out going straight as Decrypt's input. But since these functions can be called anytime by the user, I need to store the indermediatery ciphertext, which is in bytes, to a database.
I thought the conversion to string and then back to byte[] would be the best, but don't know why the final output does not match the input. There should be something wrong in the way I am converting.
The code for byte[] to String I am using is
Now, when the time comes for decrypt to call, I retrieve the value of ccname from the database
The conversion function is the following:
Is there something wrong with the way I am doing the conversion?
Many thanks,
Abhi
answered 7 months ago by:
201
11603
The conversions look OK to me though I'd insert a space just before "values('" when inserting ccname into the database. Subject to that all I can think of is that you're using a field type for dbname which doesn't 'round-trip' properly for some reason. You should be able to get an idea of what's going wrong by comparing the byte expansion (using the code I gave) before inserting to the database and then after you retrieve it.
Hi Vulpes,
I ran the code which you gave before inserting the byte into the database and then again on the byte which I received after conversion from string.
surprisingly, both the values are slightly different.
The original byte value looks like this
{ 41 FC 04 6E 84 FF CF 51 D4 FD 73 0B D5 B6 87 84 68 55 B4 2D 3F BA 69 8C 2A B2 47 92 B1 70 94 41 FA BD D3 23 32 76 E0 A4 C7 1E C3 2C 7D E7 A8 8C 63 78 0C 6B 59 CD 80 B6 EC CC 0E 28 1F E4 C2 ED 5B 06 3E 19 A1 04 B7 26 17 3A BD C4 1B 07 3F 3D 99 D2 D1 69 A7 26 62 76 1C D5 1C 06 8F 3A 9B A8 03 DB 7B 7C 11 B3 3A DC 8D 28 D2 D3 82 A0 C8 44 52 42 81 47 DD 32 C9 15 D1 A8 7A 56 1B AB 1E 63 }and the value of byte after the conversion is
{ 41 3F 04 6E 3F 3F 3F 51 3F 3F 73 0B 3F 3F 3F 3F 68 55 3F 2D 3F 3F 69 3F 2A 3F 47 3F 3F 70 3F 41 3F 3F 3F 23 32 76 3F 3F 3F 1E 3F 2C 7D 3F 3F 3F 63 78 0C 6B 59 3F 3F 3F 3F 3F 0E 28 1F 3F 3F 3F 5B 06 3E 19 3F 04 3F 26 17 3A 3F 3F 1B 07 3F 3D 3F 3F 3F 69 3F 26 62 76 1C 3F 1C 06 3F 3A 3F 3F 03 3F 7B 7C 11 3F 3A 3F 3F 28 3F 3F 3F 3F 3F 44 52 42 3F 47 3F 32 3F 15 3F 3F 7A 56 1B 3F 1E 63 }There are some minor differences in both the values. So, this is either being caused by the insert/retrieve in the database or the conversion function I am using is not correct. How can I further spot the point where the error is being caused?
Also, if I go ahead with storing the byte value itself in the database, then once I retrieve, can I just feed it as bytes or do I need to use a reverse of your code, i.e. from it's hex representation to the byte[] array..
Many thanks,
Abhi
answered 7 months ago by:
201
201
Hi Vulpes, It seems the issue is at the point when I am inserting the string to database and retrieving it back. I compared the two strings and both are different too, and that explains the difference in their respective byte[] values.. Actually, this being a cipher, there are several white spaces and other special characters.. do you think that might be a reason the interpretation is getting chaged.. Many thanks, Abhi
201
I think, using the byte[] values itself in the entire process might solve the problem.. but I was not able to convert the hex representation back to byte[].. Many thanks, Abhi
11603
Now that we have the byte expansions, I can see that the problem is that there are bytes in the ciphertext which are outside the standard ASCII range of 00 to 7F. These bytes are being replaced by the ASCII encoding by 3F (the question mark character) which is messing up the decryption. What I'd try is to change the field in the database to be of a unicode type such as nchar with a length of 64 (i.e. 2 bytes per character = 128 bytes total). Now in your conversions use the Unicode rather than the ASCII encoding both on storage and retrieval.
Hi Vulpes,
I tried changing it to unicode at both the sides.. storing as well as retrieving.
Now, I sometimes I get errors in the decryption functions itself... it is exceeding the maximum length it expects from the ciphertext..
When the code worked with unicode, I again compared the bytes usign ur code and they were still different
Here are the bytes values
{ 46 5F ED 15 73 FF F7 D3 BE 8E 3B 5B 3B F8 5E C8 49 17 D1 E0 78 7F 37 8F B5 59 E7 C8 AB 8B 5F B7 E1 70 37 ED DC 32 9B 54 61 79 DF D5 E3 47 95 79 FD 04 04 04 93 F8 84 2A 2C CD F1 95 55 2F 86 36 69 38 CD 6F 02 8A F7 6A 40 D3 AB A7 AA FB FE 2B 7D 25 0F E8 63 66 C7 39 D6 72 30 75 76 E5 EA 7C 23 66 F5 35 02 FC FA EA 5D 67 16 1E 73 AC EC 86 07 56 00 0A AC 12 44 AC 4A 87 CD AE 2B 9F A8 B1 }{ 46 5F 3F 15 73 3F 3F 3F 3F 3B 5B 3B 3F 5E 3F 49 17 3F 3F 78 7F 37 3F 3F 59 3F 3F 3F 5F 3F 3F 70 37 3F 3F 32 3F 54 61 79 3F 3F 3F 47 3F 79 3F 04 04 04 3F 3F 3F 2A 2C 3F 3F 55 2F 3F 36 69 38 3F 6F 02 3F 3F 6A 40 3F 3F 3F 3F 3F 2B 7D 25 0F 3F 63 66 3F 39 3F 72 30 75 76 3F 3F 7C 23 66 3F 35 02 3F 3F 3F 5D 67 16 1E 73 3F 3F 07 56 00 0A 3F 12 44 3F 4A 3F 3F 2B 3F 3F 3F 20 20 20 20 20 20 }In case I store the byte[] value's hex( using the code you provided) representation in the database, will it be possible to retrieve it and convert it back to the byte[] array? I am not sure if that would be a good ting to do, but right now, it seems all the encoding are messing the strings due to the numerous special characters present and until n unless the decryption function gets the exact byte[] the encrypt function generated, it will always give incorrect output. :(
Many thanks,
Abhi
answered 7 months ago by:
201
Hi Vulpes,
Thanks a million for all your help and suggestions. The decrypted output now matched the input. All the communication is in byte[] now.
I have one more issue which I am trying to solve, but since it is slightly on a different track, I will post it as a new thread.
Once again, thanks for your help. :)
Regards,
Abhi
answered 7 months ago by:
201