Compare commits

..

No commits in common. "b32f52dbe8cedeba7abdb4fe56663e4939bb6988" and "0ef0a78c387b9996f148d339e72a8fa4252aca4f" have entirely different histories.

3 changed files with 21 additions and 34 deletions

View File

@ -1,4 +0,0 @@
# TODO
- [ ] CI 集成,将 dist 传输到指定服务器指定目录。

View File

@ -260,17 +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>UTF8 </label>
<textarea
value={utf8Input}
onChange={(e) => setUtf8Input(e.target.value)}
placeholder="输入 UTF8 字符串"
style={{ height: '200px' }}
/>
<button onClick={handleUtf8ToHex} style={{ marginTop: '0.5rem' }}>UTF8 Hex</button>
</div>
<div className="form-group" style={{ flex: 1 }}> <div className="form-group" style={{ flex: 1 }}>
<label>Hex </label> <label>Hex </label>
<textarea <textarea
@ -279,17 +268,25 @@ function App() {
placeholder="输入 hex 字符串" placeholder="输入 hex 字符串"
style={{ height: '200px' }} style={{ height: '200px' }}
/> />
<button onClick={handleHexToUtf8} style={{ marginTop: '0.5rem' }}>UTF8 Hex</button>
</div> </div>
<div className="form-group" style={{ flex: 1 }}>
<label>UTF8 </label>
<textarea
value={utf8Input}
onChange={(e) => setUtf8Input(e.target.value)}
placeholder="输入 UTF8 字符串"
style={{ height: '200px' }}
/>
</div>
</div>
<div className="button-group">
<button onClick={handleHexToUtf8}>Hex UTF8</button>
<button onClick={handleUtf8ToHex}>UTF8 Hex</button>
</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>
) )
} }

View File

@ -82,9 +82,10 @@ 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: SM4.constants.ECB, mode: '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
@ -115,10 +116,6 @@ 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)
@ -179,9 +176,10 @@ 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: SM4.constants.ECB, mode: '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
@ -210,10 +208,6 @@ 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)