加密单个 int 的方法 [英] Way to encrypt a single int
问题描述
如何以低成本对 32 位 int 进行双向加密,以便每个数字都映射到该空间中的其他 int 并以难以预测的方式返回?
How can you inexpensively two-way encrypt a 32 bit int, such that every number maps to some other int in that space and back in a way that's difficult to predict?
当然不需要在映射表中预先存储 42.9 亿个整数.
And doesn't require pre-storing 4.29 billion ints in a mapping table, of course.
推荐答案
您需要的是 32 位分组密码.不幸的是,由于 短块大小的弱点.如果您可以处理两倍于输入的加密 int,那么您可以只使用 Blowfish、TDES 或其他一些经过良好审查的 64 位分组密码.
What you want is a 32-bit block cipher. Unfortunately, most block ciphers are 64-bits or more due to the weaknesses of a short block size. If you can handle the encrypted int being twice as large as the input, then you can just use Blowfish, TDES, or some other nicely vetted 64-bit block cipher.
如果你真的需要 32 位并且不介意降低安全性,那么它很容易将 Feistel 网络密码(如 Blowfish)修剪到任何块长度,该长度是 2 的倍数且小于起始密码.对于 Blowfish,只需在两个半块之间平均分配您的输入数字,然后修剪F 函数的输出和 P 值下降到目标块大小的 1/2.这一切都可以在像往常一样键入算法后完成.
If you really need 32 bits and don't mind the decreased security then its easy enough to trim a Feistel network cipher like Blowfish down to any block length that's a multiple of 2 and less than the starting cipher. For Blowfish, just split your input number evenly between the two half blocks, and trim the output of the F function and the P-values down to 1/2 your target block size. This can all be done after keying the algorithm as usual.
这篇关于加密单个 int 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!