Travis Boatman
Travis Boatman

Reputation: 2282

Convert string into byte array

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?

Upvotes: 0

Views: 890

Answers (3)

bytecode77
bytecode77

Reputation: 14860

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.

Upvotes: 0

yohannist
yohannist

Reputation: 4204

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);

Upvotes: 1

CodeTherapist
CodeTherapist

Reputation: 2806

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;
        }
    }

Upvotes: 3

Related Questions