{"record":{"id":"67d51c67d2af84f7","repo":"siyuan-note/siyuan","slug":"conf-language-316","errorCode":null,"errorMessage":"Conf.Language(316)","messagePattern":"Conf\\.Language\\(316\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":357,"sourceCode":"\tif validErr != nil {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tkek := util.DeriveKey(password, nc.MasterSalt, params)\n\tdefer zeroAndClear(kek)\n\tif nc.Checksum != computeBackupChecksum(nc) {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tif !verifyKEKMAC(nc, kek) {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tdecrypted, dErr := util.DecryptWithAAD(kek, nc.KEKVerifier, []byte(\"siyuan:kek-verifier\"))\n\tif dErr != nil || string(decrypted) != string(kekVerifierMagic) {\n\t\treturn errors.New(Conf.Language(311)) // 主密码错误\n\t}\n\n\t// 校验 KEK 能解密现存笔记本和已删除笔记本历史中的 WrappedDEK，避免导入不匹配的备份。\n\tif !verifyKEKAgainstExistingBoxes(kek, nc) || !verifyKEKAgainstEncryptedHistory(kek, nc) {\n\t\treturn errors.New(Conf.Language(316)) // 密钥不匹配\n\t}\n\n\tnc.KDFParams = params // 确保写回 Conf 的参数已经通过完整校验。\n\tnc.Enabled = true\n\n\t// 先写 backup，再提交 conf；backup 失败时 conf 尚未改变，可重试\n\tif err := writeNotebookCryptoBackupData(nc, kek); err != nil {\n\t\treturn fmt.Errorf(\"failed to persist key backup: %w\", err)\n\t}\n\tConf.m.Lock()\n\t*Conf.NotebookCrypto = *nc\n\tConf.m.Unlock()\n\tConf.Save()\n\tIncSync()\n\treturn nil\n}\n\n// saveNotebookCryptoBackup 把当前 NotebookCrypto（含 MasterSalt/KEKVerifier/KDFParams）备份到 DataDir。","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L339-L375","documentation":"ImportNotebookCryptoBackup returns Conf.Language(316) (密钥不匹配 / key mismatch) when the imported backup's KEK cannot unwrap the WrappedDEKs of the notebooks (or deleted-notebook history) already present on this machine — verifyKEKAgainstExistingBoxes or verifyKEKAgainstEncryptedHistory fails. The guard prevents importing a backup that would lock the user out of locally existing encrypted data; the backup and the local data belong to different key hierarchies.","triggerScenarios":"Calling ImportNotebookCryptoBackup on a workspace whose existing encrypted notebooks (or deleted-history boxes) were encrypted with keys from a different master password/salt than the imported backup; importing a backup from another workspace onto a machine that already has notebooks encrypted under different keys.","commonSituations":"Mixing backups between two workspaces or two machines; restoring a very old backup whose keys predate the local notebooks; after re-enabling encryption with a new password, importing the old backup; sync partially applied so local boxes use different keys than the backup.","solutions":["Import the backup that matches the keys used to encrypt the notebooks currently in this workspace (same device/workspace of origin)","If local notebooks are already inaccessible or unwanted, remove/re-export them first so the mismatch check has nothing to contradict","Verify the backup origin: re-export from the workspace that actually encrypted the existing notebooks","Do not disable or bypass the verification — it exists to prevent permanent data lockout"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before import, confirm the backup belongs to this workspace:\n// compare a non-secret identifier (BackupID/CreatedAt) with the workspace's history, or\n// verify at least one local notebook's WrappedDEK unwraps with the same password\n// via the API surface before calling the destructive import.","typeGuard":"null","tryCatchPattern":"if err := ImportNotebookCryptoBackup(raw, password); err != nil {\n    if strings.Contains(err.Error(), Conf.Language(316)) {\n        // backup keys ≠ local notebook keys: stop and locate the matching backup\n        return ErrKeyMismatch\n    }\n    return err\n}","preventionTips":["Label each backup export with its workspace and creation date","Never mix backups between workspaces or machines","After password changes, always export a fresh backup and retire old ones","Test the import on a copy of the workspace before touching production data"],"tags":["encryption","backup-import","key-mismatch","data-recovery"],"backgroundTag":"key-mismatch","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}