Python Blowfish加密 [英] Python Blowfish Encryption

查看:194
本文介绍了Python Blowfish加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对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 =测验;
密码密码= 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等效 p>

  def PKCS5Padding(string):
byteNum = len(string)
packingLength = 8 - byteNum%8
如果packingLength == 8:
返回字符串
其他:
appendage = chr(packingLength)* packingLength
返回字符串+附件

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

/ strong>



Java功能:??¾ô



Python功能:Ë4A-¾`* ã

解决方案

使用您的示例,我得到与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异常{
String s =testings;
密码密码= 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,新的BigInteger(1,enc_bytes));
}

}

Python:

 从Crypto.Cipher import Blowfish 
import binascii

#看到@falsetru的答案为以下方法

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)
packString = PKCS5Padding(string)
return c1.encrypt(packedString)

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

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


I am struggling due to my incomplete knowledge of Java to convert this encryption code to Python code. The two should have the exact same results. Help would be greatly appreciated.

Java Function

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 Equivalent

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)

Results

Java Function: "??¾ô"

Python Function: "Ë4A-¾`*ã"

解决方案

I get the same output for both python and Java using your example.

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))

In both cases the output is 223950ff19fbea872fce0ee543692ba7

这篇关于Python Blowfish加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆