Search...
Menu

Encrypted/Decrypted the Phone Number

NXLink Default Encryption/Decryption

public class CryptoNumberUtil {

    private static Logger logger = LoggerFactory.getLogger(CryptoNumberUtil.class);

    /**
     * Encryption
     * @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();
        }
        // Suppose phone = 0x1234_5678
        Long newPhone = (phone & 0xff000000); // newPhone = 0x1200_0000
        newPhone += (phone & 0x0000ff00) << 8; // (phone & 0x0000ff00) << 8 = 0x0056_0000
        newPhone += (phone & 0x00ff0000) >> 8; // (phone & 0x00ff0000) >> 8 = 0x0000_3400
        newPhone += (phone & 0x0000000f) << 4; // (phone & 0x0000000f) << 4 = 0x0000_0080
        newPhone += (phone & 0x000000f0) >> 4; // (phone & 0x000000f0) >> 4 = 0x0000_0007
        newPhone ^= salt;
        return prefix + "" + newPhone + "" + suffix + "" + newPhone.toString().length();
    }

    /**
     * Decryption
     * @param phoneStr
     * @param salt
     * @return
     */
    public static String decode(String phoneStr, Long salt) {
        int len = phoneStr.length();
        // Last two digits are additional info
        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 = "";
        // Get prefix
        if (len > (newPhoneLen+2)){
            prefix = phoneStr.substring(0,len-2-newPhoneLen);
        }
        // Restore values by reversing bit operations
        newPhone ^= salt;
        Long phone = (newPhone & 0xff000000);
        phone += (newPhone >> 8) & 0x0000ff00;
        phone += (newPhone << 8) & 0x00ff0000;
        phone += (newPhone >> 4) & 0x0000000f;
        phone += (newPhone << 4) & 0x000000f0;
        // If suffix > 0, add leading zeros
        if (suffix > 0 ){
            for (int i = 0;i < suffix;i++) {
                prefix = prefix + "0";
            }
        }
        return prefix + "" + phone;
    }

    /**
     * Convert string salt to 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);
    }
}

Notes:

  • The salt value can be obtained by an admin account at:
    Management -> Call Center Settings -> Security Settings -> {Enable Number Encryption, Get Salt}

 

 

Previous
NXLINK Webhook Callback IP Address
Next
Multi-Device Usage
Last modified: 2025-09-17