Encryption 1™ - Online : 源代码
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Encryption 1</title>
<meta name="description" content="免费的基于浏览器的加密工具。使用 AES-GCM 在本地加密和解密文本或文件——无需帐户、无需安装、不存储于服务器。可离线使用。" />
<link rel="canonical" href="https://www.encryption-one.com/zh-Hans/ec1.html" />
<link rel="alternate" hreflang="en" href="https://www.encryption-one.com/ec1.html" />
<link rel="alternate" hreflang="de" href="https://www.encryption-one.com/de/ec1.html" />
<link rel="alternate" hreflang="es" href="https://www.encryption-one.com/es/ec1.html" />
<link rel="alternate" hreflang="fr" href="https://www.encryption-one.com/fr/ec1.html" />
<link rel="alternate" hreflang="ja" href="https://www.encryption-one.com/ja/ec1.html" />
<link rel="alternate" hreflang="zh-Hans" href="https://www.encryption-one.com/zh-Hans/ec1.html" />
<link rel="alternate" hreflang="x-default" href="https://www.encryption-one.com/ec1.html" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="Encryption 1" />
<meta property="og:title" content="Encryption 1" />
<meta property="og:description" content="免费的基于浏览器的加密工具。使用 AES-GCM 在本地加密和解密文本或文件——无需帐户、无需安装、不存储于服务器。可离线使用。" />
<meta property="og:url" content="https://www.encryption-one.com/zh-Hans/ec1.html" />
<meta property="og:locale" content="zh_CN" />
<meta property="og:locale:alternate" content="en_US" />
<meta property="og:locale:alternate" content="de_DE" />
<meta property="og:locale:alternate" content="es_ES" />
<meta property="og:locale:alternate" content="fr_FR" />
<meta property="og:locale:alternate" content="ja_JP" />
<!-- og:image: add once a preview image is ready, e.g.
<meta property="og:image" content="https://www.encryption-one.com/og-image.png" /> -->
<style>
:root {
--bg: #f5f7fb;
--card: #ffffff;
--text: #1f2937;
--muted: #6b7280;
--border: #dbe3ee;
--accent: #0d6efd;
--accent-2: #0b5ed7;
--danger: #b42318;
--success: #157347;
--warning-bg: #ffe6e6;
--warning-border: #ff8080;
--shadow: 0 10px 30px rgba(0,0,0,.08);
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: linear-gradient(180deg, #eef4ff 0%, var(--bg) 100%);
color: var(--text);
min-height: 100vh;
}
.nolines { text-decoration: none; font-size: small; }
.container { max-width: 980px; margin: 0 auto; padding: 24px; }
.hero { text-align: center; margin-bottom: 20px; }
.hero h1 { margin: 0 0 8px; font-size: clamp(1.8rem, 3vw, 2.4rem); }
.hero p { margin: 0; color: var(--muted); line-height: 1.55; }
.badge {
display: inline-block;
background: #e9f2ff;
color: #0a58ca;
border-radius: 999px;
padding: 6px 12px;
font-size: .85rem;
font-weight: 700;
margin-bottom: 10px;
}
.warning {
margin: 20px 0;
padding: 5px 5px;
border: 1px solid var(--warning-border);
background: var(--warning-bg);
border-radius: 14px;
color: #5f4b00;
line-height: 1.5;
font-size: small;
text-align: center;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 20px;
box-shadow: var(--shadow);
overflow: hidden;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); background: #fbfcff; flex-wrap: wrap; }
.tab-btn {
appearance: none;
border: 0;
background: transparent;
padding: 16px 20px;
cursor: pointer;
font: inherit;
font-size: 1rem;
color: var(--muted);
border-bottom: 3px solid transparent;
transition: .2s ease;
}
.tab-btn:hover,
.tab-btn.active {
color: var(--accent);
border-bottom-color: var(--accent);
background: rgba(13, 110, 253, .04);
}
.panel { display: none; padding: 24px; }
.panel.active { display: block; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 820px) {
.grid { grid-template-columns: 1fr; }
}
label { display: block; font-weight: 600; margin-bottom: 8px; }
textarea,
input[type="password"],
input[type="text"],
input[type="file"] {
width: 100%;
padding: 12px 14px;
border: 1px solid var(--border);
border-radius: 14px;
font: inherit;
background: #fff;
color: var(--text);
}
textarea { resize: vertical; line-height: 1.5; }
.field { margin-bottom: 16px; }
.help { margin-top: 6px; font-size: .92rem; color: var(--muted); line-height: 1.45; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 18px; }
button {
appearance: none;
border: 0;
cursor: pointer;
padding: 12px 16px;
border-radius: 14px;
font: inherit;
font-weight: 600;
transition: transform .05s ease, background .2s ease, opacity .2s ease;
}
button:active { transform: translateY(1px); }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-2); }
.btn-secondary { background: #eef2f7; color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #e3e9f2; }
.btn-secondary.small { padding: 8px 12px; font-size: .85rem; white-space: nowrap; flex: none; }
.btn-danger { background: #fbeaea; color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: #f7dede; }
.status { margin-top: 16px; min-height: 24px;font-size:small;}
.status.error { color: var(--danger); }
.status.success { color: var(--success); }
.meta {
margin-top: 16px;
padding: 14px 16px;
border-radius: 14px;
background: #f8fafc;
border: 1px solid var(--border);
color: var(--muted);
line-height: 1.5;
font-size: .95rem;
}
.mono {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
word-break: break-word;
}
.footer { margin-top: 18px; color: var(--muted); font-size: .9rem; line-height: 1.5; }
.strength-wrap { margin-top: 10px; }
.strength-bar {
width: 100%;
height: 10px;
border-radius: 999px;
background: #e9eef5;
border: 1px solid var(--border);
overflow: hidden;
}
.strength-fill {
height: 100%;
width: 0;
border-radius: 999px;
background: #d0d7e2;
transition: width .2s ease, background .2s ease;
}
.strength-text { margin-top: 8px; font-size: .92rem; color: var(--muted); line-height: 1.4; }
.dropzone {
border: 2px dashed #b9c9dc;
border-radius: 18px;
padding: 28px 18px;
text-align: center;
background: #f8fbff;
cursor: pointer;
transition: .15s;
}
.dropzone:hover,
.dropzone.dragover { border-color: var(--accent); background: #eef6ff; }
.dropzone strong { display: block; font-size: 1.05rem; margin-bottom: 5px; }
.file-info {
margin-top: 12px;
padding: 12px 14px;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 12px;
font-size: .92rem;
}
.limit { font-weight: 700; color: #0a58ca; }
.mode-note {
padding: 12px 14px;
border-radius: 12px;
background: #eef6ff;
color: #0a58ca;
margin-bottom: 18px;
font-size: .92rem;
}
.hidden { display: none; }
.password-row { display: flex; gap: 8px; align-items: stretch; }
.password-row input { flex: 1; min-width: 0; }
</style>
</head>
<body>
<div class="container">
<div class="hero">
<h1>🔐 Encryption 1</h1>
<p><strong>Encryption 1</strong> •
<a class="nolines" href="journal.html">EC-1 日志</a> •
<a class="nolines" href="faq.html">常见问题</a> •
<a class="nolines" href="security-guide.html">安全指南</a></p>
</div>
<div class="warning">
<strong>重要提示:</strong> 如果您忘记密码,您的文件和文本将无法恢复。
</div>
<div class="card">
<div class="tabs">
<button class="tab-btn active" data-tab="encryptText">加密文本</button>
<button class="tab-btn" data-tab="encryptFile">加密文件</button>
<button class="tab-btn" data-tab="decrypt">解密</button>
</div>
<div id="encryptText" class="panel active">
<!-- <div class="mode-note">The original Encryption 1 text workflow is preserved here.</div> -->
<div class="grid">
<div class="field"><label for="plainText">要加密的文本</label><textarea id="plainText" rows="13" placeholder="在此输入或粘贴文本..."></textarea></div>
<div>
<div class="field"><label for="textPassword">密码 / 口令</label><div class="password-row"><input id="textPassword" type="password" placeholder="请输入强密码" autocomplete="new-password"><button id="textToggleBtn" class="btn-secondary small" type="button" aria-pressed="false" aria-label="显示密码">显示</button><button id="textGenerateBtn" class="btn-secondary small" type="button">生成</button></div><div class="strength-wrap"><div class="strength-bar"><div id="textStrengthBar" class="strength-fill"></div></div><div id="textStrengthText" class="strength-text">密码强度:尚未输入</div></div><div id="textStatus" class="status" aria-live="polite"></div></div>
<div class="field"><label for="textPassword2">确认密码</label><input id="textPassword2" type="password" placeholder="请再次输入密码" autocomplete="new-password"></div>
<div class="field"><label for="textFilename">下载文件名</label><input id="textFilename" type="text" value="private-note.pvault"></div>
<div class="actions"><button id="encryptTextBtn" class="btn-primary">加密并下载</button><button id="clearTextBtn" class="btn-danger">清除</button></div>
<div id="textMeta" class="meta hidden"></div>
</div>
</div>
</div>
<div id="encryptFile" class="panel">
<!-- <div class="mode-note"><strong>File limit: 10 MB.</strong> Images, PDFs, Word documents, spreadsheets, ZIP files, and other files can be encrypted as raw bytes.</div> -->
<div class="grid">
<div>
<label>选择一个文件</label>
<div id="dropzone" class="dropzone" tabindex="0" role="button"><strong>将文件拖放到此处</strong><span>或点击选择文件</span><div class="help">最大文件大小:<span class="limit">10 MB</span></div></div>
<input id="fileInput" type="file" class="hidden">
<div id="fileInfo" class="file-info hidden"></div>
</div>
<div>
<div class="field"><label for="filePassword">密码 / 口令</label><div class="password-row"><input id="filePassword" type="password" placeholder="请输入强密码" autocomplete="new-password"><button id="fileToggleBtn" class="btn-secondary small" type="button" aria-pressed="false" aria-label="显示密码">显示</button><button id="fileGenerateBtn" class="btn-secondary small" type="button">生成</button></div><div class="strength-wrap"><div class="strength-bar"><div id="fileStrengthBar" class="strength-fill"></div></div><div id="fileStrengthText" class="strength-text">密码强度:尚未输入</div></div>
<div id="fileStatus" class="status" aria-live="polite"></div>
</div>
<div class="field"><label for="filePassword2">确认密码</label><input id="filePassword2" type="password" placeholder="请再次输入密码" autocomplete="new-password"></div>
<div class="actions"><button id="encryptFileBtn" class="btn-primary">加密文件并下载</button><button id="clearFileBtn" class="btn-danger">清除</button></div>
<div id="fileMeta" class="meta hidden"></div>
</div>
</div>
</div>
<div id="decrypt" class="panel">
<div class="grid">
<div>
<div class="field"><label for="encryptedFile">加密文件</label><input id="encryptedFile" type="file" accept=".pvault,.json,application/json,text/plain"><div class="help">支持 Encryption 1 文本文件以及 Encryption 1 文件包。</div></div>
<div class="field"><label for="decryptPassword">密码 / 口令</label><input id="decryptPassword" type="password" placeholder="请输入加密时使用的相同密码" autocomplete="current-password"></div>
<div class="actions"><button id="decryptBtn" class="btn-primary">解密</button><button id="clearDecryptBtn" class="btn-danger">清除</button></div>
<div id="decryptStatus" class="status" aria-live="polite"></div><div id="decryptMeta" class="meta hidden"></div>
</div>
<div>
<div id="textDecryptArea" class="field"><label for="decryptedText">解密后的文本</label><textarea id="decryptedText" placeholder="解密后的文本将显示在此处..." spellcheck="false" rows="13"></textarea></div>
<div id="textDecryptActions" class="actions"><button id="copyPlainBtn" class="btn-secondary" disabled>复制文本</button><button id="downloadPlainBtn" class="btn-secondary" disabled>下载为 .txt</button></div>
<div id="fileDecryptArea" class="meta hidden"></div>
</div>
</div>
</div>
</div>
<footer>
<div class="footer">🔐 <strong>Encryption 1</strong> © 1996-<span id="year"></span> 作者 Seán L. Young。保留所有权利<br />免费使用 • 无需帐户 • 支持离线使用 • <a href="EC-1.zip">下载 ZIP</a> • <a href="privacy.html">隐私政策</a> • <a href="tos.html">服务条款</a></div>
</footer>
<script>
document.getElementById("year").textContent = new Date().getFullYear();
</script>
<div id="browserStatus" class="status" aria-live="polite"></div>
</div>
<script>
'use strict';
const APP_VERSION = 2.0;
const PBKDF2_ITERATIONS = 250000;
const SALT_LENGTH = 16, IV_LENGTH = 12, MAX_FILE_SIZE = 10 * 1024 * 1024;
const encoder = new TextEncoder(), decoder = new TextDecoder();
const $ = id => document.getElementById(id);
const els = {
tabs: document.querySelectorAll('.tab-btn'), panels: document.querySelectorAll('.panel'),
plainText:$('plainText'), textPassword:$('textPassword'), textPassword2:$('textPassword2'), textToggleBtn:$('textToggleBtn'), textGenerateBtn:$('textGenerateBtn'), textFilename:$('textFilename'), encryptTextBtn:$('encryptTextBtn'), clearTextBtn:$('clearTextBtn'), textStatus:$('textStatus'), textMeta:$('textMeta'), textStrengthBar:$('textStrengthBar'), textStrengthText:$('textStrengthText'),
dropzone:$('dropzone'), fileInput:$('fileInput'), fileInfo:$('fileInfo'), filePassword:$('filePassword'), filePassword2:$('filePassword2'), fileToggleBtn:$('fileToggleBtn'), fileGenerateBtn:$('fileGenerateBtn'), encryptFileBtn:$('encryptFileBtn'), clearFileBtn:$('clearFileBtn'), fileStatus:$('fileStatus'), fileMeta:$('fileMeta'), fileStrengthBar:$('fileStrengthBar'), fileStrengthText:$('fileStrengthText'),
encryptedFile:$('encryptedFile'), decryptPassword:$('decryptPassword'), decryptBtn:$('decryptBtn'), clearDecryptBtn:$('clearDecryptBtn'), decryptStatus:$('decryptStatus'), decryptMeta:$('decryptMeta'), decryptedText:$('decryptedText'), copyPlainBtn:$('copyPlainBtn'), downloadPlainBtn:$('downloadPlainBtn'), textDecryptArea:$('textDecryptArea'), textDecryptActions:$('textDecryptActions'), fileDecryptArea:$('fileDecryptArea'), browserStatus:$('browserStatus')
};
let selectedFile = null, currentDecryptedText = '';
// True only while the clipboard holds a password this app generated, so Clear
// can wipe it without touching anything the user copied from elsewhere.
let generatedPasswordInClipboard = false;
function setStatus(el,msg,type=''){el.textContent=msg;el.className='status'+(type?' '+type:'')}
function showMeta(el,html){el.innerHTML=html;el.classList.remove('hidden')}
function hide(el){el.classList.add('hidden')}
function uint8ToBase64(bytes){let binary='';const chunk=0x8000;for(let i=0;i<bytes.length;i+=chunk)binary+=String.fromCharCode(...bytes.subarray(i,i+chunk));return btoa(binary)}
function base64ToUint8(base64){const binary=atob(base64);const bytes=new Uint8Array(binary.length);for(let i=0;i<binary.length;i++)bytes[i]=binary.charCodeAt(i);return bytes}
function sanitizeFilename(name){const cleaned=(name||'private-note.pvault').trim().replace(/[\\/:*?"<>|]+/g,'_');return cleaned||'private-note.pvault'}
function ensurePvaultExtension(name){return /\.pvault$/i.test(name)?name:name+'.pvault'}
function formatBytes(n){if(n<1024)return n+' B';if(n<1024*1024)return (n/1024).toFixed(1)+' KB';return (n/(1024*1024)).toFixed(2)+' MB'}
async function deriveKey(password,salt,usages){const material=await crypto.subtle.importKey('raw',encoder.encode(password),'PBKDF2',false,['deriveKey']);return crypto.subtle.deriveKey({name:'PBKDF2',salt,iterations:PBKDF2_ITERATIONS,hash:'SHA-256'},material,{name:'AES-GCM',length:256},false,usages)}
async function encryptBytes(bytes,password,kind,extra={}){const salt=crypto.getRandomValues(new Uint8Array(SALT_LENGTH)),iv=crypto.getRandomValues(new Uint8Array(IV_LENGTH));const key=await deriveKey(password,salt,['encrypt']);const encrypted=await crypto.subtle.encrypt({name:'AES-GCM',iv},key,bytes);return {version:APP_VERSION,app:'Encryption 1',format:kind==='file'?'E1A-FILE-1':'E1-TEXT-1',cipher:'AES-GCM',kdf:'PBKDF2',hash:'SHA-256',iterations:PBKDF2_ITERATIONS,salt:uint8ToBase64(salt),iv:uint8ToBase64(iv),data:uint8ToBase64(new Uint8Array(encrypted)),createdAt:new Date().toISOString(),...extra}}
async function decryptBytes(payload,password){validatePayload(payload);const salt=base64ToUint8(payload.salt),iv=base64ToUint8(payload.iv),data=base64ToUint8(payload.data);const key=await deriveKey(password,salt,['decrypt']);return new Uint8Array(await crypto.subtle.decrypt({name:'AES-GCM',iv},key,data))}
function validatePayload(p){if(!p||typeof p!=='object')throw Error('加密文件格式无效。');for(const k of ['version','cipher','kdf','salt','iv','data'])if(!(k in p))throw Error('缺少加密文件字段:'+k);if(p.cipher!=='AES-GCM'||p.kdf!=='PBKDF2')throw Error('不受支持的加密格式。');if(p.iterations!==PBKDF2_ITERATIONS)throw Error('不受支持的 PBKDF2 迭代次数。');}
function evaluatePasswordStrength(password){const c={length8:password.length>=8,uppercase:/[A-Z]/.test(password),special:/[^A-Za-z0-9]/.test(password),lowercase:/[a-z]/.test(password),number:/\d/.test(password),length12:password.length>=12,length16:password.length>=16};let score=0;if(c.length8)score+=30;if(c.uppercase)score+=20;if(c.special)score+=20;if(c.lowercase)score+=10;if(c.number)score+=10;if(c.length12)score+=5;if(c.length16)score+=5;let label='非常弱',color='#b42318';if(!password){label='尚未输入';color='#d0d7e2';score=0}else if(score>=90){label='非常强';color='#157347'}else if(score>=70){label='强';color='#2d8a57'}else if(score>=50){label='中等';color='#b7791f'}else if(score>=30){label='弱';color='#c05621'}return{score,label,color,checks:c,meetsMinimum:c.length8&&c.uppercase&&c.special}}
function updateStrength(input,bar,text){const r=evaluatePasswordStrength(input.value);bar.style.width=r.score+'%';bar.style.background=r.color;const missing=[];if(!r.checks.length8)missing.push('至少 8 个字符');if(!r.checks.uppercase)missing.push('一个大写字母');if(!r.checks.special)missing.push('一个特殊字符');text.textContent=!input.value?'密码强度:尚未输入':r.meetsMinimum?'密码强度:'+r.label+'。已满足最低要求。':'密码强度:'+r.label+'。仍需:'+missing.join('、')+'。';return r}
function downloadBlob(filename,blob){const url=URL.createObjectURL(blob),a=document.createElement('a');a.href=url;a.download=filename;document.body.appendChild(a);a.click();a.remove();setTimeout(()=>URL.revokeObjectURL(url),1000)}
function downloadPayload(filename,payload){downloadBlob(filename,new Blob([JSON.stringify(payload,null,2)],{type:'application/json'}))}
function checkSecure(){return !!window.isSecureContext&&!!window.crypto?.subtle}
function setBrowserCompatibilityStatus(){const secure=!!window.isSecureContext,cryptoSupported=!!window.crypto?.subtle,supported=secure&&cryptoSupported;if(!supported){setStatus(els.browserStatus,'此浏览器版本过旧,或未运行在安全上下文中,无法进行加密操作。请更新您的浏览器,或在较新的浏览器中打开此页面。','error');[els.encryptTextBtn,els.encryptFileBtn,els.decryptBtn].forEach(b=>b.disabled=true)}else{setStatus(els.browserStatus,'浏览器兼容性:通过','success')}}
function switchTab(name){els.tabs.forEach(b=>b.classList.toggle('active',b.dataset.tab===name));els.panels.forEach(p=>p.classList.toggle('active',p.id===name))}
function validPassword(p1,p2){if(!p1)return '请输入密码。';if(p1!==p2)return '两次输入的密码不一致。';if(!evaluatePasswordStrength(p1).meetsMinimum)return '密码要求:至少 8 个字符,且至少包含一个大写字母和一个特殊字符。';return ''}
function generateStrongPassword(length=24){const alphabet='ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*-_=+';const random=new Uint32Array(length);crypto.getRandomValues(random);let result='';for(let i=0;i<random.length;i++)result+=alphabet[random[i]%alphabet.length];return result}
function setFieldVisibility(input,toggleBtn,visible){input.type=visible?'text':'password';toggleBtn.textContent=visible?'隐藏':'显示';toggleBtn.setAttribute('aria-pressed',String(visible));toggleBtn.setAttribute('aria-label',visible?'隐藏密码':'显示密码')}
async function generateInto(input,input2,toggleBtn,statusEl,strengthFn){const password=generateStrongPassword();input.value=password;input2.value=password;setFieldVisibility(input,toggleBtn,true);strengthFn();input.focus();input.select();if(!navigator.clipboard?.writeText){setStatus(statusEl,"已生成密码并显示在上方。此浏览器不支持自动复制到剪贴板,请在加密前手动复制。",'error');return}try{await navigator.clipboard.writeText(password);generatedPasswordInClipboard=true;setStatus(statusEl,'已生成密码并复制到剪贴板。请立即将其保存到安全的地方——之后将无法恢复。点击“清除”也会一并清空剪贴板中的密码。','success')}catch{setStatus(statusEl,'已生成密码并显示在上方。自动复制到剪贴板失败——请在加密前手动复制。','error')}}
function clearGeneratedClipboard(){if(generatedPasswordInClipboard&&navigator.clipboard?.writeText){navigator.clipboard.writeText('').catch(()=>{});generatedPasswordInClipboard=false}}
els.tabs.forEach(b=>b.addEventListener('click',()=>switchTab(b.dataset.tab)));
els.textPassword.addEventListener('input',()=>updateStrength(els.textPassword,els.textStrengthBar,els.textStrengthText));
els.filePassword.addEventListener('input',()=>updateStrength(els.filePassword,els.fileStrengthBar,els.fileStrengthText));
els.textToggleBtn.addEventListener('click',()=>{setFieldVisibility(els.textPassword,els.textToggleBtn,els.textPassword.type==='password');els.textPassword.focus()});
els.fileToggleBtn.addEventListener('click',()=>{setFieldVisibility(els.filePassword,els.fileToggleBtn,els.filePassword.type==='password');els.filePassword.focus()});
els.textGenerateBtn.addEventListener('click',()=>generateInto(els.textPassword,els.textPassword2,els.textToggleBtn,els.textStatus,()=>updateStrength(els.textPassword,els.textStrengthBar,els.textStrengthText)));
els.fileGenerateBtn.addEventListener('click',()=>generateInto(els.filePassword,els.filePassword2,els.fileToggleBtn,els.fileStatus,()=>updateStrength(els.filePassword,els.fileStrengthBar,els.fileStrengthText)));
els.encryptTextBtn.addEventListener('click',async()=>{hide(els.textMeta);setStatus(els.textStatus,'');if(!checkSecure()){setStatus(els.textStatus,'此浏览器上下文未提供所需的 Web Crypto API。','error');return}const text=els.plainText.value,p=els.textPassword.value,p2=els.textPassword2.value,err=validPassword(p,p2);if(!text.trim())return setStatus(els.textStatus,'请输入要加密的文本。','error');if(err)return setStatus(els.textStatus,err,'error');try{els.encryptTextBtn.disabled=true;setStatus(els.textStatus,'正在本地加密...','success');const payload=await encryptBytes(encoder.encode(text),p,'text');downloadPayload(ensurePvaultExtension(sanitizeFilename(els.textFilename.value)),payload);showMeta(els.textMeta,'<strong>加密完成。</strong><br>文本已在本地完成加密,加密文件已下载。');setStatus(els.textStatus,'加密完成。','success')}catch(e){setStatus(els.textStatus,'加密失败:'+(e.message||e),'error')}finally{els.encryptTextBtn.disabled=false}});
function selectFile(file){if(!file)return;if(file.size>MAX_FILE_SIZE){selectedFile=null;hide(els.fileInfo);setStatus(els.fileStatus,'该文件大小为 '+formatBytes(file.size)+'。Encryption 1 支持最大 10 MB 的文件。','error');return}selectedFile=file;els.fileInfo.innerHTML='<strong>'+escapeHtml(file.name)+'</strong><br>'+formatBytes(file.size)+(file.type?' • '+escapeHtml(file.type):'');els.fileInfo.classList.remove('hidden');setStatus(els.fileStatus,'文件已准备好,可以加密。','success')}
function escapeHtml(s){return String(s).replace(/[&<>'"]/g,c=>({'&':'&','<':'<','>':'>',"'":''','"':'"'}[c]))}
els.fileInput.addEventListener('change',()=>selectFile(els.fileInput.files[0]));els.dropzone.addEventListener('click',()=>els.fileInput.click());els.dropzone.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' ')els.fileInput.click()});['dragenter','dragover'].forEach(ev=>els.dropzone.addEventListener(ev,e=>{e.preventDefault();els.dropzone.classList.add('dragover')}));['dragleave','drop'].forEach(ev=>els.dropzone.addEventListener(ev,e=>{e.preventDefault();els.dropzone.classList.remove('dragover')}));els.dropzone.addEventListener('drop',e=>selectFile(e.dataTransfer.files[0]));
els.encryptFileBtn.addEventListener('click',async()=>{hide(els.fileMeta);if(!checkSecure()){setStatus(els.fileStatus,'此浏览器上下文未提供所需的 Web Crypto API。','error');return}if(!selectedFile)return setStatus(els.fileStatus,'请先选择一个文件。','error');const p=els.filePassword.value,p2=els.filePassword2.value,err=validPassword(p,p2);if(err)return setStatus(els.fileStatus,err,'error');try{els.encryptFileBtn.disabled=true;setStatus(els.fileStatus,'正在本地读取并加密...','success');const bytes=new Uint8Array(await selectedFile.arrayBuffer());const container=encoder.encode(JSON.stringify({filename:selectedFile.name,mimeType:selectedFile.type||'application/octet-stream',size:selectedFile.size,data:uint8ToBase64(bytes)}));const payload=await encryptBytes(container,p,'file');downloadPayload(sanitizeFilename(selectedFile.name)+'.pvault',payload);showMeta(els.fileMeta,'<strong>加密完成。</strong><br>原始文件:<span class="mono">'+escapeHtml(selectedFile.name)+'</span><br>大小:'+formatBytes(selectedFile.size)+'<br>加密包已下载到本地。');setStatus(els.fileStatus,'文件加密成功。','success')}catch(e){setStatus(els.fileStatus,'加密失败:'+(e.message||e),'error')}finally{els.encryptFileBtn.disabled=false}});
els.decryptBtn.addEventListener('click',async()=>{hide(els.decryptMeta);hide(els.fileDecryptArea);els.decryptedText.value='';els.textDecryptArea.classList.remove('hidden');els.textDecryptActions.classList.remove('hidden');els.copyPlainBtn.disabled=true;els.downloadPlainBtn.disabled=true;currentDecryptedText='';if(!checkSecure()){setStatus(els.decryptStatus,'此浏览器上下文未提供所需的 Web Crypto API。','error');return}const file=els.encryptedFile.files[0],p=els.decryptPassword.value;if(!file)return setStatus(els.decryptStatus,'请选择一个已加密的 .pvault 文件。','error');if(!p)return setStatus(els.decryptStatus,'请输入密码。','error');try{els.decryptBtn.disabled=true;setStatus(els.decryptStatus,'正在本地解密...','success');const payload=JSON.parse(await file.text()),bytes=await decryptBytes(payload,p);if(payload.format==='E1A-FILE-1'){const container=JSON.parse(decoder.decode(bytes));if(!container.filename||!container.data)throw Error('加密文件包无效。');const original=base64ToUint8(container.data);const blob=new Blob([original],{type:container.mimeType||'application/octet-stream'});downloadBlob(sanitizeFilename(container.filename),blob);els.textDecryptArea.classList.add('hidden');els.textDecryptActions.classList.add('hidden');els.fileDecryptArea.innerHTML='<strong>文件解密成功。</strong><br>已恢复:<span class="mono">'+escapeHtml(container.filename)+'</span><br>大小:'+formatBytes(original.byteLength)+'<br><button id="downloadRestored" class="btn-primary" style="margin-top:12px">再次下载已恢复的文件</button>';els.fileDecryptArea.classList.remove('hidden');$('downloadRestored').addEventListener('click',()=>downloadBlob(sanitizeFilename(container.filename),new Blob([original],{type:container.mimeType||'application/octet-stream'})));showMeta(els.decryptMeta,'<strong>文件包已解密。</strong><br>原始文件已在本地恢复并下载。');setStatus(els.decryptStatus,'解密成功。','success')}else{const text=decoder.decode(bytes);currentDecryptedText=text;els.decryptedText.value=text;els.copyPlainBtn.disabled=false;els.downloadPlainBtn.disabled=false;showMeta(els.decryptMeta,'<strong>文本解密成功。</strong><br>这可能是旧版 Encryption 1 文本包,也可能是高级文本包。');setStatus(els.decryptStatus,'解密成功。','success')}}catch(e){setStatus(els.decryptStatus,'解密失败。密码可能有误,文件可能已损坏,或文件格式不受支持。','error')}finally{els.decryptBtn.disabled=false}});
els.copyPlainBtn.addEventListener('click',async()=>{try{await navigator.clipboard.writeText(currentDecryptedText);generatedPasswordInClipboard=false;setStatus(els.decryptStatus,'解密后的文本已复制到剪贴板。','success')}catch{setStatus(els.decryptStatus,'此浏览器无法将文本复制到剪贴板。','error')}});
els.downloadPlainBtn.addEventListener('click',()=>{if(currentDecryptedText)downloadBlob('decrypted-text.txt',new Blob([currentDecryptedText],{type:'text/plain;charset=utf-8'}))});
function clearText(){els.plainText.value='';els.textPassword.value='';els.textPassword2.value='';els.textFilename.value='private-note.pvault';setFieldVisibility(els.textPassword,els.textToggleBtn,false);updateStrength(els.textPassword,els.textStrengthBar,els.textStrengthText);hide(els.textMeta);setStatus(els.textStatus,'');clearGeneratedClipboard()}
function clearFile(){selectedFile=null;els.fileInput.value='';els.filePassword.value='';els.filePassword2.value='';hide(els.fileInfo);hide(els.fileMeta);setFieldVisibility(els.filePassword,els.fileToggleBtn,false);updateStrength(els.filePassword,els.fileStrengthBar,els.fileStrengthText);setStatus(els.fileStatus,'');clearGeneratedClipboard()}
function clearDecrypt(){els.encryptedFile.value='';els.decryptPassword.value='';els.decryptedText.value='';currentDecryptedText='';els.copyPlainBtn.disabled=true;els.downloadPlainBtn.disabled=true;hide(els.decryptMeta);hide(els.fileDecryptArea);els.textDecryptArea.classList.remove('hidden');els.textDecryptActions.classList.remove('hidden');setStatus(els.decryptStatus,'');clearGeneratedClipboard()}
els.clearTextBtn.addEventListener('click',clearText);els.clearFileBtn.addEventListener('click',clearFile);els.clearDecryptBtn.addEventListener('click',clearDecrypt);
updateStrength(els.textPassword,els.textStrengthBar,els.textStrengthText);updateStrength(els.filePassword,els.fileStrengthBar,els.fileStrengthText);
setBrowserCompatibilityStatus();
if(!checkSecure()){setStatus(els.textStatus,'此浏览器上下文中无法使用 Web Crypto。','error');setStatus(els.fileStatus,'此浏览器上下文中无法使用 Web Crypto。','error');setStatus(els.decryptStatus,'此浏览器上下文中无法使用 Web Crypto。','error');[els.encryptTextBtn,els.encryptFileBtn,els.decryptBtn].forEach(b=>b.disabled=true)}
</script>
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
<script>
kofiWidgetOverlay.draw('youngslanguageconsulting', {
'type': 'floating-chat',
'floating-chat.donateButton.text': '支持 EC-1',
'floating-chat.donateButton.background-color': '#d5eef6',
'floating-chat.donateButton.text-color': '#323842'
});
</script>
</body>
</html>