zhaojiarui02 commited on
Commit ·
e01acd9
1
Parent(s): 5fad724
initial
Browse files- decrypt.py +73 -0
decrypt.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Decrypt the LoHoSearch dataset CSV.
|
| 2 |
+
|
| 3 |
+
The encrypted CSV must have three columns: question, answer, canary.
|
| 4 |
+
Each question/answer is base64(XOR(plaintext, SHA256-derived keystream)).
|
| 5 |
+
|
| 6 |
+
Decrypts every row's question and answer and writes them to an output CSV.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import argparse
|
| 10 |
+
import base64
|
| 11 |
+
import csv
|
| 12 |
+
import hashlib
|
| 13 |
+
import sys
|
| 14 |
+
|
| 15 |
+
ENCRYPTED_COLUMNS = ["question", "answer"]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def derive_key(password: str, length: int) -> bytes:
|
| 19 |
+
"""Derive a fixed-length key from the password using SHA256."""
|
| 20 |
+
hasher = hashlib.sha256()
|
| 21 |
+
hasher.update(password.encode())
|
| 22 |
+
key = hasher.digest()
|
| 23 |
+
return key * (length // len(key)) + key[: length % len(key)]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def decrypt(ciphertext_b64: str, password: str) -> str:
|
| 27 |
+
"""Decrypt base64-encoded ciphertext with XOR."""
|
| 28 |
+
encrypted = base64.b64decode(ciphertext_b64)
|
| 29 |
+
key = derive_key(password, len(encrypted))
|
| 30 |
+
decrypted = bytes(a ^ b for a, b in zip(encrypted, key))
|
| 31 |
+
return decrypted.decode()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def read_rows(path: str):
|
| 35 |
+
with open(path, newline="", encoding="utf-8") as f:
|
| 36 |
+
reader = csv.DictReader(f)
|
| 37 |
+
return list(reader), (reader.fieldnames or [])
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def main() -> None:
|
| 41 |
+
parser = argparse.ArgumentParser(description="Decrypt the LoHoSearch dataset CSV.")
|
| 42 |
+
parser.add_argument("encrypted", help="Encrypted CSV with question, answer, canary columns.")
|
| 43 |
+
parser.add_argument(
|
| 44 |
+
"--output",
|
| 45 |
+
default="LoHoSearch_decrypted.csv",
|
| 46 |
+
help="Path to write the decrypted rows (question, answer).",
|
| 47 |
+
)
|
| 48 |
+
args = parser.parse_args()
|
| 49 |
+
|
| 50 |
+
csv.field_size_limit(sys.maxsize)
|
| 51 |
+
|
| 52 |
+
enc_rows, enc_fields = read_rows(args.encrypted)
|
| 53 |
+
|
| 54 |
+
for col in ENCRYPTED_COLUMNS + ["canary"]:
|
| 55 |
+
if col not in enc_fields:
|
| 56 |
+
raise SystemExit(f"Encrypted CSV is missing column: {col}")
|
| 57 |
+
|
| 58 |
+
decrypted_rows = []
|
| 59 |
+
for enc in enc_rows:
|
| 60 |
+
canary = enc["canary"]
|
| 61 |
+
decrypted_rows.append(
|
| 62 |
+
{col: decrypt(enc[col], canary) for col in ENCRYPTED_COLUMNS}
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
with open(args.output, "w", newline="", encoding="utf-8") as f:
|
| 66 |
+
writer = csv.DictWriter(f, fieldnames=ENCRYPTED_COLUMNS)
|
| 67 |
+
writer.writeheader()
|
| 68 |
+
writer.writerows(decrypted_rows)
|
| 69 |
+
print(f"Wrote {len(decrypted_rows)} decrypted rows -> {args.output}")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|