Files
ks-app-employment-service/utils/crypto.js
2026-04-30 13:28:49 +08:00

38 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}