Files
devbox/js/tools-crypto-extra.js
gitea 80ecd02bfa 补 14 个工具:格式 / 编码 / 加密 / 图片 / 文本 / 时间
- 新分组「格式」:JSON ↔ YAML、XML ↔ JSON、SQL 美化、JSON ↔ 查询串
- 编码:Gzip 压缩 + CRC32、Cookie ↔ JSON、字符编码套件(Unicode/UTF-8/HEX/全角/罗马/摩斯)
- 加密:RSA 密钥对生成、私钥签名、公钥验签(WebCrypto)
- 图片:压缩、九宫格切图、EXIF 读取
- 文本:提取器(手机号/邮箱/链接/身份证/IP/卡号)、人民币与日期大写
- 时间:番茄钟 / 秒表 / 倒计时
- README 工具数 30 → 44
2026-11-20 14:22:23 +08:00

71 lines
4.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* DevBox · 加密补齐:RSA 密钥对 / 签名 / 验签(WebCrypto,全程本地) */
(function () {
'use strict'
const { el, panelTitle, copy, toast } = DB
const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn })
const pane = (title, right) => { const p = el('div', { class: 'panel' }); if (title) p.append(panelTitle(title, right)); return p }
const ta = (ph, rows) => el('textarea', { class: 'ta', placeholder: ph, rows: rows || 8, spellcheck: 'false' })
const pem = (label, bytes) => {
const b = btoa(String.fromCharCode(...new Uint8Array(bytes)))
const lines = b.match(/.{1,64}/g).join('\n')
return `-----BEGIN ${label}-----\n${lines}\n-----END ${label}-----`
}
const derFromB64 = (b64) => Uint8Array.from(atob(b64.replace(/\s/g, '')), (c) => c.charCodeAt(0))
const pemBody = (p) => p.replace(/-----[^-]+-----/g, '').replace(/\s/g, '')
DB.tool({
id: 'rsa',
name: 'RSA 密钥与签名',
group: 'crypto',
icon: 'key',
desc: '本地生成 2048/3072 位密钥对(PEM),用私钥签名、公钥验签,SHA-256',
keywords: ['rsa', 'sign', 'verify', 'pem', '签名', '验签', '密钥'],
mount(root) {
const bits = el('select', { class: 'input' }, el('option', { value: '2048', text: '2048 位' }), el('option', { value: '3072', text: '3072 位' }), el('option', { value: '4096', text: '4096 位' }))
const priv = ta('-----BEGIN PRIVATE KEY-----\n…')
const pub = ta('-----BEGIN PUBLIC KEY-----\n…')
const data = ta('要签名的内容', 5)
const out = el('pre', { class: 'out' })
const stat = el('div', { class: 'hint' })
const gen = async () => {
stat.textContent = '生成中…'; stat.className = 'hint'
try {
const kp = await crypto.subtle.generateKey({ name: 'RSASSA-PKCS1-v1_5', modulusLength: Number(bits.value), publicExponent: new Uint8Array([1, 0, 1]), hash: 'SHA-256' }, true, ['sign', 'verify'])
priv.value = pem('PRIVATE KEY', await crypto.subtle.exportKey('pkcs8', kp.privateKey))
pub.value = pem('PUBLIC KEY', await crypto.subtle.exportKey('spki', kp.publicKey))
stat.textContent = '已生成 ' + bits.value + ' 位密钥对'; stat.className = 'hint good'
} catch (e) { stat.textContent = '生成失败:' + e.message; stat.className = 'hint bad' }
}
const importKey = (which) => {
const src = which === 'priv' ? priv.value : pub.value
return crypto.subtle.importKey('pkcs8', derFromB64(pemBody(src)), { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, [which === 'priv' ? 'sign' : 'verify'])
.catch(() => crypto.subtle.importKey('spki', derFromB64(pemBody(src)), { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, [which === 'priv' ? 'sign' : 'verify']))
}
const sign = async () => {
try {
const key = await importKey('priv')
const sig = await crypto.subtle.sign('RSASSA-PKCS1-v1_5', key, new TextEncoder().encode(data.value))
out.textContent = btoa(String.fromCharCode(...new Uint8Array(sig)))
stat.textContent = '签名完成(Base64)'; stat.className = 'hint good'
} catch (e) { stat.textContent = '签名失败:' + e.message; stat.className = 'hint bad' }
}
const verify = async () => {
try {
const key = await importKey('pub')
const sig = Uint8Array.from(atob(out.textContent.trim()), (c) => c.charCodeAt(0))
const ok = await crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, sig, new TextEncoder().encode(data.value))
stat.textContent = ok ? '验签通过 ✓' : '验签不通过 ✗'; stat.className = 'hint ' + (ok ? 'good' : 'bad')
} catch (e) { stat.textContent = '验签失败:' + e.message; stat.className = 'hint bad' }
}
root.append(
pane('密钥', el('div', { class: 'row' }, bits, btn('生成密钥对', gen), btn('复制公钥', () => copy(pub.value), 'ghost'), btn('复制私钥', () => copy(priv.value), 'ghost'))),
el('div', { class: 'grid2' }, priv, pub),
pane('签名 / 验签', el('div', { class: 'row' }, btn('用私钥签名', sign), btn('用公钥验签', verify))),
data, stat, out
)
},
})
})()