将int从c#gethashcode()转换回字符串? [英] Convert the int from c# gethashcode() back to string?

查看:286
本文介绍了将int从c#gethashcode()转换回字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题:

我正在做一件简单的事情.我只有几个string:

I am doing a simple thing. I have few string like:

 string A = "usd";

我想在C#中获取哈希码:

And I want to get the hashcode in C#:

 int audusdReqId = Convert.ToInt32("usd".GetHashCode());

现在如何将32位整数audusdReqId转换回string"usd"?

Now how could I convert the 32-bit integer audusdReqId back to the string "usd"?

推荐答案

您不能.哈希码不包含将其转换回字符串的所有必要信息.更重要的是,您应该小心使用GetHashCode的用途. 这篇文章解释您应该使用它的用途.您甚至无法保证GetHashCode将在不同的环境中返回相同的内容.因此,请勿出于任何加密目的而使用GetHashCode.

You cannot. A hash code does not contain all of the necessary information to convert it back to a string. More importantly, you should be careful what you use GetHashCode for. This article explains what you should use it for. You can't even guarantee that GetHashCode will return the same thing in different environments. So you should not be using GetHashCode for any cryptographic purposes.

如果您尝试创建字符串的二进制表示形式,则可以使用

If you are trying to create a binary representation of a string, you can use

var bytes = System.Text.Encoding.UTF8.GetBytes(someString);

这将为您提供该特定字符串的byte[].您可以在编码中使用GetString将其转换回字符串:

This will give you a byte[] for that particular string. You can use GetString on Encoding to convert it back to a string:

var originalString = System.Text.Encoding.UTF8.GetString(bytes);

如果您试图对字符串进行密码保护,则可以使用AES之类的对称加密算法,如 Robert Harvey 在评论中指出.

If you are trying to cryptographically secure the string, you can use a symmetric encryption algorithm like AES as Robert Harvey pointed out in the comments.

这篇关于将int从c#gethashcode()转换回字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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