Is there a Java equivalent for C#'s HttpServerUtility.UrlTokenDecode?

前端 未结 1 790
一个人的身影
一个人的身影 2021-01-20 05:51

How do I decode in Java a string that was encoded in C# using HttpServerUtility.UrlTokenEncode?

相关标签:
1条回答
  • 2021-01-20 06:21

    I tried using org.apache.commons.codec.binary.Base64 (The ctor accepts a parameter stating whether the encoding/decoding is url-safe) but turns out it is not implemented the same as UrlTokenEncode/Decode.

    I ended up migrating the C# implementation to 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);
    }   
    
    0 讨论(0)
提交回复
热议问题