接口加密

This commit is contained in:
冯辉
2026-04-30 13:28:49 +08:00
parent 5007c3ca4e
commit e40f0ebab8
4 changed files with 128 additions and 7 deletions

37
utils/crypto.js Normal file
View File

@@ -0,0 +1,37 @@
import { sm4 } from 'sm-crypto';
export function sm4Decrypt(key, value, mode = "hex") {
try {
if (key.length !== 32) {
uni.showToast({ title: '密钥必须是32位16进制字符串128位', icon: 'none' });
return;
}
const decrypted = sm4.decrypt(value, key, {
mode: 'ecb',
cipherType: mode === 'hex' ? 'hex' : 'base64',
padding: 'pkcs#5'
});
return decrypted;
} catch (e) {
console.error('sm4 decrypt error:', e);
return value;
}
}
export function sm4Encrypt(key, value, mode = "hex") {
try {
if (key.length !== 32) {
uni.showToast({ title: '密钥必须是32位16进制字符串128位', icon: 'none' });
return;
}
const encrypted = sm4.encrypt(value, key, {
mode: 'ecb',
cipherType: mode === 'hex' ? 'hex' : 'base64',
padding: 'pkcs#5'
});
return encrypted;
} catch (e) {
console.error('sm4 encrypt error:', e);
return value;
}
}