0

I have a model which has a bytearray as a property,

public byte[] bytearraytest{ get; set; }

when I get this model via javascript GET request it is a string.

tgwBAQ==

How can i decode it now ?

3
  • You want to convert it to string with certain encoding or display it as byte array? Commented Nov 17, 2017 at 13:39
  • want to convert it to string because it is an ip address Commented Nov 17, 2017 at 13:42
  • It is base64 encoding. So search how to decode it. Commented Nov 17, 2017 at 16:09

2 Answers 2

0

The string in javascript is a Base64 string. You have to do a Base64 Decode in the javascript to access de bytes. Try this: https://www.w3schools.com/jsref/met_win_atob.asp

Sign up to request clarification or add additional context in comments.

1 Comment

nop that not it, it returns crypto stuff ¶
0

The string tgwBAQ== is Base64-encoded (easily discernible via the tell-tale == at the end). To get a byte array from that, you would use:

var bytes = Convert.FromBase64String(value);

If you're directly binding that to a byte[], it's most likely creating a char[], by splitting the string, i.e.:

[ 't', 'g', 'w', 'B', 'A', 'Q', '=', '=' ]

That's fairly obviously useless to you, so first and foremost, you need to accept the value as a string, so you can manipulate it as a string.

Once, you've got a real byte[] representation of the string, getting back to the IP address string, depends on what occurred previously to Base64-encoding it in the first place. For example, if the provided string was created by doing something like:

var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(ipAddress));

Then, you would simply reverse that:

var ipAddress = Encoding.UTF8.GetString(Convert.FromBase64String(base64));

However, if encryption was involved, you'd need to decrypt it first, using the same cryptographic algorithm it was encrypted in and the decrypt "key", either an actual shared key, a private key, etc.

Long and short, there's no enough info here to help you with that last piece of the puzzle, so you'll either need to update your question with more information regarding what you're doing or simply take this guidance and run with it on your own.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.