完善细节

This commit is contained in:
cheney 2026-04-15 20:41:32 +08:00
parent 0ef0a78c38
commit 0b7779312f
2 changed files with 30 additions and 21 deletions

View File

@ -260,16 +260,6 @@ function App() {
<h2>Hex / UTF8 </h2>
<div style={{ display: 'flex', gap: '1rem', marginBottom: '1rem' }}>
<div className="form-group" style={{ flex: 1 }}>
<label>Hex </label>
<textarea
value={hexInput}
onChange={(e) => setHexInput(e.target.value)}
placeholder="输入 hex 字符串"
style={{ height: '200px' }}
/>
</div>
<div className="form-group" style={{ flex: 1 }}>
<label>UTF8 </label>
<textarea
@ -278,15 +268,28 @@ function App() {
placeholder="输入 UTF8 字符串"
style={{ height: '200px' }}
/>
<button onClick={handleUtf8ToHex} style={{ marginTop: '0.5rem' }}>UTF8 Hex</button>
</div>
<div className="form-group" style={{ flex: 1 }}>
<label>Hex </label>
<textarea
value={hexInput}
onChange={(e) => setHexInput(e.target.value)}
placeholder="输入 hex 字符串"
style={{ height: '200px' }}
/>
<button onClick={handleHexToUtf8} style={{ marginTop: '0.5rem' }}>UTF8 Hex</button>
</div>
</div>
<div className="button-group">
<button onClick={handleHexToUtf8}>Hex UTF8</button>
<button onClick={handleUtf8ToHex}>UTF8 Hex</button>
</div>
</div>
)}
{/* 页脚 */}
<footer style={{ marginTop: '2rem', padding: '1rem', borderTop: '1px solid #ccc', textAlign: 'center' }}>
<p>© 2026 </p>
<p>版本号: V1.0-{new Date().toISOString().slice(0, 19).replace(/[-:T]/g, '')}</p>
</footer>
</div>
)
}

View File

@ -82,10 +82,9 @@ export const sm4Encrypt = (message: string, key: string, mode: string, iv?: stri
if (modeLower === 'ecb') {
// ECB 模式使用 gm-crypto
return SM4.encrypt(message, key, {
mode: 'ecb',
mode: SM4.constants.ECB,
inputEncoding: 'hex',
outputEncoding: 'hex',
padding: paddingMode === 'pkcs7' ? 'pkcs7' : 'none'
outputEncoding: 'hex'
})
} else if (modeLower === 'cbc') {
// CBC 模式使用 gmsm-sm4js
@ -116,6 +115,10 @@ export const sm4Encrypt = (message: string, key: string, mode: string, iv?: stri
if (!iv) {
throw new Error('IV is required for GCM mode')
}
// 检查 IV 长度是否为 12 个字节
if (iv.length !== 24) { // 12 字节的 hex 字符串长度为 24
throw new Error('IV length must be 12 bytes (24 hex characters) for GCM mode')
}
// 转换为字节数组
const messageBytes = hexToBytes(message)
const keyBytes = hexToBytes(key)
@ -176,10 +179,9 @@ export const sm4Decrypt = (encrypted: string, key: string, mode: string, iv?: st
if (modeLower === 'ecb') {
// ECB 模式使用 gm-crypto
return SM4.decrypt(encrypted, key, {
mode: 'ecb',
mode: SM4.constants.ECB,
inputEncoding: 'hex',
outputEncoding: 'hex',
padding: paddingMode === 'pkcs7' ? 'pkcs7' : 'none'
outputEncoding: 'hex'
})
} else if (modeLower === 'cbc') {
// CBC 模式使用 gmsm-sm4js
@ -208,6 +210,10 @@ export const sm4Decrypt = (encrypted: string, key: string, mode: string, iv?: st
if (!iv) {
throw new Error('IV is required for GCM mode')
}
// 检查 IV 长度是否为 12 个字节
if (iv.length !== 24) { // 12 字节的 hex 字符串长度为 24
throw new Error('IV length must be 12 bytes (24 hex characters) for GCM mode')
}
// 转换为字节数组
const encryptedBytes = hexToBytes(encrypted)
const keyBytes = hexToBytes(key)