#!/usr/bin/env python3 import argparse from pathlib import Path import json from encryption_utils import derive_key, decrypt_text def main(): parser = argparse.ArgumentParser(description="Batch decrypt BCV3 JSONL") parser.add_argument("--input", required=True, help="Path to train.jsonl (or archived bcv3_encrypted.jsonl)") parser.add_argument("--key-file", required=True, help="Path to a text file containing passphrase") parser.add_argument("--output-dir", required=True, help="Directory to write per-sample JSON") parser.add_argument("--keep-encrypted", action="store_true", help="Keep encrypted fields in output") args = parser.parse_args() key_str = Path(args.key_file).read_text(encoding="utf-8").strip() key = derive_key(key_str) out_dir = Path(args.output_dir) out_dir.mkdir(parents=True, exist_ok=True) count = 0 total = 0 with Path(args.input).open("r", encoding="utf-8") as f: for line in f: if not line.strip(): continue total += 1 obj = json.loads(line) # Detect format: archived bcv3_encrypted.jsonl vs current train.jsonl if "encrypted_data" in obj: enc = obj.get("encrypted_data", {}) question = decrypt_text(enc.get("question", {}), key) answer = decrypt_text(enc.get("answer", {}), key) elif "encrypted_question" in obj: enc_q = json.loads(obj.get("encrypted_question", "{}")) enc_a = json.loads(obj.get("encrypted_answer", "{}")) question = decrypt_text(enc_q, key) answer = decrypt_text(enc_a, key) else: print(f"Warning: Unrecognized format in record {total}, skipping...") continue out_obj = obj.copy() out_obj["question"] = question out_obj["answer"] = answer if not args.keep_encrypted: out_obj.pop("encrypted_data", None) out_obj.pop("encrypted_question", None) out_obj.pop("encrypted_answer", None) out_obj.pop("trajectory", None) out_path = out_dir / f"{obj.get('id')}.json" out_path.write_text(json.dumps(out_obj, ensure_ascii=False, indent=2), encoding="utf-8") count += 1 print(f"Decrypted {count}/{total} samples and wrote to {out_dir}") if __name__ == "__main__": main()