| 6 | |
| 7 | |
| 8 | public class UserIDBase64 { |
| 9 | |
| 10 | /** |
| 11 | * userID解密 |
| 12 | * |
| 13 | * @param encodedUserID 加密后的用户id |
| 14 | * @return |
| 15 | */ |
| 16 | public static Integer decoderUserID(String encodedUserID) { |
| 17 | if (StrUtil.isBlank(encodedUserID)) { |
| 18 | return null; |
| 19 | } |
| 20 | try { |
| 21 | String reversedString = new StringBuffer(encodedUserID).reverse().toString(); |
| 22 | String base64String = reversedString.replaceAll("#", "="); |
| 23 | int userIDPos = base64String.indexOf("==") + 6; |
| 24 | String realBase64UserID = base64String.substring(userIDPos); |
| 25 | String base64Encoded = new String(Base64.getDecoder().decode(realBase64UserID.getBytes())); |
| 26 | return Integer.parseInt(base64Encoded); |
| 27 | } catch (Exception e) { |
| 28 | return null; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * 用户id加密 |
| 34 | * |
| 35 | * @param userID 用户id |
| 36 | * @return |
| 37 | */ |
| 38 | public static String encoderUserID(Integer userID) { |
| 39 | String base64UserIDEncoded = Base64.getEncoder().encodeToString((userID + "").getBytes()); |
| 40 | String currentStringBase64Encoded = Base64.getEncoder().encodeToString((System.currentTimeMillis() + "").getBytes()); |
| 41 | String keyString = currentStringBase64Encoded |
| 42 | + currentStringBase64Encoded.substring(4, 8) + base64UserIDEncoded; |
| 43 | byte[] codeBytes = keyString.getBytes(); |
| 44 | byte[] ordedBytes = new byte[codeBytes.length]; |
| 45 | for (int i = 0; i < codeBytes.length; i++) { |
| 46 | ordedBytes[i] = codeBytes[codeBytes.length - i - 1]; |
| 47 | } |
| 48 | return new String(ordedBytes).replaceAll("=", "#"); |
| 49 | } |
| 50 | |
| 51 | public static void main(String[] args) { |
| 52 | System.out.println(encoderUserID(20)); |
| 53 | System.out.println(decoderUserID("#AjMzgjM##QN1AjN4gTOzgjM3UTM")); |
| 54 | } |
| 55 | } |
nothing calls this directly
no outgoing calls
no test coverage detected