问题
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.
回答1:
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))
回答2:
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?
回答3:
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
来源:https://stackoverflow.com/questions/1859583/48-bit-blowfish