【发布时间】:2022-01-12 04:56:08
【问题描述】:
我正在尝试将我的 Java Blowfish 加密算法转换为 Python。我正在使用 blowfish 包,它采用与 Java 库相同的参数。它们都成功执行,但是,我没有得到相同的结果。
Java 代码
public static void main(String[] args) {
try {
String mainText = "hello world";
String stBlowfishIv = "zzyyxxaa";
String stBlowfishKey = "how.good";
byte[] byteString;
IvParameterSpec iv = new IvParameterSpec(stBlowfishIv.getBytes());
SecretKey key = new SecretKeySpec(stBlowfishKey.getBytes(), "Blowfish");
Cipher c = Cipher.getInstance("Blowfish/CFB/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key, iv);
byteString = c.doFinal(mainText.getBytes());
System.out.println(Arrays.toString(byteString));
}
catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
输出
[47, -19, 48, -42, 19, 126, -105, 66, 21, -126, -44]
Python 代码
def encrypt(self, initVector="zzyyxxaa", key="how.good"):
totalString = "hello world"
initVectorBytes = bytes(initVector, 'utf-8')
keyBytes = bytes(key, 'utf-8')
totalStringBytes = bytes(totalString, 'utf-8')
cipher = blowfish.Cipher(keyBytes)
dataEncrypted = b"".join(cipher.encrypt_cfb(totalStringBytes, initVectorBytes))
print(dataEncrypted)
for byte in dataEncrypted:
print(byte, end=' ')
输出
b'/\xed0\xd6\x13~\x97B\x15\x82\xd4'
47 237 48 214 19 126 151 66 21 130 212
我们将不胜感激一些帮助或指导。
【问题讨论】:
-
是相同的输出。请注意,在每个输出数组中字符不同的位置处,两个不同的值总和为 256。这能告诉您什么吗? (提示:有符号字节与无符号字节)
标签: python java encryption byte blowfish