Created
January 23, 2014 08:39
-
-
Save NicholasZhao/8575073 to your computer and use it in GitHub Desktop.
Sina Weibo mid encryption/decryption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A helper class to do the convert between id and mid in Sina Weibo. | |
| * Based on BASE62 encryption. | |
| */ | |
| public static class Base62Util { | |
| static String[] str62key = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", | |
| "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", | |
| "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", | |
| "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", | |
| "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; | |
| /** | |
| * Convert mid to id. | |
| * @param mid | |
| * @return | |
| */ | |
| public static String mid2id(String mid) { | |
| String id = ""; | |
| String k = mid.toString().substring(3, 4); //will be used when the 4th char is 0 | |
| if (!k.equals("0")) { | |
| for (int i = mid.length() - 4; i > -4; i = i - 4) { //4 chars as a group | |
| int offset1 = i < 0 ? 0 : i; | |
| int offset2 = i + 4; | |
| String str = mid.toString().substring(offset1, offset2); | |
| str = str62to10(str); //convert string to decimal base | |
| // if not the 1st group, add '0's if length < 7 | |
| if (offset1 > 0) { | |
| while (str.length() < 7) { | |
| str = '0' + str; | |
| } | |
| } | |
| id = str + id; | |
| } | |
| } else { | |
| for (int i = mid.length() - 4; i > -4; i = i - 4) { | |
| int offset1 = i < 0 ? 0 : i; | |
| int offset2 = i + 4; | |
| if (offset1 > -1 && offset1 < 1 || offset1 > 4) { | |
| String str = mid.toString().substring(offset1, offset2); | |
| str = str62to10(str); | |
| // if not the 1st group, add '0's if length < 7 | |
| if (offset1 > 0) { | |
| while (str.length() < 7) { | |
| str = '0' + str; | |
| } | |
| } | |
| id = str + id; | |
| } else { | |
| String str = mid.toString().substring(offset1 + 1, offset2); | |
| str = str62to10(str); | |
| // if not the 1st group, add '0's if length < 7 | |
| if (offset1 > 0) { | |
| while (str.length() < 7) { | |
| str = '0' + str; | |
| } | |
| } | |
| id = str + id; | |
| } | |
| } | |
| } | |
| return id; | |
| } | |
| /** | |
| * Convert id to mid. | |
| * | |
| * @param id | |
| * @return | |
| */ | |
| public static String id2mid(String id) { | |
| String mid = ""; | |
| for (int j = id.length() - 7; j > -7; j = j - 7) { // 7 chars as a group | |
| int offset3 = j < 0 ? 0 : j; | |
| int offset4 = j + 7; | |
| if ((j > 0 && j < 6) && (id.substring(id.length() - 14, id.length() - 13).equals("0") && id.length() == 19)) { | |
| String num = id.toString().substring(offset3 + 1, offset4); | |
| num = int10to62(Integer.valueOf(num)); // convert 10 base to 62 base | |
| mid = 0 + num + mid; | |
| if (mid.length() == 9) { | |
| mid = mid.substring(1, mid.length()); | |
| } | |
| } else { | |
| String num = id.toString().substring(offset3, offset4); | |
| num = int10to62(Integer.valueOf(num)); | |
| mid = num + mid; | |
| } | |
| } | |
| return mid; | |
| } | |
| /** | |
| * Convert 62 base to 10 base. | |
| * | |
| * @param str | |
| * @return | |
| */ | |
| public static String str62to10(String str) { | |
| String i10 = "0"; | |
| int c = 0; | |
| for (int i = 0; i < str.length(); i++) { | |
| int n = str.length() - i - 1; | |
| String s = str.substring(i, i + 1); | |
| for (int k = 0; k < str62key.length; k++) { | |
| if (s.equals(str62key[k])) { | |
| int h = k; | |
| c += (int) (h * Math.pow(62, n)); | |
| break; | |
| } | |
| } | |
| i10 = String.valueOf(c); | |
| } | |
| return i10; | |
| } | |
| /** | |
| * Convert 10 base to 62 base. | |
| * | |
| * @param int10 | |
| * @return | |
| */ | |
| public static String int10to62(double int10) { | |
| String s62 = ""; | |
| int w = (int) int10; | |
| int r = 0; | |
| int a = 0; | |
| while (w != 0) { | |
| r = (int) (w % 62); | |
| s62 = str62key[r] + s62; | |
| a = (int) (w / 62); | |
| w = (int) Math.floor(a); | |
| } | |
| return s62; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment