3

使用在https://raw.github.com/usefulfor/usefulfor/master/security/JBoss.java找到的代码,我做了以下事情:

bash-3.2$ java -cp . JBoss -e testpython
-27038292d345798947e2852756afcf0a
bash-3.2$ java -cp . JBoss -d -27038292d345798947e2852756afcf0a
testpython

但是,我一辈子都无法弄清楚如何在 python 中使用 pycrypto 解密字符串 '27038292d345798947e2852756afcf0a'。我的理解是 Java 代码使用 Blowfish,而短语“jaas is the way”作为密码的关键。但我无法理解如何在 python 中执行此操作。以下结果大多是不可打印的垃圾:

import Crypto
from Crypto.Cipher import Blowfish
from base64 import b64encode, b64decode

bs        = Blowfish.block_size
key       = 'jaas is the way'
plaintext = b'27038292d345798947e2852756afcf0a'
iv        = '\0' * 8

c1 = Blowfish.new(key, Blowfish.MODE_ECB)
c2 = Blowfish.new(key, Blowfish.MODE_CBC, iv)
c3 = Blowfish.new(key, Blowfish.MODE_CFB, iv)
c4 = Blowfish.new(key, Blowfish.MODE_OFB, iv)

msg1 = c1.decrypt(plaintext)
msg2 = c2.decrypt(plaintext)
msg3 = c3.decrypt(plaintext)
msg4 = c4.decrypt(plaintext)

print "msg1 = %s\n" % msg1
print "msg2 = %s\n" % msg2 
print "msg3 = %s\n" % msg3 
print "msg4 = %s\n" % msg4 

我错过了什么?

谢谢。

4

2 回答 2

5

首先,Java 示例代码非常糟糕。它将密文输出为整数,而密文应保留为二进制字符串。原因是一个整数可以用无数种二进制编码来表示。例如,数字 1 可以是“0x01”(1 个字节)、“0x0001”(2 个字节)等等。当您处理加密函数时,您必须非常精确地处理表示。

此外,该示例使用javax.cryptoAPI 的默认值,在任何地方都没有描述。所以这真的是反复试验。

对于解决方案,您必须知道如何在 Python 中将负整数转换为十六进制字符串。在这种情况下,您不需要十六进制字符串,而是它的字节表示。不过这个概念是一样的。我使用 PyCryptolong_to_bytes将正整数(任意长度)转换为字节字符串。

from Crypto.Cipher import Blowfish
from Crypto.Util.number import long_to_bytes

def tobytestring(val, nbits):
    """Convert an integer (val, even negative) to its byte string representation.
    Parameter nbits is the length of the desired byte string (in bits).
    """
    return long_to_bytes((val + (1 << nbits)) % (1 << nbits), nbits/8)

key = b'jaas is the way'
c1  = Blowfish.new(key, Blowfish.MODE_ECB)

fromjava = b"-27038292d345798947e2852756afcf0a"
# We don't know the real length of the ciphertext, assume it is 16 bytes
ciphertext = tobytestring(int(fromjava, 16), 16*8)
print c1.decrypt(ciphertext)

输出是:

'testpython\x06\x06\x06\x06\x06\x06'

从中您可以看到,javax.crypto它还添加了 PKCS#5 填充,您需要自行删除。不过,这是微不足道的。

但是,解决问题的真正方法是以更好的方式进行 Java 加密。Python 代码将大大简化。

于 2012-06-01T08:22:27.437 回答
0

这对我有帮助

private byte[] encrypt(String key, String plainText) throws GeneralSecurityException {

    SecretKey secret_key = new SecretKeySpec(key.getBytes(), ALGORITM);

    Cipher cipher = Cipher.getInstance(ALGORITM);
    cipher.init(Cipher.ENCRYPT_MODE, secret_key);

    return cipher.doFinal(plainText.getBytes());
}

希望这对你有用,更多http://dexxtr.com/post/57145943236/blowfish-encrypt-and-decrypt-in-java-android

于 2013-08-02T13:20:05.293 回答