由于我对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-¾`*ã"
packingLength == 8
的情况下,您的 PKCS5Padding 是错误的。请参阅 RFC 2898,第6.1节。 - President James K. Polk