密码锁解密函数
This commit is contained in:
parent
2601cdc863
commit
0c0f0f3d58
@ -99,7 +99,6 @@ class SourceCollector:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
print("wa ! ")
|
print("wa ! ")
|
||||||
u.children.append(new_node)
|
u.children.append(new_node)
|
||||||
|
|
||||||
qsk.append((nx, ny, new_node,[]))
|
qsk.append((nx, ny, new_node,[]))
|
||||||
else:
|
else:
|
||||||
qsk.append((nx, ny, u,new_path))
|
qsk.append((nx, ny, u,new_path))
|
||||||
|
30
maze.csv
30
maze.csv
@ -1,16 +1,16 @@
|
|||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
||||||
1,0,0,1,0,e,t7,1,0,0,0,0,0,g19,0,1
|
1,0,1,0,0,0,1,e,0,0,1,t11,1,0,0,1
|
||||||
1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1
|
1,0,1,1,1,t17,1,0,1,0,1,0,0,0,t6,1
|
||||||
1,0,l25,0,t5,0,0,1,0,0,0,0,t9,0,g27,1
|
1,0,1,0,1,0,0,0,1,0,1,0,1,0,g25,1
|
||||||
1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1
|
1,0,1,0,0,0,1,0,1,0,1,0,1,1,1,1
|
||||||
1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,1
|
1,0,1,b89,1,0,1,0,1,0,1,0,1,g30,0,1
|
||||||
1,0,1,0,1,0,0,1,1,1,1,0,0,0,0,1
|
1,0,0,0,1,0,1,0,1,0,1,0,1,l11,0,1
|
||||||
1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1
|
1,g30,1,0,1,0,1,0,1,0,1,0,0,g21,0,1
|
||||||
1,1,1,0,0,0,0,1,1,1,1,0,1,0,1,1
|
1,0,1,0,1,s,1,t11,1,1,1,0,1,0,0,1
|
||||||
1,l25,g10,0,1,0,0,0,0,1,0,0,1,0,b73,1
|
1,0,1,0,1,t14,1,0,0,0,1,0,1,0,0,1
|
||||||
1,1,1,1,1,t9,1,1,g20,1,0,0,1,0,g12,1
|
1,1,1,1,1,1,1,0,1,1,1,0,1,0,0,1
|
||||||
1,0,1,0,1,0,0,1,0,1,0,0,1,0,0,1
|
1,0,0,g21,0,0,0,0,0,0,1,0,1,0,0,1
|
||||||
1,0,0,0,1,0,0,1,0,1,0,0,1,0,0,1
|
1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1
|
||||||
1,1,1,0,0,0,0,1,t7,1,0,1,1,0,0,1
|
1,0,l23,0,0,0,g17,0,0,g22,1,0,0,l26,0,1
|
||||||
1,g12,0,0,1,0,s,1,0,0,t13,0,1,l17,0,1
|
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
||||||
|
|
308
mylock.py
Normal file
308
mylock.py
Normal file
@ -0,0 +1,308 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
from Lock import PasswordLock
|
||||||
|
|
||||||
|
|
||||||
|
def is_prime(n):
|
||||||
|
"""判断一个数字是否是素数"""
|
||||||
|
if n < 2:
|
||||||
|
return False
|
||||||
|
for i in range(2, int(n ** 0.5) + 1):
|
||||||
|
if n % i == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def satisfies_prime_unique_condition(digits):
|
||||||
|
"""检查是否满足[-1, -1]条件:每位密码为素数且不重复"""
|
||||||
|
return all(is_prime(d) for d in digits) and len(set(digits)) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def crack_method1(conditions):
|
||||||
|
"""从高位到低位回溯(第一位→第二位→第三位)"""
|
||||||
|
possible_passwords = []
|
||||||
|
tries = 0
|
||||||
|
|
||||||
|
def backtrack(index, current_digits):
|
||||||
|
nonlocal tries
|
||||||
|
tries += 1
|
||||||
|
if index == 3:
|
||||||
|
for condition in conditions:
|
||||||
|
if condition == [-1, -1]:
|
||||||
|
if not satisfies_prime_unique_condition(current_digits):
|
||||||
|
return
|
||||||
|
elif len(condition) == 2:
|
||||||
|
a, t = condition
|
||||||
|
a = abs(a)
|
||||||
|
if 1 <= a <= 3:
|
||||||
|
if t == 0 and current_digits[a - 1] % 2 != 0:
|
||||||
|
return
|
||||||
|
elif t == 1 and current_digits[a - 1] % 2 != 1:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
elif len(condition) == 3:
|
||||||
|
b1, b2, b3 = condition
|
||||||
|
if b1 != -1 and current_digits[0] != b1:
|
||||||
|
return
|
||||||
|
if b2 != -1 and current_digits[1] != b2:
|
||||||
|
return
|
||||||
|
if b3 != -1 and current_digits[2] != b3:
|
||||||
|
return
|
||||||
|
possible_passwords.append("".join(map(str, current_digits)))
|
||||||
|
return
|
||||||
|
|
||||||
|
for digit in range(10):
|
||||||
|
if index == 0:
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 1:
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 2:
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
current_digits.append(digit)
|
||||||
|
backtrack(index + 1, current_digits)
|
||||||
|
current_digits.pop()
|
||||||
|
|
||||||
|
backtrack(0, [])
|
||||||
|
return possible_passwords, tries
|
||||||
|
|
||||||
|
|
||||||
|
def crack_method2(conditions):
|
||||||
|
"""从第二位开始回溯(第二位→第三位→第一位)"""
|
||||||
|
possible_passwords = []
|
||||||
|
tries = 0
|
||||||
|
|
||||||
|
def backtrack(index, current_digits):
|
||||||
|
nonlocal tries
|
||||||
|
tries += 1
|
||||||
|
if index == 3:
|
||||||
|
reordered = [current_digits[2], current_digits[0], current_digits[1]]
|
||||||
|
for condition in conditions:
|
||||||
|
if condition == [-1, -1]:
|
||||||
|
if not satisfies_prime_unique_condition(reordered):
|
||||||
|
return
|
||||||
|
elif len(condition) == 2:
|
||||||
|
a, t = condition
|
||||||
|
a = abs(a)
|
||||||
|
if 1 <= a <= 3:
|
||||||
|
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||||
|
return
|
||||||
|
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
elif len(condition) == 3:
|
||||||
|
b1, b2, b3 = condition
|
||||||
|
if b1 != -1 and reordered[0] != b1:
|
||||||
|
return
|
||||||
|
if b2 != -1 and reordered[1] != b2:
|
||||||
|
return
|
||||||
|
if b3 != -1 and reordered[2] != b3:
|
||||||
|
return
|
||||||
|
possible_passwords.append("".join(map(str, reordered)))
|
||||||
|
return
|
||||||
|
|
||||||
|
for digit in range(10):
|
||||||
|
if index == 0: # 第二位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 1: # 第三位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 2: # 第一位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
current_digits.append(digit)
|
||||||
|
backtrack(index + 1, current_digits)
|
||||||
|
current_digits.pop()
|
||||||
|
|
||||||
|
backtrack(0, [])
|
||||||
|
return possible_passwords, tries
|
||||||
|
|
||||||
|
|
||||||
|
def crack_method3(conditions):
|
||||||
|
"""从第三位开始回溯(第三位→第一位→第二位)"""
|
||||||
|
possible_passwords = []
|
||||||
|
tries = 0
|
||||||
|
|
||||||
|
def backtrack(index, current_digits):
|
||||||
|
nonlocal tries
|
||||||
|
tries += 1
|
||||||
|
if index == 3:
|
||||||
|
reordered = [current_digits[1], current_digits[2], current_digits[0]]
|
||||||
|
for condition in conditions:
|
||||||
|
if condition == [-1, -1]:
|
||||||
|
if not satisfies_prime_unique_condition(reordered):
|
||||||
|
return
|
||||||
|
elif len(condition) == 2:
|
||||||
|
a, t = condition
|
||||||
|
a = abs(a)
|
||||||
|
if 1 <= a <= 3:
|
||||||
|
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||||
|
return
|
||||||
|
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
elif len(condition) == 3:
|
||||||
|
b1, b2, b3 = condition
|
||||||
|
if b1 != -1 and reordered[0] != b1:
|
||||||
|
return
|
||||||
|
if b2 != -1 and reordered[1] != b2:
|
||||||
|
return
|
||||||
|
if b3 != -1 and reordered[2] != b3:
|
||||||
|
return
|
||||||
|
possible_passwords.append("".join(map(str, reordered)))
|
||||||
|
return
|
||||||
|
|
||||||
|
for digit in range(10):
|
||||||
|
if index == 0: # 第三位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 1: # 第一位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
elif index == 2: # 第二位
|
||||||
|
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||||
|
if has_fixed_value:
|
||||||
|
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||||
|
if digit not in fixed_values:
|
||||||
|
continue
|
||||||
|
current_digits.append(digit)
|
||||||
|
backtrack(index + 1, current_digits)
|
||||||
|
current_digits.pop()
|
||||||
|
|
||||||
|
backtrack(0, [])
|
||||||
|
return possible_passwords, tries
|
||||||
|
|
||||||
|
|
||||||
|
def format_json(data):
|
||||||
|
"""自定义 JSON 格式化函数"""
|
||||||
|
lines = ['{']
|
||||||
|
|
||||||
|
# 格式化 C 线索部分
|
||||||
|
if "C" in data:
|
||||||
|
lines.append(' "C": [')
|
||||||
|
for i, item in enumerate(data["C"]):
|
||||||
|
lines.append(f" {json.dumps(item, ensure_ascii=False)}{',' if i < len(data['C']) - 1 else ''}")
|
||||||
|
lines.append(' ],')
|
||||||
|
|
||||||
|
# 格式化 L 哈希值
|
||||||
|
if "L" in data:
|
||||||
|
lines.append(f' "L": {json.dumps(data["L"], ensure_ascii=False)},')
|
||||||
|
|
||||||
|
# 格式化 password
|
||||||
|
if "password" in data:
|
||||||
|
lines.append(f' "password": {json.dumps(data["password"], ensure_ascii=False)},')
|
||||||
|
|
||||||
|
# 格式化 results
|
||||||
|
if "results" in data:
|
||||||
|
lines.append(' "results": {')
|
||||||
|
result_items = data["results"]
|
||||||
|
method_names = list(result_items.keys())
|
||||||
|
for i, method in enumerate(method_names):
|
||||||
|
line = f' "{method}": {json.dumps(result_items[method], ensure_ascii=False)}'
|
||||||
|
if i < len(result_items) - 1:
|
||||||
|
line += ","
|
||||||
|
lines.append(line)
|
||||||
|
lines.append(' }')
|
||||||
|
|
||||||
|
lines.append('}')
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# 输入和输出路径
|
||||||
|
input_dir = r"" # path
|
||||||
|
output_dir = r"" # path
|
||||||
|
|
||||||
|
# 如果输出目录不存在,则创建
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# 遍历输入目录下所有 .json 文件
|
||||||
|
for filename in os.listdir(input_dir):
|
||||||
|
if filename.endswith(".json"):
|
||||||
|
input_file_path = os.path.join(input_dir, filename)
|
||||||
|
output_file_path = os.path.join(output_dir, filename)
|
||||||
|
|
||||||
|
with open(input_file_path, 'r', encoding='utf-8') as f:
|
||||||
|
try:
|
||||||
|
# 读取原始 JSON 文本内容(保留结构)
|
||||||
|
original_content = f.read()
|
||||||
|
# 解析为字典用于处理
|
||||||
|
sample_data = json.loads(original_content)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print(f"跳过无效的 JSON 文件: {filename}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
conditions = sample_data["C"]
|
||||||
|
stored_hash = sample_data["L"]
|
||||||
|
|
||||||
|
print(f"\n正在处理文件: {filename}")
|
||||||
|
|
||||||
|
pwd1, tries1 = crack_method1(conditions)
|
||||||
|
pwd2, tries2 = crack_method2(conditions)
|
||||||
|
pwd3, tries3 = crack_method3(conditions)
|
||||||
|
|
||||||
|
lock = PasswordLock()
|
||||||
|
matched_passwords = []
|
||||||
|
|
||||||
|
for pwd in pwd1 + pwd2 + pwd3:
|
||||||
|
if pwd not in matched_passwords and lock.verify_password(pwd, stored_hash):
|
||||||
|
matched_passwords.append(pwd)
|
||||||
|
|
||||||
|
first_match = matched_passwords[0] if matched_passwords else ""
|
||||||
|
|
||||||
|
# 更新 JSON 内容中的结果部分
|
||||||
|
result_update = {
|
||||||
|
"password": first_match,
|
||||||
|
"results": {
|
||||||
|
"method1": {"tries": tries1, "password": list(map(int, first_match)) if first_match else []},
|
||||||
|
"method2": {"tries": tries2, "password": list(map(int, first_match)) if first_match else []},
|
||||||
|
"method3": {"tries": tries3, "password": list(map(int, first_match)) if first_match else []}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 加载原始内容为 dict 并更新关键字段
|
||||||
|
data = json.loads(original_content)
|
||||||
|
data.update(result_update)
|
||||||
|
|
||||||
|
# 使用自定义格式化函数生成 JSON 字符串
|
||||||
|
formatted_json = format_json(data)
|
||||||
|
|
||||||
|
# 写入文件
|
||||||
|
with open(output_file_path, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(formatted_json)
|
||||||
|
|
||||||
|
print(f"结果已保存至: {output_file_path}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user