将字符串转换为字节数组

时间:2015-09-14 08:32:46

标签: c#

使用找到的here ProtectedData类,我创建了两种加密和解密的方法。

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

然后我将字节数组保存到本地XML文件中。当我使用Xdocument读取我的XML文件时,它会将字节数组作为字符串返回,如下所示:

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

如何将字符串(已经是字符串格式的字节数组)转换为字节数组,以便我可以解密它?

2 个答案:

答案 0 :(得分:3)

我会使用base64编码的字符串将其存储在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;
        }
    }

答案 1 :(得分:0)

如果您的二进制文件是加密数据,则建议 base64

Convert.ToBase64String(yourByteArray)

使用

将其转换回来
Convert.FromBase64String(yourBase64String)

这样,您的数据就可以安全存储,因此不会被破坏,因为base64可以安全地存储在XML文件中。

此外,它优于使用逗号分隔数字,因为它更快,并且不会在XML中占用太多空间。