Blowfish的java和Python实现产生不同的结果

Blowfish的java和Python实现产生不同的结果,java,python-3.x,pycrypto,blowfish,Java,Python 3.x,Pycrypto,Blowfish,我有一个用java实现的传统blowfish,我正在尝试将其移植到Python 爪哇: 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

我有一个用java实现的传统blowfish,我正在尝试将其移植到Python

爪哇:

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')
如果要加密的字符串为“12345678”,密钥为“1234567890123456”,则java代码输出“E00723BBB58234A”,python代码输出“61d2570dc6e09632”

因为java代码是遗留的,所以我不能碰它。表示pycrypto对河豚的实现存在问题。但是,我可以确认接受的答案是有效的。不知道为什么。我尝试了pycrypto和pycrypto,结果相同


你知道如何在Python中复制与传统java代码相同的河豚输出吗?

所有这些都归功于@Kai Iskratsch指出了正确的方向

参考:

  • 这是对我有用的代码

    Python:

    from Crypto.Cipher import Blowfish
    from binascii import hexlify
    
    def encrypt(key, string):
        """
        Encrypts input string using BlowFish-Compat ECB algorithm.
        :param key: secret key
        :param string: string to encrypt
        :return: encrypted string
        """
        cipher = Blowfish.new(key, Blowfish.MODE_ECB)
        return hexlify(_reverse_bytes(cipher.encrypt(_reverse_bytes(string)))).decode('utf-8')
    
    @staticmethod
    def _reverse_bytes(data):
        """
        Takes data and reverses byte order to fit blowfish-compat format. For example, using _reverse_bytes('12345678')
        will return 43218765.
        :param data as bytes
        :return: reversed bytes
        """
        data_size = 0
        for n in data:
            data_size += 1
    
        reversed_bytes = bytearray()
        i = 0
        for x in range(0, data_size // 4):
            a = (data[i:i + 4])
            i += 4
            z = 0
    
            n0 = a[z]
            n1 = a[z + 1]
            n2 = a[z + 2]
            n3 = a[z + 3]
            reversed_bytes.append(n3)
            reversed_bytes.append(n2)
            reversed_bytes.append(n1)
            reversed_bytes.append(n0)
    
        return bytes(reversed_bytes)
    

    您的java代码似乎使用了河豚的ECB模式。您是否检查了pyhon是否也在ECB模式下运行,而不是在CBC或其他模式下运行?我使用的是
    cipher=blowfish.new(key\u str,blowfish.mode\u ECB)
    。这不是将python河豚设置为ECB模式吗?是的,你是对的,在我自己检查了一下之后,我认为python实际上为河豚生成了正确的结果。java似乎是河豚紧凑型什么是河豚紧凑型?请你详细说明一下好吗?有没有一种方法可以在python上实现这一点?以前从未听说过,但现在我已经在测试时选择作为选项的工具中看到了它。它似乎是由一个bug创建的,当时有人并没有按照算法的规定在河豚加密中使用big-endian字节顺序。这里已经有一个问题了:
    from Crypto.Cipher import Blowfish
    from binascii import hexlify
    
    def encrypt(key, string):
        """
        Encrypts input string using BlowFish-Compat ECB algorithm.
        :param key: secret key
        :param string: string to encrypt
        :return: encrypted string
        """
        cipher = Blowfish.new(key, Blowfish.MODE_ECB)
        return hexlify(_reverse_bytes(cipher.encrypt(_reverse_bytes(string)))).decode('utf-8')
    
    @staticmethod
    def _reverse_bytes(data):
        """
        Takes data and reverses byte order to fit blowfish-compat format. For example, using _reverse_bytes('12345678')
        will return 43218765.
        :param data as bytes
        :return: reversed bytes
        """
        data_size = 0
        for n in data:
            data_size += 1
    
        reversed_bytes = bytearray()
        i = 0
        for x in range(0, data_size // 4):
            a = (data[i:i + 4])
            i += 4
            z = 0
    
            n0 = a[z]
            n1 = a[z + 1]
            n2 = a[z + 2]
            n3 = a[z + 3]
            reversed_bytes.append(n3)
            reversed_bytes.append(n2)
            reversed_bytes.append(n1)
            reversed_bytes.append(n0)
    
        return bytes(reversed_bytes)