0

i am trying to display the byte[] in the text box, first i am reading all the encrypted info from the file temp.enc to the byte array

FileStream f = File.OpenRead("temp.enc");
byte[] b = new byte[f.Length];//length is 32 bytes
f.Read(b, 0, Convert.ToInt32(f.Length));
f.Close();

second i try to display the content on to the text box, when i run the program i dont see the complete data. it displays only the first 23 bytes of data

outputFileTextBox.Text = System.Text.Encoding.Default.GetString(b);

how ever if i use the same byte array again to write the info back to file, i have all the 32bytes written on to file

BinaryWriter swEnc = new BinaryWriter(File.OpenWrite("encypt.txt"));
swEnc.Write(b);
swEnc.Close();

this is a c# windows application, i m not sure what i am doing wrong.

user2805439
  • 45
  • 1
  • 6
  • Do you want to display decimal/hexadecimal/binary/etc representation of `Int32` values or values as `string`? `System.Text.Encoding.Default.GetString(b)` doesn't makes any sense, because you are not having `string` (yet?). – Sinatr Jun 10 '15 at 14:25
  • byte[] b has a encrypted data from AES256 scheme. temp.enc data i}ÊÀ§DÕI”²E5¯cÙ¸,­ñÛŒòÅÿIöJ'j, when i read it into byte[] it automatically converted into dec. i want to display as it is in the temp.enc – user2805439 Jun 10 '15 at 14:35
  • If you have *arbitrary* bytes and you want to produce something printable, you need to e.g. convert using Base 64 or bin to hex. You should only use `Encoding.GetBytes` when those bytes were produced *from* an encoding. – Damien_The_Unbeliever Jun 10 '15 at 14:41
  • Encrypted data (or generally *byte array*) is not necessarily something you can convert to `string`. You see 23 bytes by coincidence, because this is what default encoder can display as a `string`. With another encoded data you will see different number of characters. What you want is to convert array of bytes to something what is `string`, e.g. [hexadecimal values](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa). – Sinatr Jun 10 '15 at 14:41
  • got it. i was trying to display as it is so that user can copy the enc data directly. – user2805439 Jun 10 '15 at 14:56

1 Answers1

0

Be sure you Read and Write in the same format. It looks like you are reading in as TEXT using whatever the Default encoding is, but you write it back in Binary format.

Here is a sample code that writes back in the same format as it reads in:

    private string ReadFile(string filename)
    {
        var sb = new StringBuilder();
        if (System.IO.File.Exists(filename))
        {
            using (var f = System.IO.File.OpenRead(filename))
            {
                var b = new byte[f.Length];
                var len = f.Read(b, 0, b.Length);
                while ((-1 < len) && (len == b.Length))
                {
                    sb.Append(System.Text.Encoding.UTF8.GetString(b, 0, len));
                    len = f.Read(b, 0, b.Length);
                }
            }
        }
        return sb.ToString();
    }

    private void WriteFile(string filename, string data)
    {
        using (var f = System.IO.File.OpenWrite(filename))
        {
            var b = System.Text.Encoding.UTF8.GetBytes(data);
            f.Write(b, 0, b.Length);
        }
    }

Also, some of the TEXT characters could be non-printing values (Carriage Returns, Start of Field, End of Field, etc.).

Consider all of the non-displayable values below the Decimal 32 value in the following ASCII chart:

ASCII Chart