0

Using the ProtectedData class found here I created two methods that encrypt and decrypt.

public byte[] Encrpyt(string unprotectedData)
{
    try
    {
        var rawByte = Encoding.Default.GetBytes(unprotectedData);
        var protectedByte = ProtectedData.Protect(rawByte, mEntropy, DataProtectionScope.CurrentUser);

        var sb = new StringBuilder();
        foreach (var b in protectedByte)
        {
            sb.Append(b + ",");
        }
        return sb.ToString();
    }
    catch (CryptographicException e)
    {
        Log.Error("Unable to encrypt data: " + e.Message);
        return null;
    }
}

public string Decrpyt(byte[] encryptedByte)
{
    try
    {
        var rawByte = ProtectedData.Unprotect(encryptedByte, mEntropy, DataProtectionScope.CurrentUser);
        return Encoding.Default.GetString(rawByte);
    }
    catch (CryptographicException e)
    {
        Log.Error("Unable to decrypt data: " + e.Message);
        return null;
    }
}

I'll then save the byte array to a local XML file. When I read my XML file using Xdocument it will return the byte array as a string like so:

<Password>1,0,0,0,208,140,157,223,1,21,209,17,140,122,0,192,79,194,151,235,1,0,0,0,38,216,9,185,58,253,108,75,147,117,90,37,221,16,167,39,0,0,0,0,2,0,0,0,0,0,16,102,0,0,0,1,0,0,32,0,0,0,50,46,190,245,251,118,123,109,212,25,65,59,228,112,35,12,231,87,116,180,220,108,96,93,61,94,60,131,19,3,232,12,0,0,0,0,14,128,0,0,0,2,0,0,32,0,0,0,245,81,93,64,218,37,115,108,206,224,202,116,43,234,19,61,212,166,204,96,17,126,26,232,150,250,70,99,133,158,128,234,16,0,0,0,69,74,29,51,0,61,167,191,240,205,78,93,126,83,206,189,64,0,0,0,203,223,66,5,16,98,235,67,174,91,97,5,208,0,222,134,190,239,222,0,169,211,165,22,121,150,37,232,33,180,45,196,138,101,29,220,156,128,231,137,214,207,31,170,65,96,101,252,252,53,218,220,240,140,15,92,35,27,98,222,3,151,248,247,</Password>

How can I convert the string (which is already a byte array in string format) to a byte array so I can then decrypt it?

bytecode77
  • 14,163
  • 30
  • 110
  • 141
Travis Boatman
  • 2,252
  • 16
  • 31
  • What have you tried? Reading XML, splitting a string on comma and converting numeric strings to their byte values have all been solved... See for example [Convert comma separated string of ints to int array](http://stackoverflow.com/questions/1763613/convert-comma-separated-string-of-ints-to-int-array). – CodeCaster Sep 14 '15 at 08:33
  • 5
    That's a poor way of representing the binary data to start with, IMO. Very longwinded - why not just `Convert.ToBase64String` in `Encrypt`, and `Convert.FromBase64String` in `Decrypt`? (I'd strongly advise against using `Encoding.Default`, by the way - surely you want the content of the file to be portable across systems.) – Jon Skeet Sep 14 '15 at 08:38
  • @JonSkeet Thank you, I didn't think of using To/FromBase64String, the code has been refactored and now works. – Travis Boatman Sep 14 '15 at 08:52

3 Answers3

3

I would use a base64 encoded string to store it in the XML:

  public byte[] Encrpyt(string unprotectedData)
    {
        try
        {
            var rawByte = Encoding.UTF8.GetBytes(unprotectedData);
            var protectedByte = ProtectedData.Protect(rawByte, mEntropy, DataProtectionScope.CurrentUser);
            return System.Convert.ToBase64String(protectedByte);
        }
        catch (CryptographicException e)
        {
            Log.Error("Unable to encrypt data: " + e.Message);
            return null;
        }
    }

    public string Decrpyt(string encryptedBase64)
    {
        try
        {
            var bytes = System.Convert.FromBase64String(encryptedBase64)

            var rawByte = ProtectedData.Unprotect(bytes , mEntropy, DataProtectionScope.CurrentUser);
            return Encoding.UTF8.GetString(rawByte);
        }
        catch (CryptographicException e)
        {
            Log.Error("Unable to decrypt data: " + e.Message);
            return null;
        }
    }
CodeTherapist
  • 2,776
  • 14
  • 24
1

Try this

 var byteString = "1,0,0,0,208,140,157,223,1,21,209,17,140,122,0,192,79,194,151,235,1,0,0,0,38,216,9,185,58,253,108,75,147,117,90,37,221,16,167,39,0,0,0,0,2,0,0,0,0,0,16,102,0,0,0,1,0";

 var byteArray = byteString
   .Split(',')
   .Select(t => Byte.Parse(t))
   .ToArray();

 var decodedString = Decrpyt(byteArray);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
yohannist
  • 4,166
  • 3
  • 35
  • 58
0

If your binary is encrypted data, then base64 is recommended:

Convert.ToBase64String(yourByteArray)

And convert it back using

Convert.FromBase64String(yourBase64String)

This way, your data is stored safe, so it doesn't get corrupted, because base64 can be safely stored in an XML file.

Plus it is superior to using comma seperated numbers, because it's faster and doesn't consume so much space in your XML.

bytecode77
  • 14,163
  • 30
  • 110
  • 141