// 密钥
const k = '12ABC'
// 输入手机号
const ccc = phoneEncode('15399907331')
console.log('加密',ccc)
console.log('解密',phoneDecode(ccc))
// 手机号加密
function phoneEncode(phone){
const a = phone.substring(0,3)
const b = phone.substring(3,7)
const c = phone.substring(7)
const code = `${btoa(~(a^k))}_${btoa(~(b^k))}_${btoa(~(c^k))}`
return encodeURIComponent(code)
}
// 手机号解密
function phoneDecode(str){
let phone = ''
try{
const pp = decodeURIComponent(str)
const[a,b,c] = pp.split('_')
const a1 = (~atob(a)^k)
const b1 = (~atob(b)^k)
const c1 = (~atob(c)^k)
phone = String(a1).padStart(3,'0') + String(b1).padStart(4,'0') + String(c1).padStart(4,'0')
}catch(e){
console.error('解密失败',e.message)
}
return phone
}
console