Unicode 解码方式与编码文本不同

发布于 2024-11-03 12:50:25 字数 511 浏览 0 评论 0原文

我的数据正在被加密并发送到另一个页面,在那里它被解码和解释。 使用 System.Text.Encoding.Unicode.GetString() 将数据哈希为 unicode,然后加密,然后编码为 Base64 字符串并通过 URL 发送,然后接收站点将其从 Base64 中取出并再次解密,然后使用 System.Text.Encoding.Unicode.GetBytes() 再次返回字节。

但是,我得到的单个字符没有像我发送它一样被解释为字节。它“看起来”是一样的,就像一个“�”。然后该字符串被解密为字节,但计算结果不同。

编码时,unicode 字符占用两个字节 - 该字符被编码为这些字节:

[0]: 139
[1]: 222 

但解码为

[0]: 253
[1]: 255

所有其他字符都正确地转换为加密和加密,其字节转换保持不变。

这个角色是特例吗?还有其他角色可以达到同样的效果吗?加密有什么问题吗?

I have data that is being encrypted and sent to another page, where it is decoded and interpreted.
The data is hashed into unicode using System.Text.Encoding.Unicode.GetString(), then is encrypted, then encoded into a Base64 string and sent through the URL, then the receiving site takes that out of Base64 and decrypts it again, and uses System.Text.Encoding.Unicode.GetBytes() to get back to bytes again.

However, I'm getting a single character that is not interpreted into bytes the same way as I am sending it. It "appears" the same, as a '�'. This string is then decrypted into bytes, but evaluates differently.

When encoding, the unicode characters take two bytes- this character is encoded as these bytes:

[0]: 139
[1]: 222 

but is decoded as

[0]: 253
[1]: 255

All the other characters are converted properly into and out of the encryption with their byte conversions remaining the same.

Is this character a special case? Are there other characters that would have this same effect? Is something broken with the encryption?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-11-10 12:50:25

在这种情况下,您根本不应该使用文本编码。将原始数据直接加密为字节数组,然后将其编码为base64字符串,然后将其解码为字节数组,并解密原始字节。唯一的字符串是 base64 字符串。不要在加密/解密阶段使用中间字符串。

You should not use a Text encoding at all in this situation. Encrypt the original data directly to a byte array, then encode that to a base64 string, then decode that to a byte array, and decrypt the original bytes. The only String is the base64 string. DO NOT use an intermediate String during the enrypting/decrypting stages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文