java Python Blowfish加密

0yycz8jy  于 2023-05-15  发布在  Java
关注(0)|答案(3)|浏览(93)

由于我的Java知识不完整,我很难将此加密代码转换为Python代码。两者应该有完全相同的结果。如能提供帮助将不胜感激。

  • Java函数 *
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String s = "testings";
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        Key key = new SecretKeySpec("6#26FRL$ZWD".getBytes(), "Blowfish");
        cipher.init(1, key);
        byte[] enc_bytes = cipher.doFinal(s.getBytes());
        System.out.println(enc_bytes);
    }
}
  • Python等效 *
def PKCS5Padding(string):
    byteNum = len(string)
    packingLength = 8 - byteNum % 8
    if packingLength == 8:
        return string
    else:
        appendage = chr(packingLength) * packingLength
        return string + appendage

def PandoraEncrypt(string):
    from Crypto.Cipher import Blowfish
    key = b'6#26FRL$ZWD'
    c1  = Blowfish.new(key, Blowfish.MODE_ECB)
    packedString = PKCS5Padding(string)
    return c1.encrypt(packedString)

结果

Java函数:“??!”
Python函数:“4A-`*ã”

i7uaboj4

i7uaboj41#

使用您的示例,我得到了Python和Java的相同输出。
Java:

import java.math.BigInteger;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Blowfish1 {

    public static void main(String[] args) throws Exception {
        String s = "testings";
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        Key key = new SecretKeySpec("6#26FRL$ZWD".getBytes(), "Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] enc_bytes = cipher.doFinal(s.getBytes());
        System.out.printf("%x%n", new BigInteger(1, enc_bytes));
    }

}

Python:

from Crypto.Cipher import Blowfish
import binascii

# See @falsetru answer for the following method
#
def PKCS5Padding(string):
    byteNum = len(string)
    packingLength = 8 - byteNum % 8
    appendage = chr(packingLength) * packingLength
    return string + appendage

def PandoraEncrypt(string):
    key = b'6#26FRL$ZWD'
    c1  = Blowfish.new(key, Blowfish.MODE_ECB)
    packedString = PKCS5Padding(string)
    return c1.encrypt(packedString)

if __name__ == '__main__':
    s = 'testings'
    c = PandoraEncrypt(s)
    print(binascii.hexlify(c))

在这两种情况下,输出都是223950ff19fbea872fce0ee543692ba7

m3eecexj

m3eecexj2#

def PKCS5Padding(string):
    byteNum = len(string)
    packingLength = 8 - byteNum % 8
    appendage = chr(packingLength) * packingLength
    return string + appendage

使用chr代替str。

>>> chr(1)
'\x01'
>>> str(1)
'1'

str * int -> repeated string

>>> '!' * 5
'!!!!!'
cedebl8k

cedebl8k3#

https://stackoverflow.com/a/17139643/1645017回答正确python2.
对于python 3 https://pycryptodome.readthedocs.io/en/latest/index.html `

from Crypto.Cipher import Blowfish
import binascii

# See @falsetru answer for the following method
#
def PKCS5Padding(string):
    byteNum = len(string)
    packingLength = 8 - byteNum % 8
    appendage = chr(packingLength).encode() * packingLength
    return string + appendage

def PandoraEncrypt(string):
    key = b'6#26FRL$ZWD'
    c1  = Blowfish.new(key, Blowfish.MODE_ECB)
    packedString = PKCS5Padding(string)
    return c1.encrypt(packedString)

def PandoraDecrypt(string):
    key = b'6#26FRL$ZWD'
    c1  = Blowfish.new(key, Blowfish.MODE_ECB)
    decrypted = c1.decrypt(string)
    last_byte = decrypted[-1]
    decrypted_without_padding = decrypted[:-last_byte]
    return decrypted_without_padding

if __name__ == '__main__':
    s = b'testings'
    c = PandoraEncrypt(s)
    d = PandoraDecrypt(c)
    print(binascii.hexlify(c)) # b'223950ff19fbea872fce0ee543692ba7'
    print(d) # b'testings'

相关问题