2
如何在Java中解碼使用HttpServerUtility.UrlTokenEncode在C#中編碼的字符串?是否有與C#的HttpServerUtility.UrlTokenDecode相當的Java?
如何在Java中解碼使用HttpServerUtility.UrlTokenEncode在C#中編碼的字符串?是否有與C#的HttpServerUtility.UrlTokenDecode相當的Java?
我試過使用org.apache.commons.codec.binary.Base64
(ctor接受一個參數,說明編碼/解碼是否是網址安全的),但事實證明它沒有像UrlTokenEncode/Decode一樣實現。
我結束了遷移C#實現對Java:
public static byte[] UrlTokenDecode(String input) {
if (input == null)
return new byte[0];
int len = input.length();
if (len < 1)
return new byte[0];
///////////////////////////////////////////////////////////////////
// Step 1: Calculate the number of padding chars to append to this string.
// The number of padding chars to append is stored in the last char of the string.
int numPadChars = (int)input.charAt(len - 1) - (int)'0';
if (numPadChars < 0 || numPadChars > 10)
return null;
///////////////////////////////////////////////////////////////////
// Step 2: Create array to store the chars (not including the last char)
// and the padding chars
char[] base64Chars = new char[len - 1 + numPadChars];
////////////////////////////////////////////////////////
// Step 3: Copy in the chars. Transform the "-" to "+", and "*" to "/"
for (int iter = 0; iter < len - 1; iter++) {
char c = input.charAt(iter);
switch (c) {
case '-':
base64Chars[iter] = '+';
break;
case '_':
base64Chars[iter] = '/';
break;
default:
base64Chars[iter] = c;
break;
}
}
////////////////////////////////////////////////////////
// Step 4: Add padding chars
for (int iter = len - 1; iter < base64Chars.length; iter++) {
base64Chars[iter] = '=';
}
// Do the actual conversion
String assembledString = String.copyValueOf(base64Chars);
return Base64.decodeBase64(assembledString);
}
你能不能也發表您的編碼方法(Java)的?謝謝 – ori888