如何模糊一个整数? [英] How to obfuscate an integer?

查看:191
本文介绍了如何模糊一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C#中的整数列表中,我需要生成唯一值的列表。我想在MD5或类似的,但它们产生太多字节。

From a list of integers in C#, I need to generate a list of unique values. I thought in MD5 or similar but they generates too many bytes.

整数大小为2个字节。

I想要获得单向通信,例如

I want to get a one way correspondence, for example

0 -> ARY812Q3
1 -> S6321Q66
2 -> 13TZ79K2

所以,证明哈希,用户不能知道整数或干扰列表后面的序列的哈希值。

So, proving the hash, the user cannot know the integer or to interfere a sequence behind a list of hashes.

现在,我尝试使用MD5(我的号码),然后我使用前8个字符。不过我在51389发现了第一次碰撞。我可以使用哪些其他选择?

For now, I tried to use MD5(my number) and then I used the first 8 characters. However I found the first collision at 51389. Which other alternatives I could use?

正如我所说,我只需要一种方法。没有必要能够从哈希计算整数。系统使用字典查找它们。

As I say, I only need one way. It is not necessary to be able to calculate the integer from the hash. The system uses a dictionary to find them.

更新:

回应有关使用GetHashCode()的一些建议。 GetHashCode返回相同的整数。我的目的是为最终用户隐藏整数。在这种情况下,整数是数据库的主键。我不想将这些信息提供给用户,因为他们可以推断数据库中的记录数量或记录的增量。

Replying some suggestions about using GetHashCode(). GetHashCode returns the same integer. My purpose is to hide to the end user the integer. In this case, the integer is the primary key of a database. I do not want to give this information to users because they could deduce the number of records in the database or the increment of records by week.

哈希不是唯一的,所以也许我需要使用像TripleDes这样的加密,但是我想使用快速简单的东西。此外,TripleDes也返回太多的字节。

Hashes are not unique, so maybe I need to use encryption like TripleDes or so, but I wanted to use something fast and simple. Also, TripleDes returns too many bytes too.

更新2:
我在谈论散列,这是一个错误。实际上,我试图混淆它,而且我使用哈希算法来尝试,这不是一个好主意,因为它们不是唯一的。

UPDATE 2: I was talking about hashes and it is an error. In reality, I am trying to obfuscate it, and I tried it using hash algorithm, that it is not a good idea because they are not unique.

推荐答案

使用Skip32加密,产生32位输出。我发现这个C#实现,但不能保证其正确性。 Skip32是一个相对罕见的加密选择,可能还没有被分析太多。仍然应该足以使您的混淆目的。

Encrypt it with Skip32, which produces a 32 bit output. I found this C# implementation but can't vouch for its correctness. Skip32 is a relatively uncommon crypto choice and probably hasn't been analyzed much. Still it should be sufficient for your obfuscation purposes.

强大的选择是在FFX模式下使用AES格式保存加密。但是对于您的应用程序来说,这是非常复杂的,可能是过度的。

The strong choice would be format preserving encryption using AES in FFX mode. But that's pretty complicated and probably overkill for your application.

当使用Base32(不区分大小写,字母数字)编码时,32位值对应于7个字符。当用十六进制编码时,它对应于8个字符。

When encoded with Base32 (case insensitive, alphanumeric) a 32 bit value corresponds to 7 characters. When encoded in hex, it corresponds to 8 characters.

还有生成随机值的非加密替代方法,将其存储在数据库中并处理冲突。

There is also the non cryptographic alternative of generating a random value, storing it in the database and handling collisions.

这篇关于如何模糊一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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