URL安全的Base64編碼,解碼

Base64 能夠將二進制轉碼成可見字符方便進行http傳輸,可是base64轉碼時會生成「+」,「/」,「=」這些被URL進行轉碼的特殊字符,致使兩方面數據不一致。html

咱們能夠在發送前將「+」,「/」,「=」替換成URL不會轉碼的字符,接收到數據後,再將這些字符替換回去,再進行解碼。android

Base64加密轉換原理與代碼實現 :http://www.cnblogs.com/jxust-jiege666/p/8590116.html安全

PHP:app

 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自帶該功能:url

1 Base64.encodeToString(encrypted, Base64.URL_SAFE|Base64.NO_WRAP) 2 Base64.decode(data, Base64.URL_SAFE);
相關文章
相關標籤/搜索