xml文档中十六进制值的问题 [英] problem with hexadecimal values in xml document

查看:65
本文介绍了xml文档中十六进制值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 C# 函数,它接受一个字符串编码并返回它:

I have simple C# function which takes one string encode it and return it:

    public static string EncodeString(string input)
    {
        byte[] bChiperText = null;

        RijndaelManaged rp = new RijndaelManaged();
        rp.Key = UTF8Encoding.UTF8.GetBytes("!Lb!&*W_4Xc54_0W");
        rp.IV = UTF8Encoding.UTF8.GetBytes("6&^Fi6s5SAKS_Ax6");

        ICryptoTransform re = rp.CreateEncryptor();

        byte[] bClearText = UTF8Encoding.UTF8.GetBytes(input);
        MemoryStream Mstm =  new MemoryStream();

        CryptoStream Cstm = new CryptoStream(Mstm, re, CryptoStreamMode.Write);

        Cstm.Write(bClearText, 0, bClearText.Length);
        Cstm.FlushFinalBlock();

        bChiperText = Mstm.ToArray();

        Cstm.Close();
        Mstm.Close();


        return System.Text.ASCIIEncoding.ASCII.GetString(bChiperText);
   } 

使用参数hello"调用此函数后,我得到如下 xml 文件:

After call this function with parameter "hello" i get xml file like this:

  <?xml version="1.0" encoding="utf-8"?>
  <users>
      <user name="user1" password="?V?Py????%??&#x13;?9?"/>
  </users>

一切都很好,但是当我在 Visual Studio 2010 中打开 xml 文件时,我收到如下警告:

Everithing fine but when i open the xml file in visual studio 2010 i receive warning like this:

错误 1 ​​字符 ' ',十六进制值 0x13 在 XML 文档中是非法的.

谁能告诉我我做错了什么?我可以忽略这些警告吗?
谢谢

Can anybody tell what i have done wrong?can i ignore those warnings?
Thanks

推荐答案

这是问题所在:

return System.Text.ASCIIEncoding.ASCII.GetString(bChiperText);

您只是通过将其视为 ASCII 来将任意二进制数据转换为文本.它不是.不要那样做.

You're converting arbitrary binary data to text just by treating it as if it were ASCII. It's not. Don't do that.

最安全的方法是使用 Base64:

The safest approach is to use Base64:

return Convert.ToBase64String(bChiperText);

当然,您的客户需要在恢复中执行相同的操作,例如通过使用 Convert.FromBase64String.

Of course, your client will need to do the same in revert, e.g. by using Convert.FromBase64String.

这篇关于xml文档中十六进制值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆