Skip to content

Commit 961b534

Browse files
committed
Lab8
1 parent d9cbe84 commit 961b534

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

lab8/solve.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#!/usr/bin/env python3
22
import sys
3-
import claripy
3+
from z3 import BitVec, Solver, And, sat
44

55
def main():
6-
bvs = [claripy.BVS(f'k{i}', 32) for i in range(8)]
7-
solver = claripy.Solver()
6+
# 建立 8 個 32-bit BitVec 變數
7+
bvs = [BitVec(f'k{i}', 32) for i in range(8)]
8+
solver = Solver()
89

10+
# 0 <= xi <= 255
911
for x in bvs:
10-
solver.add(x >= 0)
11-
solver.add(x <= 255)
12+
solver.add(And(x >= 0, x <= 255))
1213

14+
# 約束條件
1315
solver.add(bvs[0] ^ bvs[1] == 0x55)
1416
solver.add(bvs[2] + bvs[3] == 200)
1517
solver.add(bvs[4] * 3 == bvs[5])
1618
solver.add(bvs[6] - bvs[7] == 1)
1719
solver.add(bvs[1] + bvs[2] - bvs[3] == 50)
1820
solver.add(bvs[5] ^ bvs[6] == 0x2A)
1921

22+
# 限制為可列印 ASCII 字元 (0x20–0x7e),且不等於換行符
2023
for x in bvs:
21-
solver.add(x >= 0x20)
22-
solver.add(x <= 0x7e)
23-
solver.add(x != 0x0a)
24+
solver.add(And(x >= 0x20, x <= 0x7e, x != 0x0A))
2425

25-
if solver.satisfiable():
26-
vals = [solver.eval(x, 1)[0] for x in bvs]
26+
# 求解並輸出
27+
if solver.check() == sat:
28+
model = solver.model()
29+
vals = [model[x].as_long() for x in bvs]
2730
solution = bytes(vals)
31+
# 如果需要十六進制或 ASCII 字串可以這樣:
2832
hex_str = " ".join(f"{v:02x}" for v in vals)
2933
ascii_str = solution.decode('ascii', errors='replace')
34+
# 直接把原始 bytes 寫到 stdout
3035
sys.stdout.buffer.write(solution)
36+
# 以下兩行可選:印出 hex / ASCII 方便檢查
37+
# print("\nhex:", hex_str)
38+
# print("ascii:", ascii_str)
3139
else:
3240
sys.exit(1)
3341

0 commit comments

Comments
 (0)