完善细节
This commit is contained in:
parent
0ef0a78c38
commit
0b7779312f
31
src/App.tsx
31
src/App.tsx
@ -260,16 +260,6 @@ function App() {
|
|||||||
<h2>Hex / UTF8 转换</h2>
|
<h2>Hex / UTF8 转换</h2>
|
||||||
|
|
||||||
<div style={{ display: 'flex', gap: '1rem', marginBottom: '1rem' }}>
|
<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 }}>
|
<div className="form-group" style={{ flex: 1 }}>
|
||||||
<label>UTF8 输入</label>
|
<label>UTF8 输入</label>
|
||||||
<textarea
|
<textarea
|
||||||
@ -278,15 +268,28 @@ function App() {
|
|||||||
placeholder="输入 UTF8 字符串"
|
placeholder="输入 UTF8 字符串"
|
||||||
style={{ height: '200px' }}
|
style={{ height: '200px' }}
|
||||||
/>
|
/>
|
||||||
|
<button onClick={handleUtf8ToHex} style={{ marginTop: '0.5rem' }}>UTF8 → Hex</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="button-group">
|
<div className="form-group" style={{ flex: 1 }}>
|
||||||
<button onClick={handleHexToUtf8}>Hex → UTF8</button>
|
<label>Hex 输入</label>
|
||||||
<button onClick={handleUtf8ToHex}>UTF8 → Hex</button>
|
<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>
|
||||||
</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>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -82,10 +82,9 @@ export const sm4Encrypt = (message: string, key: string, mode: string, iv?: stri
|
|||||||
if (modeLower === 'ecb') {
|
if (modeLower === 'ecb') {
|
||||||
// ECB 模式使用 gm-crypto
|
// ECB 模式使用 gm-crypto
|
||||||
return SM4.encrypt(message, key, {
|
return SM4.encrypt(message, key, {
|
||||||
mode: 'ecb',
|
mode: SM4.constants.ECB,
|
||||||
inputEncoding: 'hex',
|
inputEncoding: 'hex',
|
||||||
outputEncoding: 'hex',
|
outputEncoding: 'hex'
|
||||||
padding: paddingMode === 'pkcs7' ? 'pkcs7' : 'none'
|
|
||||||
})
|
})
|
||||||
} else if (modeLower === 'cbc') {
|
} else if (modeLower === 'cbc') {
|
||||||
// CBC 模式使用 gmsm-sm4js
|
// CBC 模式使用 gmsm-sm4js
|
||||||
@ -116,6 +115,10 @@ export const sm4Encrypt = (message: string, key: string, mode: string, iv?: stri
|
|||||||
if (!iv) {
|
if (!iv) {
|
||||||
throw new Error('IV is required for GCM mode')
|
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 messageBytes = hexToBytes(message)
|
||||||
const keyBytes = hexToBytes(key)
|
const keyBytes = hexToBytes(key)
|
||||||
@ -176,10 +179,9 @@ export const sm4Decrypt = (encrypted: string, key: string, mode: string, iv?: st
|
|||||||
if (modeLower === 'ecb') {
|
if (modeLower === 'ecb') {
|
||||||
// ECB 模式使用 gm-crypto
|
// ECB 模式使用 gm-crypto
|
||||||
return SM4.decrypt(encrypted, key, {
|
return SM4.decrypt(encrypted, key, {
|
||||||
mode: 'ecb',
|
mode: SM4.constants.ECB,
|
||||||
inputEncoding: 'hex',
|
inputEncoding: 'hex',
|
||||||
outputEncoding: 'hex',
|
outputEncoding: 'hex'
|
||||||
padding: paddingMode === 'pkcs7' ? 'pkcs7' : 'none'
|
|
||||||
})
|
})
|
||||||
} else if (modeLower === 'cbc') {
|
} else if (modeLower === 'cbc') {
|
||||||
// CBC 模式使用 gmsm-sm4js
|
// CBC 模式使用 gmsm-sm4js
|
||||||
@ -208,6 +210,10 @@ export const sm4Decrypt = (encrypted: string, key: string, mode: string, iv?: st
|
|||||||
if (!iv) {
|
if (!iv) {
|
||||||
throw new Error('IV is required for GCM mode')
|
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 encryptedBytes = hexToBytes(encrypted)
|
||||||
const keyBytes = hexToBytes(key)
|
const keyBytes = hexToBytes(key)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user