简体   繁体   English

用于将字节数组转换为字符串的编码,反之亦然

[英]Encoding to use to convert Bytes array to String and vice-versa

I use this code to encrypt a string (basically, this is the example given on the Rijndael class on MSDN ):我使用此代码来加密字符串(基本上,这是MSDNRijndael 类中给出的示例):

public static String AESEncrypt(String str2Encrypt, Byte[] encryptionKey, Byte[] IV)
{
    Byte[] encryptedText;

    using (RijndaelManaged rijAlg = new RijndaelManaged())
    {
        // Use the provided key and IV
        rijAlg.Key = encryptionKey;
        rijAlg.IV = IV;

        // Create a decrytor to perform the stream transform
        ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

        // Create the streams used for encryption
        using (MemoryStream msEncrypt = new MemoryStream())
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {
                // Write all data to the stream
                swEncrypt.Write(str2Encrypt);
            }

            encryptedText = msEncrypt.ToArray();
        }
    }

    return Encoding.Default.GetString(encryptedText);
}

I use Encoding.Default to convert a byte array to a string but I'm not sure it's a good solution.我使用Encoding.Default将字节数组转换为字符串,但我不确定这是一个好的解决方案。 My goal is to store encrypted text (such as passwords...) in files.我的目标是将加密文本(例如密码...)存储在文件中。 Should I continue with Encoding.Default or use Encoding.UTF8Encoding or something else?我应该继续使用Encoding.Default还是使用Encoding.UTF8Encoding或其他东西?

Can that have negative consequences on the stored values when I try to encrypt and decrypt them if the files are moved onto different OS'?如果文件被移动到不同的操作系统上,当我尝试加密和解密它们时,这会对存储的值产生负面影响吗?

You should absolutely not use an Encoding to convert arbitrary binary data to text.绝对不应该使用Encoding将任意二进制数据转换为文本。 Encoding is for when you've got binary data which genuinely is encoded text - this isn't. Encoding用于当您获得真正编码文本的二进制数据时 - 这不是。

Instead, use Convert.ToBase64String to encode the binary data as text, then decode using Convert.FromBase64String .相反,使用Convert.ToBase64String将二进制数据编码为文本,然后使用Convert.FromBase64String进行解码。

If your goal is to store encrypted information in files, and you have a byte array, you should store it directly - don't convert it to a string at all.如果您的目标是在文件中存储加密信息,并且您有一个字节数组,则应该直接存储它 -根本不要将其转换为字符串。 For example, in C# you can use File.WriteAllBytes to write a byte array directly to a file.例如,在 C# 中,您可以使用File.WriteAllBytes将字节数组直接写入文件。 Remember that files hold bytes, not strings (if you write a string into a file, some kind of Encoding will be used to convert it to bytes, even if you are using a technique that doesn't explicitly refer to Encoding ).请记住,文件保存字节,而不是字符串(如果您将字符串写入文件,则将使用某种Encoding将其转换为字节,即使您使用的技术未明确引用Encoding )。

If you actually need the byte array to be stored as a string (for example, because it will be stored in JSON) then the most common technique is to use base64 with Convert.ToBase64String , as Jon mentioned.如果您确实需要将字节数组存储为字符串(例如,因为它将存储在 JSON 中),那么最常用的技术是将 base64 与Convert.ToBase64String一起使用,正如 Jon 所提到的。

(In non-encryption applications you might have a byte[] that contains a mixture of ASCII and binary data and you want to convert it to a string. In this case base64 works fine but it hides the text - Byte Array In String format is like base64 except that runs of ASCII are preserved - C# version in Loyc.Essentials NuGet package, Java version here ) (在非加密应用程序中,您可能有一个包含 ASCII 和二进制数据混合的byte[] ,并且您希望将其转换为字符串。在这种情况下,base64 工作正常,但它隐藏了文本 - 字符串格式的字节数组是与 base64 类似,但保留 ASCII 运行 - Loyc.Essentials NuGet 包中的C# 版本此处为Java 版本)

Yes, Encoding.Default is your machine's current ANSI code page and may well be different between multiple machines/locales.是的, Encoding.Default是您机器的当前 ANSI 代码页,并且在多台机器/区域设置之间可能会有所不同。 If the data was textual data and to make it portable, you would need to use a fixed encoding format, for example UTF8.如果数据是文本数据并使其可移植,则需要使用固定编码格式,例如 UTF8。

However, as your data is NOT textual data, you can not try and use any textual encoding to convert to a string.但是,由于您的数据不是文本数据,因此您不能尝试使用任何文本编码来转换为字符串。 The best option is to use a hex or Base64 encoding or keep it as a blob/byte array.最好的选择是使用十六进制或 Base64 编码或将其保留为 blob/字节数组。

im not 100% sure here but i think that Encoding.Default refers to the system default encoding and yes that can change from system to system..我在这里不是 100% 确定,但我认为 Encoding.Default 指的是系统默认编码,是的,可以从系统更改为系统..

If i where you i would use UTF8 or some other encoding that does not change from system to system..如果我在你那里我会使用 UTF8 或其他一些不会因系统而改变的编码..

for more info about extended ascii look here有关扩展的 ascii 的更多信息,请看这里

http://en.wikipedia.org/wiki/Extended_ASCII http://en.wikipedia.org/wiki/Extended_ASCII

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

相关问题 C#将PublicKey转换为字符串,反之亦然 - C# Convert a PublicKey to string and vice-versa 在C#中将字符串数组转换为简单字符串,反之亦然 - converting an array of strings into a simple string and vice-versa in c# C#字节数组为字符串,反之亦然 - C# byte array to string, and vice-versa 如何将数据库大小从兆字节转换为字节,反之亦然? - How do I convert the database size from mega bytes to bytes or vice-versa? 将字典列表转换为字符串,反之亦然 - converting list of dictionaries to string and vice-versa 在C#中将数字数组通用转换为字节,反之亦然 - Generic conversion of number arrays into bytes and vice-versa in C# 将位图图像转换为字符串格式以通过网络(LAN)发送,反之亦然 - Convert Bitmap image to String format to send over network(LAN) and vice-versa 是否可以将整数(short,int,long)转换为以64为基数的字符串(反之亦然)? - Is it possible to convert an integer ( short, int, long ) into a base 64 string ( and vice-versa )? 如何将日期转换为毫秒(UTC),反之亦然? - How to convert date to ms (UTC) and vice-versa? 在C#中将十进制数组转换为字节数组,反之亦然 - Converting decimal array to byte array and vice-versa in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM