简体   繁体   English

将字符串转换为字节数组

[英]Convert string into byte array

Using the ProtectedData class found here I created two methods that encrypt and decrypt. 使用此处找到的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;
    }
}

I'll then save the byte array to a local XML file. 然后,我将字节数组保存到本地XML文件中。 When I read my XML file using Xdocument it will return the byte array as a string like so: 当我使用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>

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? 如何将字符串(已经是字符串格式的字节数组)转换为字节数组,然后解密呢?

I would use a base64 encoded string to store it in the XML: 我将使用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;
        }
    }

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

If your binary is encrypted data, then base64 is recommended: 如果您的二进制文件是加密数据,则建议使用base64

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. 这样,您的数据将安全地存储,因此不会损坏,因为base64可以安全地存储在XML文件中。

Plus it is superior to using comma seperated numbers, because it's faster and doesn't consume so much space in your XML. 另外,它比使用逗号分隔数字更好,因为它速度更快,并且不会在XML中占用太多空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM