使用 PyCrypto(尽管我在 ObjC 中也尝试过使用 OpenSSL 绑定):
from Crypto.Cipher import DES
import base64
obj=DES.new('abcdefgh', DES.MODE_ECB)
plain="Guido van Rossum is a space alien.XXXXXX"
ciph=obj.encrypt(plain)
enc=base64.b64encode(ciph)
#print ciph
print enc
输出 base64 编码值:
ESzjTnGMRFnfVOJwQfqtyXOI8yzAatioyufiSdE1dx02McNkZ2IvBg==
如果你在口译员中,ciph 会给你
'\x11,\xe3Nq\x8cDY\xdfT\xe2pA\xfa\xad\xc9s\x88\xf3,\xc0j\xd8\xa8\xca\xe7\xe2I\xd15w\x1d61\xc3dgb/\x06'
很容易。我应该能够将此输出通过管道传输到 OpenSSL 并对其进行解码:
我测试以确保 b64 解码有效 -
python enctest.py | openssl enc -base64 -d
+ python enctest.py
+ openssl enc -base64 -d
,?Nq?DY?T?pA???s??,?jب???I?5w61?dgb/
不漂亮,但你可以看到它解码得很好,“dgb”和“Nq”仍然存在。
但要全力以赴:
python enctest.py | openssl enc -base64 -d | openssl enc -nosalt -des-ecb -d -pass pass:abcdefgh
+ python enctest.py
+ openssl enc -nosalt -des-ecb -d -pass pass:abcdefgh
+ openssl enc -base64 -d
bad decrypt
15621:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:461:
j?7????vc]???LE?m³??q?
我究竟做错了什么?我试过使用 -k abcdefgh -iv 0000000000000000 或以交互方式输入密码 - 同样的问题。