Base64String

G

Guest

I am entering the world of encryption in my windows form application and have
a question or two about formats. I start out with a string that I would
like to encrypt and store in my sql server database as an encrypted string.

Dim MyString as string = "Now is the time for all good men to come to the
aid of their country"

Now, encyrption algorythms require that his be converted to byte array. So,
Dim MyData() as byte =encoding.ascii.getbytes(MyString.ToCharArray)
?? Is there a better way to do this conversion CType, etc

Now, after encryption, I now have a encrypted byte array that I need to get
back into string format so I can store it in my database as an encrypted
string. Here is where I get confused with string/base64.

Can someone show me how to take a byte array and assign it to a string
variable in vb?

Thanks,
Fred Herring
 
C

Chris Dunaway

You can try:

Dim s As String = BitConverter.ToString(MyData, 0, MyData.Length)
I'm not sure if that will preserve the encoding.
 
M

Matt Berther

Hello Fred,

You could also use Convert.ToBase64String(byteArray) and Convert.From64String(string).
 
G

Guest

Hi Matt,

Would like to add on to your suggestion on using
Convert.ToBase64String(byteArray) and Convert.From64String(string). I have
used both methods on serialization. Unfortunately, the serialized object
string being truncated.

Any idea on this?

Feedback is much appreciated.
 
J

Jon Skeet [C# MVP]

Red Devil said:
Would like to add on to your suggestion on using
Convert.ToBase64String(byteArray) and Convert.From64String(string). I have
used both methods on serialization. Unfortunately, the serialized object
string being truncated.

Well, Convert.ToBase64String and Convert.FromBase64String are
themselves lossless, so either your original binary data is being
truncated, or the resulting string is being truncated wherever you're
storing it.
 
G

Guest

Jon,

It didn't happen all the time thou..is there a solution to this?

Help is much appreciated.
 
J

Jon Skeet [C# MVP]

Red Devil said:
It didn't happen all the time thou..is there a solution to this?

That entirely depends on what the problem is - and as I'm convinced
that the Base64 routines themselves are okay, we'll need to know much
more information - what exactly are you doing, and have you done any
debugging to check where the information is being lost?
 
G

Guest

Jon,

The function of this library is to serialize an object and then convert to
string before it gets send to server via socket. likewise, when server
receives the string, it will then be deserialize into a class. The problem
occurs when I did load test using test director. It didn't happen when I sent
from multiple windows application thou.

The string always get truncated at the same position.
 
J

Jon Skeet [C# MVP]

Red Devil said:
The function of this library is to serialize an object and then convert to
string before it gets send to server via socket. likewise, when server
receives the string, it will then be deserialize into a class. The problem
occurs when I did load test using test director. It didn't happen when I sent
from multiple windows application thou.

The string always get truncated at the same position.

So you're not even getting the string properly? That sounds like a
communications problem rather than anything to do with Base64 then. How
are you reading the string?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top