不要让昨天 占据你的今天 夏午晴天

夏午晴天

URL安全的Base64编码,解码

Base64 可以将二进制转码成可见字符方便进行http传输,但是base64转码时会生成“+”,“/”,“=”这些被URL进行转码的特殊字符,导致两方面数据不一致。

我们可以在发送前将“+”,“/”,“=”替换成URL不会转码的字符,接收到数据后,再将这些字符替换回去,再进行解码。

Base64加密转换原理与代码实现 :http://www.cnblogs.com/jxust-jiege666/p/8590116.html

PHP:

 1     /**
 2 
 3      * URL base64解码
 4 
 5      * '-' -> '+'
 6 
 7      * '_' -> '/'
 8 
 9      * 字符串长度%4的余数,补'='
10 
11      * @param unknown $string
12 
13      */
14 
15     public static function urlsafe_b64decode($string) {
16 
17         $data = str_replace(array('-','_'),array('+','/'),$string);
18 
19         $mod4 = strlen($data) % 4;
20 
21         if ($mod4) {
22 
23             $data .= substr('====', $mod4);
24 
25         }
26 
27         return base64_decode($data);
28 
29     }
30 
31     
32 
33     /**
34 
35      * URL base64编码
36 
37      * '+' -> '-'
38 
39      * '/' -> '_'
40 
41      * '=' -> ''
42 
43      * @param unknown $string
44 
45      */
46 
47     function urlsafe_b64encode($string) {
48 
49         $data = base64_encode($string);
50 
51         $data = str_replace(array('+','/','='),array('-','_',''),$data);
52 
53         return $data;
54 
55     }

 

OC:

 1 #pragma - 将saveBase64编码中的"-","_"字符串转换成"+","/",字符串长度余4倍的位补"="
 2 
 3 +(NSData*)safeUrlBase64Decode:(NSString*)safeUrlbase64Str
 4 
 5 {
 6 
 7     // '-' -> '+'
 8 
 9     // '_' -> '/'
10 
11     // 不足4倍长度,补'='
12 
13     NSMutableString * base64Str = [[NSMutableString alloc]initWithString:safeUrlbase64Str];
14 
15     base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
16 
17     base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
18 
19     NSInteger mod4 = base64Str.length % 4;
20 
21     if(mod4 > 0)
22 
23         [base64Str appendString:[@"====" substringToIndex:(4-mod4)]];
24 
25     NSLog(@"Base64原文:%@", base64Str);
26 
27     return [GTMBase64 decodeData:[base64Str dataUsingEncoding:NSUTF8StringEncoding]];
28 
29     
30 
31 }
32 
33  
34 
35 #pragma - 因为Base64编码中包含有+,/,=这些不安全的URL字符串,所以要进行换字符
36 
37 +(NSString*)safeUrlBase64Encode:(NSData*)data
38 
39 {
40 
41     // '+' -> '-'
42 
43     // '/' -> '_'
44 
45     // '=' -> ''
46 
47     NSString * base64Str = [GTMBase64 stringByEncodingData:data];
48 
49     NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];
50 
51     safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
52 
53     safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
54 
55     safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"=" withString:@""];
56 
57     NSLog(@"safeBase64编码:%@", safeBase64Str);
58 
59     return safeBase64Str;
60 
61 }

 

Java:

 1 public static String safeUrlBase64Encode(byte[] data){
 2 
 3 String encodeBase64 = new BASE64Encoder().encode(data);
 4 
 5 String safeBase64Str = encodeBase64.replace('+', '-');
 6 
 7 safeBase64Str = safeBase64Str.replace('/', '_');
 8 
 9 safeBase64Str = safeBase64Str.replaceAll("=", "");
10 
11 return safeBase64Str;
12 
13 }
14 
15  
16 
17 public static byte[] safeUrlBase64Decode(final String safeBase64Str){
18 
19 String base64Str = safeBase64Str.replace('-', '+');
20 
21 base64Str = base64Str.replace('_', '/');
22 
23 int mod4 = base64Str.length()%4;
24 
25 if(mod4 > 0){
26 
27 base64Str = base64Str + "====".substring(mod4);
28 
29 }
30 
31 return new BASE64Decoder().decodeBuffer(base64Str);
32 
33 }
34 
35  

 

如果是Android的话,android.utils.Base64自带该功能:

1 Base64.encodeToString(encrypted, Base64.URL_SAFE|Base64.NO_WRAP)
2 Base64.decode(data, Base64.URL_SAFE);

 



posted on 2018-04-07 14:25  夏晴天  阅读(14714)  评论(0编辑  收藏  举报

导航

Live2D