Java 中使用 BigInteger 进行位移位
我正在使用 BigIntegers 在 Java 中实现 DES 加密。
我通过执行 BigInteger.leftShift(int n) 方法使用 Java BigIntegers 左移二进制键。 N(Kn)的密钥取决于Kn-1的移位结果。我遇到的问题是,我在生成每个密钥后打印出结果,并且移位不是预期的输出。密钥分为 2 个 Cn 和 Dn(分别为左和右)。
我专门尝试这样做: “要进行左移,请将每个位向左移动一位,除了第一位,它循环到块的末尾。”
似乎根据移位在末尾添加 O。不知道如何纠正这个问题。
Results:
c0: 11110101010100110011000011110
d0: 11110001111001100110101010100
c1: 111101010101001100110000111100
d1: 111100011110011001101010101000
c2: 11110101010100110011000011110000
d2: 11110001111001100110101010100000
c3: 1111010101010011001100001111000000
d3: 1111000111100110011010101010000000
c4: 111101010101001100110000111100000000
d4: 111100011110011001101010101000000000
c5: 11110101010100110011000011110000000000
d5: 11110001111001100110101010100000000000
c6: 1111010101010011001100001111000000000000
d6: 1111000111100110011010101010000000000000
c7: 111101010101001100110000111100000000000000
d7: 111100011110011001101010101000000000000000
c8: 1111010101010011001100001111000000000000000
d8: 1111000111100110011010101010000000000000000
c9: 111101010101001100110000111100000000000000000
d9: 111100011110011001101010101000000000000000000
c10: 11110101010100110011000011110000000000000000000
d10: 11110001111001100110101010100000000000000000000
c11: 1111010101010011001100001111000000000000000000000
d11: 1111000111100110011010101010000000000000000000000
c12: 111101010101001100110000111100000000000000000000000
d12: 111100011110011001101010101000000000000000000000000
c13: 11110101010100110011000011110000000000000000000000000
d13: 11110001111001100110101010100000000000000000000000000
c14: 1111010101010011001100001111000000000000000000000000000
d14: 1111000111100110011010101010000000000000000000000000000
c15: 11110101010100110011000011110000000000000000000000000000
d15: 11110001111001100110101010100000000000000000000000000000
I am implementing DES Encryption in Java with use of BigIntegers.
I am left shifting binary keys with Java BigIntegers by doing the BigInteger.leftShift(int n) method. Key of N (Kn) is dependent on the result of the shift of Kn-1. The problem I am getting is that I am printing out the results after each key is generated and the shifting is not the expected out put. The key is split in 2 Cn and Dn (left and right respectively).
I am specifically attempting this:
"To do a left shift, move each bit one place to the left, except for the first bit, which is cycled to the end of the block. "
It seems to tack on O's on the end depending on the shift. Not sure how to go about correcting this.
Results:
c0: 11110101010100110011000011110
d0: 11110001111001100110101010100
c1: 111101010101001100110000111100
d1: 111100011110011001101010101000
c2: 11110101010100110011000011110000
d2: 11110001111001100110101010100000
c3: 1111010101010011001100001111000000
d3: 1111000111100110011010101010000000
c4: 111101010101001100110000111100000000
d4: 111100011110011001101010101000000000
c5: 11110101010100110011000011110000000000
d5: 11110001111001100110101010100000000000
c6: 1111010101010011001100001111000000000000
d6: 1111000111100110011010101010000000000000
c7: 111101010101001100110000111100000000000000
d7: 111100011110011001101010101000000000000000
c8: 1111010101010011001100001111000000000000000
d8: 1111000111100110011010101010000000000000000
c9: 111101010101001100110000111100000000000000000
d9: 111100011110011001101010101000000000000000000
c10: 11110101010100110011000011110000000000000000000
d10: 11110001111001100110101010100000000000000000000
c11: 1111010101010011001100001111000000000000000000000
d11: 1111000111100110011010101010000000000000000000000
c12: 111101010101001100110000111100000000000000000000000
d12: 111100011110011001101010101000000000000000000000000
c13: 11110101010100110011000011110000000000000000000000000
d13: 11110001111001100110101010100000000000000000000000000
c14: 1111010101010011001100001111000000000000000000000000000
d14: 1111000111100110011010101010000000000000000000000000000
c15: 11110101010100110011000011110000000000000000000000000000
d15: 11110001111001100110101010100000000000000000000000000000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BigInteger 实现无限精度整数,因此向左移位将不断向左侧添加零。您需要旋转:
这对于 32 位数字来说效率相当低,因此您不妨使用原语来旋转 DES 的 28 位一半。我不熟悉 DES 算法,所以我假设您需要 BigInteger 来做其他事情。
BigInteger implements infinite-precision integers, so shifting to the left will keep adding zeros to the left. You need a rotate:
That's going to be rather inefficient for 32-bit numbers, so you might as well just use primitives to rotate DES' 28-bit halves. I'm not familiar with the DES algorithm, so I'll assume you need BigInteger for something else.
看来您需要循环左移。
BigInteger.shiftLeft
不是循环的。您必须组合shiftLeft
、shiftRight
、and
和or
,就像使用时一样int
和<<
。现在,
cycloLeftShift(n, L, k)
返回n
向左循环移位k
位,其中循环窗口为L。
其工作原理如下:
另请参阅
注意:如果您有固定的
L
,您可以通过缓存allOnes(L)
来优化它,而不是每次都计算它。It seems that you need a cyclic left shift.
BigInteger.shiftLeft
is not cyclic. You'd have to combineshiftLeft
,shiftRight
,and
andor
, just like you would if you were usingint
and<<
.Now,
cyclicLeftShift(n, L, k)
returnsn
cyclically-shiftedk
bits to the left, where the cycle window isL
.How this works is as follows:
See also
Note: if you have a fixed
L
, you can optimize this a bit by cachingallOnes(L)
instead of computing it every time.解决更大的问题 1) DES 已被破坏,除了与遗留系统一起使用外,不应该用于任何其他用途,2) Sun JCE 已经提供了一个实现(BouncyCastle 和其他加密提供商也是如此),3) 实现任何加密算法都是具有挑战性,并且您确实希望采用经过充分测试的实现来用于生产。
如果这是课堂练习,我会使用 byte[] 而不是 BigInteger。您需要手工做更多的事情,但它更接近 DES 的精神,因为它被设计为可以在硬件中轻松实现。
Addressing the bigger question 1) DES is broken and should never be used for anything other than working with legacy systems, 2) the Sun JCE already provides an implementation (as does BouncyCastle and other crypto providers), and 3) implementing any crypto algorithm is challenging and you really want to go with a well-tested implementation for production use.
If it's a class exercise I would use a byte[] instead of a BigInteger. You'll need to do a little bit more by hand but it's a lot closer to the spirit of DES since it was designed to be easily implemented in hardware.
我认为您使用位串实现 DES 的想法作为一种教育工具是合理的。我建议您创建一个 BitString 类来准确实现项目所需的那些位字符串方法,而不是直接使用 BigIntegers 来表示这些位字符串。在 BitString 类中,您可以使用 BigIntegers,但您可能会发现每个数组元素 1 位的简单 int 数组同样简单或更容易,或者可能是一个链接列表。
I think your idea of implementing DES using bit strings is reasonable as an educational tool. Instead of directly using BigIntegers to represent these bitstrings, I recommend you create a BitString class that implements exactly those bit string methods you need for your project. Inside the BitString class, you can use BigIntegers, but you may find that a simple int array with 1 bit per array element is just as easy or easier, or maybe a linked list.