0

So it seems like the conversion is not the same

example

byte[] salt;  // this i hover over and see {byte[32]}  
string saltText = Convert.ToBase64String(salt);
// This saltText come out with the string

// example is that saltText = puYwD1RHO9mdrg4eakJIskcrN4wPlxzkBwjdwyJL+Eg=

This part is just fine, however I was in need of converting it back so I did this

byte[] saltArray = Encoding.ASCII.GetBytes(saltText);

// Hover over and the saltArray is saying {byte[44]}  

Am I not understanding Bytes ? Why is is changing from 32 to 44 ??

  • try this one http://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string – reza.cse08 May 24 '16 at 06:44
  • 1
    Because you first convert to base-64 string, and then treat that as ASCII encoded string. Why would you expect that to work at all? Use Convert.FromBase64String instead. – Evk May 24 '16 at 06:44
  • Thanks, not use to working with this stuff at all, and I simply found the other code on SO , but clearly must had been for some other use case. –  May 24 '16 at 07:15
  • Well, your string of text is 44 characters so that is exactly what you are getting with ASCII... OP is correct Convert.FromBase64String is what you want instead. – Tom Stickel May 24 '16 at 07:25

1 Answers1

0

try this for decoding

var base64EncodedBytes = System.Convert.FromBase64String(salt);
var stringData = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
reza.cse08
  • 5,938
  • 48
  • 39
  • BTW, I'm only using the 1st line var base64EncodedBytes , I only needed that... that is working fine. I don't need the second line of taking it back to a string, but something is not right with that , I get a lot of funny ascii characters... fyi –  May 24 '16 at 07:06
  • "�hr]��Y�C}�\0pC��A���\v!;c�g\vE" –  May 24 '16 at 07:06
  • please add your simple input & expected output? – reza.cse08 May 24 '16 at 07:10
  • string salt = "1mhyXbq7WdsXQ32IAHBDGtbzQQWKjKELIRw7Y6lnC0U="; var base64EncodedBytes = System.Convert.FromBase64String(salt); base64EncodedBytes.Dump(); // 32 items in byte array var stringData = System.Text.Encoding.UTF8.GetString(base64EncodedBytes); stringData.Dump(); // �hr]��Y�C}�pC��A��� !;c�g E –  May 24 '16 at 07:13
  • string saltText = Convert.ToBase64String(base64EncodedBytes); that seems to convert it back properly –  May 24 '16 at 07:13
  • What is your the source of byte[] salt=?. data & how you get that? I think there was a problem with your source. – reza.cse08 May 24 '16 at 07:36
  • public static byte[] GetSalt(int size = 32) { byte[] salt = new byte[size]; using (var cryptoServiceProvider = new RNGCryptoServiceProvider()) { cryptoServiceProvider.GetBytes(salt); } return salt; } –  May 24 '16 at 08:01
  • I think it provide you the correct character as your input string if the ASCII is less than 128. � is for Extended ASCII Codes, when your ASCII values is more than 127. see here http://www.asciitable.com/ – reza.cse08 May 24 '16 at 08:39