Newer
Older
smartwell_front / src / utils / security1.ts
liyaguang on 28 Feb 1 KB 正式环境问题排查
const encodedString = 'encrypt'.charCodeAt(0) - 3
export const getencodedString = () => {
  return encodedString
}
// 简易加密
export function encrypt(str) {
  if (!str) {
    return ''
  }
  let encryptedStr = '';
  // 遍历每个字符并进行加密
  for (let i = 0; i < str.length; i++) {
    // 将字符转换为 ASCII 码并加上常量 10
    let encryptedCharCode = str.charCodeAt(i) + 10;
    // 将加密后的字符拼接起来
    encryptedStr += String.fromCharCode(encryptedCharCode);
  }
  // 返回加密后的字符串
  return `${encryptedStr},${encodedString}`;
}
export function encryptNoCodedString(str) {
  if (!str) {
    return ''
  }
  let encryptedStr = '';
  // 遍历每个字符并进行加密
  for (let i = 0; i < str.length; i++) {
    // 将字符转换为 ASCII 码并加上常量 10
    let encryptedCharCode = str.charCodeAt(i) + 10;
    // 将加密后的字符拼接起来
    encryptedStr += String.fromCharCode(encryptedCharCode);
  }
  // 返回加密后的字符串
  return `${encryptedStr}`;
}
// 判断是否加密
export function isEncrypt(str) {
  if (str.includes(`,${encodedString}`)) {
    return true
  }
  else {
    return false
  }
}
export function decrypt(str) {
  if (!str) {
    return ''
  }
  if (!isEncrypt(str)) {
    return str
  }
  let decryptedNum = '';
  str = str.split(`,${encodedString}`)[0]
  // 遍历每个字符并进行解密
  for (let i = 0; i < str.length; i++) {
    // 将字符转换为 ASCII 码并减去常量 10
    let decryptedCharCode = str.charCodeAt(i) - 10;
    // 将解密后的 ASCII 码转换为数字并拼接起来
    decryptedNum += String.fromCharCode(decryptedCharCode);
  }
  // 将字符串转换为数字并返回
  return decryptedNum
  // return parseInt(decryptedNum);
}