加密字符数组的字节序切换
我正在学校开展一个项目,我们在网络上建立了一个简单的密钥分发中心,并使用河豚加密。我已经成功地对其进行了编码,并使其在相同字节序的机器上运行。当我必须将其发送到不同字节序的机器时,就会出现问题。密钥被加密为字符数组,并通过网络发送和接收。当加密密钥打印在任一端时,它会显示相同的加密字符串,但解密失败。我尝试颠倒数组的顺序并解密,但结果是相同的。
我的问题:
在这种情况下,我对 char 数组的反转是否是处理字节序问题的正确方法?
问题是否是它已在单端式机器上加密,并且无法在小端式机器上使用相同算法解密? (这是我使用的河豚版本: http://www.codeproject.com/KB /security/blowfish.aspx)
I am working on a project at school, where we set up a simple key distribution center on our network, encrypted with blowfish. I have successfully coded it and have it working on same-endian machines. The issue arises when I have to send it to a machine of a different endian. The key is encrypted as a character array, and sent and received across the network as such. When the encrypted key is printed on either end, it displays the same encrypted string, but decrypting fails. I have tried reversing the order of the array and decrypting, but the results are the same.
My questions:
Is my reversing of the char array the proper way to deal with endian issues in this situation?
Could the issue be that it has been encrypted on a machine of one endian style and there for can not be decrypted with the same algorithm on a little endian machine? (here is the version of blowfish I used: http://www.codeproject.com/KB/security/blowfish.aspx)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于算法的实现。查看您正在使用的实现(查看
BytesToBlock
和BlockToBytes
函数),它将字节块转换为无符号整数。此转换取决于字节序,因此必须根据正在运行的机器的字节序来调整算法。
It depends on the algorithm implementation. Looking at the implementation your are using (look at the
BytesToBlock
andBlockToBytes
functions), it is casting the blocks of bytes to unsigned ints.This transformation is endian dependant and so the the algorithm will have to be adjusted based on the endianness of the machine it is being run on.
字节序适用于数据中较小的元素。如果不同的字节序机器无法读取您的
char
数组,则可能意味着它期望字节顺序相同,但位相反。因此,您需要反转
char
元素中的位顺序。您的数组顺序应该保持不变。Endianness applies to smaller elements in your data. If a different endian machine can't read your
char
array then it possibly means it is expecting bytes in the same order but with their bits reversed.So you need to reverse the order of bits in your
char
elements. Your array order should remain intact.字节可能会以某种方式反转,但这通常只发生在数字上(请参阅 https://beej.us/guide/bgnet/html/multi/htonsman.html)。更有可能的是每个字符中的位。您只需要一个简单的算法即可在解密之前切换它们。当然,如果位和字节颠倒过来,那就更有趣了。
现在,它显示相同字符串的事实使我相信问题实际上并不在于字符数组中 - 它是解密算法本身中依赖于字节顺序的东西。在这种情况下,您肯定需要查看上面的链接并考虑对多字节类型执行的操作。 (64 位字节序切换器通常必须手动编写,但字节交换并不是一个很难编写的算法。)
It is possible that the bytes are reversed in some capacity, but that usually only happens with numbers (see https://beej.us/guide/bgnet/html/multi/htonsman.html). More likely it is the bits with in each char. You just need a simple algorithm to switch them around before decrypting. Of course, if the bits AND bytes are reversed, that's even more fun.
Now, the fact that it displays the same string leads me to believe that the issue is not actually in the character array - it's something in the decryption algorithm itself that is depending on byte order. In that case you definitely need to look at the above link and think about operations you perform on multi-byte types. (64-bit endian switchers generally have to be manually written, but byte-swapping isn't a hard algorithm to write.)