diff --git a/src/App.tsx b/src/App.tsx
index f341b31..eb32db0 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -260,16 +260,6 @@ function App() {
Hex / UTF8 转换
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
)}
+
+ {/* 页脚 */}
+
)
}
diff --git a/src/utils/algorithm/sm4.ts b/src/utils/algorithm/sm4.ts
index ad35170..4a820d4 100644
--- a/src/utils/algorithm/sm4.ts
+++ b/src/utils/algorithm/sm4.ts
@@ -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)