手机号变6位加密算法,手机号java加解密

11位手机号加密解密算法,手机号变6位。

import org.apache.commons.codec.binary.Base64;
public static String encodePhoneNumber(String number){
			int encnum = Integer.parseInt(number.substring(2));
			int prefix = (number.charAt(1) - '2') /2;   // '3'=>0, '5'=>1, '[67]'=>2, '8'=>3
			encnum |= (prefix <<= 30);
			final byte[] raw = new byte[4];
			raw[0] = (byte)(encnum >>> 24);
			raw[1] = (byte)(encnum >>> 16);
	        raw[2] = (byte)(encnum >>> 8);
	        raw[3] = (byte) encnum;
	        return Base64.encodeBase64URLSafeString(raw);
		}
		public static String decodePhoneNumber(String encoded){
			final byte[] raw = Base64.decodeBase64(encoded);
			int encnum = (raw[0]) << 24
		            | ((raw[1] & 0xFF) << 16)
		            | ((raw[2] & 0xFF) << 8)
		            |  (raw[3] & 0xFF);
			StringBuilder number = new StringBuilder(11);
			number.append('1');
			final char[] prefix = {'3','5','6','8'};
			number.append(prefix[encnum >>> 30]);
			String suffix = Integer.toString(encnum & ~(3<<30));
			int pad = 9 - suffix.length();
			if (pad > 0){
				final char[] padding = {'0','0','0','0','0','0','0','0'}; // max 8 '0's
				number.append(padding, 0, pad);
			}
			number.append(suffix);
			return number.toString();
		}

你可能感兴趣的:(JavaWeb)