/** * 获取数字字母组合验证码 */ function getCaptcha() { let currentTimeMillis = Date.now(); $('#currentTimeMillis').val(currentTimeMillis); $.ajax({ dataType: "json", type: "POST", url: "/verification/getCaptcha?key=" + currentTimeMillis, contentType: "application/json;charset=UTF-8", success: function (result) { if (result.success) { $('#randomImage').attr('src', result.result); } else { layer.msg(result.msg, {icon: 2}); } }, error: function () { layer.msg("获取验证码失败,请重试!", {icon: 2}); } }); } //随机生成长度为length的密钥 function generateAESKey(length) { if (![16, 24, 32].includes(length)) { throw new Error("密钥长度无效。必须是128位(16字节)、192位(24字节)或256位(32字节)。"); } const key = new Uint8Array(length); crypto.getRandomValues(key); return Array.from(key).map(b => b.toString(16).padStart(2, '0')).join(''); } //AES对称算法对铭感信息进行加密 function encrypt(text, secretKey) { const keyBytes = CryptoJS.enc.Hex.parse(secretKey); const iv = CryptoJS.enc.Hex.parse('00000000000000000000000000000000'); // 16-byte zero IV for simplicity return CryptoJS.AES.encrypt(text, keyBytes, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }).toString(); } // 验证手机号格式是否正确 function isValidPhoneNumber(phone) { // 正则表达式:以1开头,第二位为3-9,后面9位是数字 const reg = /^1[3-9]\d{9}$/; return reg.test(phone); } // 验证身份证号码格式是否正确 function isValidIDCard(id) { // 1. 检查长度是否为18位 if (id.length !== 18) { return false; } // 2. 正则表达式,检查前17位是否为数字,最后一位是否是数字或X const reg = /^[1-9]\d{5}(\d{4})(\d{2})(\d{2})\d{3}(\d|X)$/; if (!reg.test(id)) { return false; } // 3. 提取出生日期部分并验证合法性 const year = id.slice(6, 10); const month = id.slice(10, 12); const day = id.slice(12, 14); // 验证出生日期是否合法 const birthDate = new Date(`${year}-${month}-${day}`); if (birthDate.getFullYear() !== parseInt(year) || birthDate.getMonth() + 1 !== parseInt(month) || birthDate.getDate() !== parseInt(day)) { return false; } // 4. 校验18位身份证的校验码 return checkIDCardChecksum(id); } // 校验身份证号码的最后一位校验码 function checkIDCardChecksum(id) { // 校验码的加权因子 const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 身份证号码前17位 const idWithoutChecksum = id.slice(0, 17); // 校验码对应的值 const checkDigits = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; // 计算加权和 let sum = 0; for (let i = 0; i < 17; i++) { sum += parseInt(idWithoutChecksum[i]) * weights[i]; } // 计算校验码 const checkIndex = sum % 11; const checkCode = checkDigits[checkIndex]; // 校验码是否匹配 return checkCode === id[17].toUpperCase(); } // 验证邮箱格式是否正确 function isValidEmail(email) { // 1. 检查基本长度范围 if (email.length < 6 || email.length > 254) { return false; } // 2. 正则表达式,检查邮箱格式 const reg = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; if (!reg.test(email)) { return false; } // 3. 检查域名部分是否合法 const atIndex = email.lastIndexOf('@'); const domain = email.slice(atIndex + 1); // 验证域名格式(至少包含一个点,且最后一部分为2-6个字母) const domainReg = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; if (!domainReg.test(domain)) { return false; } // 4. 检查邮箱中不能有连续的点号 return !email.includes('..'); }