views:

255

answers:

3
+1  Q: 

48-bit blowfish

Is there an implementation (Java/C++/Ruby) of a blowfish algorithm that supports 48-bit data blocks? I have an encryption problem where the input and output channels are exactly 48-bits. All implementations on the net are for 64-bit blocks.

Thanks, Tom

+3  A: 

That's because Blowfish has a set block size of 64-bits. You could pad two random bytes to the end of your data.

require 'rubygems'
require 'crypt/blowfish'
blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
plain="123456"
encryptedBlock = blowfish.encrypt_block(plain+(rand(250)+5).chr+(rand(250)+5).chr)

or if your plain could be less than 6 bytes / 48 bits

encryptedBlock = blowfish.encrypt_block(plain.ljust(8))
Jonas Elfström
The only issue with adding extra bytes is that the resulting encrypted datablock will be 64 bits. I cannot truncate that and also this is more than what my data channel can support.
Tom Jinaad
I've never heard of a 48 bit block cipher. I knew there are 32 bit ones but that doesn't help you either. Could it be possible to split the 64-bit block and send it as 2x48 bits?
Jonas Elfström
+1  A: 

You could use counter-mode with blowfish. Just remember never to reuse any counter-value.

Just select a counter (it will need to be unique across all encryptions with the same key), pad the counter to 64 bits and encrypt the padded counter. Then XOR the first 48 bits of this encryption with your plaintext to gain the ciphertext. Repeat the operation on the ciphertext to decrypt.

The only problem is finding a suitable counter. If you include it with the ciphertext, you need more than 48 bits. Perhaps you have a session-id or something you can use?

Rasmus Faber
Block size and mode are very different. Using counter mode will not solve the problem.
Rook
Of course it will. Counter-mode transforms blowfish into a stream-cipher, so you can just take the first 48-bit of the stream and xor it with the plain-text to get the cipher-text (just as you would with RC4-drop as you suggest in your answer).
Rasmus Faber
+1  A: 

I recommend using RC4-drop 1024. RC4 is a stream cipher so you can encrypt an arbitrary size, if the message is less than 48bytes, then you can pad it with nulls. Drop 1024 means you throw away the first 1024 bytes of PRNG stream, to do this you can encrypt 1024 bytes of junk the first time you use it.

BitTorrent's Message Stream Encryption uses RC4-drop 1024 and here is a python implementation using the ARC4 library:

http://google.com/codesearch/p?hl=en#4FSOSMZ6Pxc/distfiles/BitTorrent-5.0.7.tar.gz|eyN-AXYL_0E/BitTorrent-5.0.7/BitTorrent/Connector.py&q=lang:python%20%22ARC4.new%22

Rook