Take the 2-minute tour ×

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/security/blowfish.aspx)

share|improve this question
yes. you should change somehow the algo –  BlackBear Mar 15 '11 at 22:37
why don't you encode in the opposite endianess so that it decrypts properly? –  user195488 Mar 16 '11 at 0:45
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

It is possible that the bytes are reversed in some capacity, but that usually only happens with numbers (see http://beej.us/guide/bgnet/output/html/multipage/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.)

share|improve this answer
add comment

Your Answer


