开发者

Switching Endian of encrypted char array

开发者 https://www.devze.com 2023-02-16 21:16 出处:网络
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.

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:

  1. Is my reversing of the char array the proper way to deal with endian issues in this situation?

  2. 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/securit开发者_Go百科y/blowfish.aspx)


It depends on the algorithm implementation. Looking at the implementation your are using (look at the BytesToBlock and BlockToBytes 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.


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消