天空宫阙 发表于 2024-11-3 16:18

密钥已知DES解密英文正常中文乱码

DES解密英文正常中文乱码,求正确的解密方式
大概率的加密方式:DES/CBC/PKCS5Padding
密钥:iscooler

加密数据比较长放蓝奏云了,https://wwyh.lanzouw.com/ixfwr2e4lhdc


from Crypto.Cipher import DES # pip install pycryptodome
from base64 import b64encode,b64decode, encodebytes
from Crypto.Util.Padding import unpad

with open('encrypted_data.txt','r',encoding='utf-8') as f:
    encrypted_data = f.read()

'''
大概率的加密方式:DES/CBC/PKCS5Padding
密钥:iscooler
'''

encrypted_data = b64decode(encrypted_data)
# padded_data = unpad(encrypted_data, 8)# 移除PKCS5填充
cryptor = DES.new('iscooler'.encode(), DES.MODE_CBC)
ciphertext = cryptor.decrypt(encrypted_data)
print(ciphertext)
result =ciphertext.decode('UTF-8').strip()
print(result)

尝试过decode('UTF-8'), unpad(encrypted_data, 8) 未成功

grekevin 发表于 2024-11-3 16:18

from Crypto.Cipher import DES
from base64 import b64decode
from Crypto.Util.Padding import unpad

with open('encrypted_data.txt', 'r', encoding='utf-8') as f:
    encrypted_data = f.read()

# 大概率的加密方式:DES/CBC/PKCS5Padding
# 密钥:iscooler
key = 'iscooler'.encode()
iv = b'12345678'# 需要一个8字节的IV

encrypted_data = b64decode(encrypted_data)
cryptor = DES.new(key, DES.MODE_CBC, iv)
ciphertext = cryptor.decrypt(encrypted_data)
padded_data = unpad(ciphertext, DES.block_size)# 移除PKCS5填充
result = padded_data.decode('UTF-8').strip()
print(result)

天空宫阙 发表于 2024-11-3 16:21

提供的正确解密的代码不限于python,nodejs,java都可以

天空宫阙 发表于 2024-11-3 17:05

grekevin 发表于 2024-11-3 16:52
from Crypto.Cipher import DES
from base64 import b64decode
from Crypto.Ut ...
中文确实正常了,但是前面几位还是乱码,整段不好直接用json解析。能否完美解密?
Ka +V%ticle":0,"meta":{},"format":"rf","version":"1.0","segments":[{"sentid":"31910541474925569","section_begin":null,"segid":"0","start":"690","en":"A powerful ocean storm caused deadly floods and
landslides in the northern part of Vietnam over the weekend.","end":10479,"cn":"一场强烈的海洋风暴
在周末给越南北部地区带来了致命的洪水和山体滑坡。"

天空宫阙 发表于 2024-11-3 17:09

grekevin 发表于 2024-11-3 16:52
from Crypto.Cipher import DES
from base64 import b64decode
from Crypto.Ut ...

前面几位正确的是 {"read_article":0,"meta":{},"format":"rf","version":"1.0","segments":[{"sentid"

grekevin 发表于 2024-11-3 17:46

天空宫阙 发表于 2024-11-3 17:09
前面几位正确的是 {"read_article":0,"meta":{},"format":"rf","version":"1.0","segments":[{"sentid"

加密和解密的Iv要一样

天空宫阙 发表于 2024-11-3 18:21

grekevin 发表于 2024-11-3 17:46
加密和解密的Iv要一样

大佬我hook到的iv是: qazwsx 不够8个字节
var cipher = Java.use("javax.crypto.Cipher");
cipher.init('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec') is called!   
DES/CBC/PKCS5Padding init Key Utf8:iscooler
DES/CBC/PKCS5Padding init Key Hex:6973636f6f6c6572
DES/CBC/PKCS5Padding init Key Base64:aXNjb29sZXI=
DES/CBC/PKCS5Padding init iv Utf8:qazwsx
DES/CBC/PKCS5Padding init iv Hex:0171617a02777378
DES/CBC/PKCS5Padding init iv Base64:AXFhegJ3c3g=

天空宫阙 发表于 2024-11-3 18:52

我知道了iv的字符串形式有不可打印的字符,用Base64形式decode一下可以。
from Crypto.Cipher import DES
from base64 import b64decode
from Crypto.Util.Padding import unpad

import json
with open('encrypted_data.txt', 'r', encoding='utf-8') as f:
    encrypted_data = f.read()

# 大概率的加密方式:DES/CBC/PKCS5Padding
# 密钥:iscooler
key = 'iscooler'.encode()
iv = b64decode('AXFhegJ3c3g=')   # 需要一个8字节的IV iv的字符串形式有不可打印的字符用base64形式

encrypted_data = b64decode(encrypted_data)
cryptor = DES.new(key, DES.MODE_CBC,iv)
ciphertext = cryptor.decrypt(encrypted_data)
padded_data = unpad(ciphertext, DES.block_size)# 移除PKCS5填充
result = padded_data.decode('UTF-8')
print(result)
res_json = json.loads(result)
print(res_json)
页: [1]
查看完整版本: 密钥已知DES解密英文正常中文乱码