I have a legacy implementation of blowfish in java that I am trying to port to Python.
Java:
import blowfishj.*;
import org.apache.commons.codec.binary.Hex;
private static byte[] EncryptBlowFish(byte[] sStr, String sSecret) {
byte[] key = sSecret.getBytes();
byte[] cipher = new byte[sStr.length];
BlowfishECB blowfish = new BlowfishECB(key, 0, key.length);
blowfish.encrypt(sStr, 0, cipher, 0, sStr.length);
return (new String(Hex.encodeHex(cipher)));
}
Python:
from Crypto.Cipher import Blowfish
import binascii
def encrypt(encr_str, key_str):
cipher = Blowfish.new(key_str, Blowfish.MODE_ECB)
return binascii.hexlify(cipher.encrypt(encr_str)).decode('utf-8')
If the string to be encrypted is "12345678" and the key is "1234567890123456", the java code outputs "e00723bbb58234aa" and the python code outputs "61d2570dc6e09632".
Since the java code is legacy, I can't touch it. This indicates there's a problem in pycrypto's implementation of blowfish. However, I can confirm the accepted answer here works. Not sure why though. I tried both pycrypto as well as this blowfish module with the same result.
Any ideas how to replicate the same blowfish output in Python as the legacy java code?