C#:字符串 -> MD5-> 十六进制

发布于 2024-07-26 01:17:04 字数 416 浏览 2 评论 0原文

在 PHP 或 Python 等语言中,有一些方便的函数可以将输入字符串转换为输出字符串,即其十六进制表示形式。

我发现它是一个非常常见且有用的任务(密码存储和检查、文件内容的校验和..),但据我所知,在 .NET 中,您只能处理字节流。

完成这项工作的函数很容易实现(例如 http://blog.stevex.net/index.php/c-code-snippet-creating-an-md5-hash-string/),但我想知道是否我遗漏了一些东西,使用了错误的模式,或者.NET 中根本没有这样的东西。

谢谢

in languages like PHP or Python there are convenient functions to turn an input string into an output string that is the HEXed representation of it.

I find it a very common and useful task (password storing and checking, checksum of file content..), but in .NET, as far as I know, you can only work on byte streams.

A function to do the work is easy to put on (eg http://blog.stevex.net/index.php/c-code-snippet-creating-an-md5-hash-string/), but I'd like to know if I'm missing something, using the wrong pattern or there is simply no such thing in .NET.

Thanks

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

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

发布评论

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

评论(3

暮年慕年 2024-08-02 01:17:04

您链接到的方法似乎是正确的,MSDN C# FAQ

评论建议您可以使用:

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string, "MD5");

The method you linked to seems right, a slightly different method is showed on the MSDN C# FAQ

A comment suggests you can use:

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string, "MD5");
无声无音无过去 2024-08-02 01:17:04

是的,你只能使用字节(据我所知)。 但是,您可以通过循环遍历这些字节并执行以下操作,轻松地将这些字节转换为十六进制表示形式:

myByte.ToString("x2");

并且您可以使用以下命令获取组成字符串的字节:

System.Text.Encoding.UTF8.GetBytes(myString);

因此可以在几行内完成。

Yes you can only work with bytes (as far as I know). But you can turn those bytes easily into their hex representation by looping through them and doing something like:

myByte.ToString("x2");

And you can get the bytes that make up the string using:

System.Text.Encoding.UTF8.GetBytes(myString);

So it could be done in a couple lines.

抚笙 2024-08-02 01:17:04

一个问题在于“[字符串]的十六进制表示”这一概念。

字符串是字符的序列。 这些字符如何表示为单独的位取决于编码。 .NET 的“本机”编码是 UTF-16,但通常使用 UTF-8 可以实现更紧凑的表示(同时保留对任何字符串进行编码的能力)。

您可以使用 Encoding.GetBytes 一旦您选择了适当的编码,就可以获取字符串的编码版本 - 但事实上,要做出这样的选择是没有太多 API 可以使用的原因直接从字符串到 base64/hex 或直接对字符串执行加密/散列。 任何存在的此类 API 几乎肯定会执行“编码为字节数组、执行适当的二进制操作、将不透明二进制数据解码为十六进制/base64”。

(这让我想知道是否不值得编写一个可以接受编码、Func和输出格式(例如 hex/base64)的实用程序类- 可以表示应用于字符串的任意二元运算。)

One problem is with the very concept of "the HEXed representation of [a string]".

A string is a sequence of characters. How those characters are represented as individual bits depends on the encoding. The "native" encoding to .NET is UTF-16, but usually a more compact representation is achieved (while preserving the ability to encode any string) using UTF-8.

You can use Encoding.GetBytes to get the encoded version of a string once you've chosen an appropriate encoding - but the fact that there is that choice to make is the reason that there aren't many APIs which go straight from string to base64/hex or which perform encryption/hashing directly on strings. Any such APIs which do exist will almost certainly be doing the "encode to a byte array, perform appropriate binary operation, decode opaque binary data to hex/base64".

(That makes me wonder whether it wouldn't be worth writing a utility class which could take an encoding, a Func<byte[], byte[]> and an output format such as hex/base64 - that could represent an arbitrary binary operation applied to a string.)

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