Files
ks-app-employment-service/utils/crypto.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-04-30 13:28:49 +08:00
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;
}
}