views:

47

answers:

3

Does a binary encrypted string could contain a 'space' or 'carriage return'? What's the best way to put a separator between two encrypted byte[] arrays?

+2  A: 

There's no such concept in binary data, generally. You could define a particular byte (or byte sequence) to be a separator, but then you'd have to work out escaping rules in case that data occurred naturally.

It's generally a better idea to include a length prefix - so if you had multiple values you might have:

length-1 data-1 length-2 data-2 terminator

(where terminator is a length of -1 or some equally invalid value).

Aside from anything else, this makes it much easier to read the data than having to scan it for separators. You can read just the data for the next chunk (or value, or whatever) without accidentally reading into the next value... so you can then just pass the stream on to whatever needs to read the next length/value pair.

Jon Skeet
is length a separated 4-byte integer value from the key?
lnk1347
@Ink1347: It's whatever you want to make it :) I'm just suggesting you design your protocol in this kind of way.
Jon Skeet
+1  A: 

you shouldnt need any way to separate the strings. since you know each string has to be 256 bits, just take the string and break it into blocks of 256 bits

calccrypto
A: 

Probably you problem is that you want use encrypted data somewhere and want to hold encrypted data as strings because you original data were string also. Then solution is very easy. You should only convert bytes into a string with respect of Convert.ToBase64String function (see http://msdn.microsoft.com/en-us/library/8f9a8s97.aspx). With respect of options parameter you can specify whether you want to insert a line breaks after every 76 characters in you output. Inserting of line break is a standard way. Probably Base64FormattingOptions.InsertLineBreaks it the option what you want.

With respect of Convert.FromBase64String function you can convert the data back to binary.

Oleg