Store binary data string into byte array using c# - c#

I have a webservice that returns a binary data as a string. Using C# code how can I store it in byte array? Is this the right way?
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(inputString);
Actually, this didn't work. Let me explain it more:
the web service code converts a string (containing XSLFO data) into byte array using utf8 encoding. In my web service response I only see data something like "PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnM­6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c==". Actually I would like to have the original string value that was converted into byte[] in the service. Not sure if it possible?

No, that's a bad idea.
Unless the input data was originally text, trying to use Encoding is a bad idea. The web service should be using something like base64 to encode it - at which point you can use Convert.FromBase64String to get the original binary data back.
Basically, treating arbitrary binary data as if it were encoded text is a quick way to lose data. When you need to represent binary data in a string, you should use base64, hex or something similar.
This may mean you need to change the web service as well, of course - if it's creating the string by simply treating the binary data as UTF-8-encoded text, it's broken to start with.

If the string is encoded in UTF8 Encoding, then yes that is the correct way. If it is in Unicode it is very similar:
System.Text.Unicode encoding = new System.Text.Unicode();
byte[] bytes = encoding.GetBytes(inputString);
Base64Encoding is a little different:
byte[] bytes = Convert.FromBase64String(inputString);

Related

Writing a ByteArray to file AS bytes in C#

My goal is to convert a string to a ByteArray, write this ByteArray AS a ByteArray to a string so it's unreadable but still readable again upon "ByteArray to String" conversion in C#.
This is how my code is right now:
string json = "{\"database\":{\"tables\":{\"Users\":[\"column\":{\"id\":\"1\", \"name\":\"Test\"}]}}}";
var bytes = Encoding.ASCII.GetBytes(json);
File.WriteAllBytes("database.dat", bytes);
This works in theory, however the final output file has the same content of the string, and not the converted ByteArray. This is what the file contains:
database.dat
{"database":{"tables":{"Users":["column":{"id":"1", "name":"Test"}]}}}
But I expected something like
l4#ˆC}nC(YXX>AI0ve‚22úL«*“ÑÃYgPæaiäi
’Ê¢±·Ä¿|^Û×RÉ!×¹ÝYPZŠO•QÚÉèT“g‘Ѳ¬¡\g²Ô
What am I doing wrong? Is this not a ByteArray? Is there another way to convert data to an unreadable file, and then be able to convert it back into a string in my program?
What am I doing wrong? Is this not a ByteArray? Is there another way
to convert data to an unreadable file, and then be able to convert it
back into a string in my program?
Depends how much unreadable you want it to be? In the most extreme case you might need to use encryption.
In your case, you are storing ASCII representation of the string into a file, so of course a text editor can read it back to you.
One way could be try converting the byte array which you obtained to base64 encoded string - and store that string in file. That way it will not be easily readable, however, someone else can still decode it if he/she tries. So the security guarantees provided aren't that much. But again, depends on your needs.

Converting Byte[] to string to remain the original byte format

I have large amount of data which consists of tables,font,bold,size,etc. Those data will be stored as byte[] in Database.
when i retrieve those data i need to convert byte[] into string,because i need to some find & replace from this string,after i convert this string into byte[],am losing the original data structure which means, i can't able to see any tables,font,bold etc. properly. So how can i find and replace in byte[] by converting string and also to keep remain the data in original format.
The short answer is don't. Figure out the format of the data and see what you can do to do the manipulation. If the data is actually text, just stored as byte[], your approach would work, provided you encode the string correctly (ie. if your DB expects UTF-8, use UTF-8 encoding, if it's windows-1251, use that).
If you have a structure where a part of it is a string, what you're doing can't really work well. First, you probably want to modify just the relevant parts of the field. On MS SQL, you have handy functions for that. But even then, you should know what's actually stored there, not just assume that a string replace will magically work.
Now, a hack could be to use an explicit encoding that doesn't break the non-string data. That would be some single-byte encoding that doesn't do anything fancy. This is OK as long as you use the same encoding while reading the text data - however, if you use any variant of unicode, you're out of luck; due to features like string normalization, you can't really guarantee that what comes in comes out the same way, per-byte. It's generally a bad practice anyway.
Don't forget that it's quite possible the string you are looking for is actually somewhere outside of the text fields - even by pure chance, it can happen, and certain practices make that even more likely.
Again: figure out the data format inside that data field - then you can decide how to do what you want.
Try this
string result = System.Text.Encoding.UTF8.GetString(byteArray)
To make Byte[] to String
byte[] byteArray = new byte[10]; // put your byte array here
public void byteToString()
{
stringTemp = "";
stringTemp = BitConverter.ToString(byteArray).Replace("-", "");
}
And your data still in byteArray.. :)
If the byte Array contains binary data and is no string, try to convert it to base64:
Convert.ToBase64String(yourByteArray);

Convert file (or byte[]) into ascii, then convert that ascii back into the original file

I'm using an old technology called RTTY to send data (it's basically fancy Morse Code) over radio.
RTTY can only transmit ascii characters
What I want to do is convert a file such as a small jpg or something similar into a block of ascii text, send the characters over radio, then convert the characters on the remote end back into the original file.
Some help getting started would be great.
I know I need to use StreamReader but then how can I convert the byte[] into an encoded ascii string that I can then 'decode'.
I know i need to use streamreader but then how can I convert the byte[] into an encoded ascii string that I can then 'decode'
Basically, you want to use a Base64 conversion. It will inflate the size of the data, but it guarantees that you'll be able to round-trip the original binary data.
Use Convert.ToBase64String to convert a byte[] into a string, and Convert.FromBase64String to do the reverse.

How to convert between string and byte[] without losing integrity

I know how to convert from a string to byte[] in C#. In this particular case, I'm working with the string representation of an HMAC-SHA256 key. Let's say the string representation of this key I get from an OpenID OP is:
"81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8="
I convert it to byte[] like this:
byte[] myByteArr = Encoding.UTF8.GetBytes("81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=");
The problem I have with that is that it seems to be losing the original data. If I take the byte array from the previous step and convert it back to a string, it's different from the original string.
string check = Convert.ToBase64String(myByteArr);
check ends up being:
"ODFGTnliS1dmY001Mzl2Vkd0SnJYUm1vVk14Tm1aSFkzT2dVcm84K3BaOD0="
which is obviously not the same as the original string representation I started with.
With crypto keys, always use Convert.FromBase64String and Convert.ToBase64String. That way you'll be doing it the standard way and will not lose bytes due to encoding problems. Base 64 string may not be space efficient but it is the preferred method for storage and transport of keys in many schemes.
Here is a quick verification:
byte[] myByteArr = Convert.FromBase64String("81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=");
string check = Convert.ToBase64String(myByteArr);
Console.WriteLine(check);
// Writes: 81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=
The first function (Encoding.UTF8.GetBytes) takes a string (of any kind) and returns a byte[] that represents that string in a particular encoding -- in your case, UTF8.
The second function (Convert.ToBase64String) takes a byte array (of any kind) and returns a string in base64 format so that you can store this binary data in any ASCII-compatible field using only printable characters.
These functions are not counterparts. It looks like the string you're getting is a base64-encoded string. If this is the case, use Convert.FromBase64String to get the byte[] that it represents, not Encoding.UTF8.GetBytes.
The bytes you get when using byte[] Encoding.GetBytes(string) and decoding a base64 string are not the same things. The former gives you the bytes that represent the string. You however want to decode a base64 string back to the bytes it represents. In that case you want to use Convert.FromBase64String().
string encoded = "81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=";
byte[] decoded = Convert.FromBase64String(encoded); // this gives the bytes that the encoded string represents
The encoding classes have a GetString method, to convert it from a byte array back to a string.
If you used the UTF8 encoding to create the byte array, you should use the same coding to get it back again.
var original = "81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=";
var byteArray = Encoding.UTF8.GetBytes(original);
var copy = Encoding.UTF8.GetString(byteArray);
bool match = (copy == original); // This returns true

Issue while encoding a string

I'm developing an application where at some point i need an encoded stream of bytes based on the given user output.
Something like
Encoding sysEncode = System.Text.Encoding.GetEncoding(850);
byte[] dataToEncrypt = sysEncode.GetBytes(m_oStrActivation);
However when i extract the string from the byte stream i get the encrypted string as
W?????e?????W?X????;??2????W???????#
Is there any way(type of Encoding/equivalent) i can restrict these question marks and allow only plain scrambled alphanumeric characters ?
From m_oStrActivation and you mentioning "encryption" I assume that you're writing some kind of activation/licensing code. If this is the case, you're doing it wrong. A correct way to do this is to use a hash function over your activation data.
You can then transform this string into Base64 string using Convert.ToBase64String() method.

Categories