牛信默认加解密
public class CryptoNumberUtil { private static Logger logger = LoggerFactory.getLogger(CryptoNumberUtil.class); /** * 加密 * @param phone * @param salt * @return */ public static String encode(Long phone, Long salt) { String phoneStr = phone + ""; int len = phoneStr.length(); String prefix = ""; int suffix = 0; if (len > 8) { prefix = phoneStr.substring(0, len - 8); phoneStr = phoneStr.substring(len - 8); phone = Long.valueOf(phoneStr); suffix = 8 - phone.toString().length(); } else { phone = Long.valueOf(phoneStr); suffix = len - phone.toString().length(); } // 假设此处phone=0x1234_5678 Long newPhone = (phone & 0xff000000); /// newphone=0x1200_0000 newPhone += (phone & 0x0000ff00) << 8; //(phone & 0x0000ff00) << 8 = 0x0000_5600 << 8 = 0x0056_0000 newPhone += (phone & 0x00ff0000) >> 8; //(phone & 0x00ff0000) >> 8 = 0x0034_0000 >> 8 = 0x0000_3400 newPhone += (phone & 0x0000000f) << 4; //(phone & 0x0000000f) << 4 = 0x0000_0008 << 4 = 0x0000_0080 newPhone += (phone & 0x000000f0) >> 4; //(phone & 0x000000f0) >> 4 = 0x0000_0070 >> 4 = 0x0000_0007 newPhone ^= salt; return prefix + "" + newPhone + "" + suffix + "" + newPhone.toString().length(); } /** * 解密 * @param phoneStr * @param salt * @return */ public static String decode(String phoneStr, Long salt) { int len = phoneStr.length(); //后面两位是附加信息 if (len < 2) { return null; } int newPhoneLen = Integer.parseInt(phoneStr.substring(len - 1)); if(newPhoneLen == 0){ return null; } int suffix = Integer.parseInt(phoneStr.substring(len-2, len-1)); if (2 + newPhoneLen > len) { return null; } Long newPhone = Long.valueOf(phoneStr.substring(len-2-newPhoneLen, len-2)); String prefix = ""; //获取前缀 if (len > (newPhoneLen+2)){ prefix = phoneStr.substring(0,len-2-newPhoneLen); } //还原后面的值,按十六进制的位数来各自还原,并将它们相加或者| newPhone ^= salt; Long phone = (newPhone & 0xff000000); phone += (newPhone >> 8) & 0x0000ff00; phone += (newPhone << 8) & 0x00ff0000; phone += (newPhone >> 4) & 0x0000000f; phone += (newPhone << 4) & 0x000000f0; //suffix不为0,则表示需要在中间补零 if (suffix > 0 ){ for (int i = 0;i < suffix;i++) { prefix = prefix + "0"; } } return prefix + "" + phone; } /** * 字符串的盐转成long * @param str * @return */ public static long djbHash(String str) { int hash = 0; for (int i = 0; i < str.length(); i++) { hash = (hash << 5) + hash + str.charAt(i); } return (long) (hash & ((1<<26)-1)); } public static void main(String[] args) { System.out.println(djbHash("11111111")); String ss = CryptoNumberUtil.decode("13686542566", djbHash("11111111")); System.out.println(ss); } }
* 其中salt可以通过管理员帐号在: 管理->呼叫中心设置->号码加密->{开启号码加密,获取盐值}
