架构师_程序员_码农网

Retrieve password
Register

QQ登录

Just one step to get started

Search
View:5466|Reply: 0
打印 上一主题 下一主题

URL secure Base64 encoding, decoding

[copy link]
跳转到指定楼层
owner of the building
发表于 2019-11-15 14:10:48| 看该作者回帖奖励|Browse in reverse order|Read mode
Base64 can transcode binary into visible characters for http transmission, but base64 transcoding generates "+", "/", "=" which are special characters that are transcoded by the URL. However, base64 transcoding will generate "+", "/", "=" which are special characters transcoded by the URL, resulting in inconsistency between the two sides of the data.
We can send before the "+", "/", "=" replaced by the URL will not be transcoded characters, after receiving the data, and then replace these characters back, and then decode.


PHP:

/**

* URL base64 decoding

* '-' -> '+'

* '_' -> '/'

* Remainder of string length %4, complement '='

* @param unknown $string

*/

public static function urlsafe_b64decode($string) {

$data = str_replace(array('-','_'),array('+','/'),$string);

$mod4 = strlen($data) % 4;

if ($mod4) {

$data . = substr('====', $mod4);

}

return base64_decode($data);

}



/**

* URL base64 encoding

* '+' -> '-'

* '/' -> '_'

* '=' -> ''

* @param unknown $string

*/

function urlsafe_b64encode($string) {

$data = base64_encode($string);

$data = str_replace(array('+','/','='),array('-','_',''),$data); return $data; $data

return $data;

}




OC.



#pragma - Convert the "-", "_" strings in saveBase64 encoding to "+", "/", with bitwise "=" for the remaining 4 times the length of the string.

+(NSData*)safeUrlBase64Decode:(NSString*)safeUrlbase64Str

{

// '-' -> '+'

// '_' -> '/'

// Less than 4x length, complement '='

NSMutableString * base64Str = [[NSMutableString alloc]initWithString:safeUrlbase64Str];

base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"-" withString:@"+"];

base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"_" withString:@"/"];

NSInteger mod4 = base64Str.length % 4; if(mod4 > 0); if(base64Str.length % 4)

if(mod4 > 0)

[base64Str appendString:[@"====" substringToIndex:(4-mod4)]];

NSLog(@"Base64 original text: %@", base64Str);

return [GTMBase64 decodeData:[base64Str dataUsingEncoding:NSUTF8StringEncoding]];;



}



#pragma - because Base64 encoding contains +,/,= which are unsafe URL strings, the character swap is performed

+(NSString*)safeUrlBase64Encode:(NSData*)data

{

// '+' -> '-'

// '/' -> '_'

// '=' -> ''

NSString * base64Str = [GTMBase64 stringByEncodingData:data];

NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];

safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+" withString:@"-& quot;];

safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/" withString:@"_& quot;];

safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"=" withString:@"& quot;];

NSLog(@"safeBase64 encoding:%@", safeBase64Str);

return safeBase64Str;

}



Java:

public static String safeUrlBase64Encode(byte[] data){

String encodeBase64 = new BASE64Encoder().encode(data);

String safeBase64Str = encodeBase64.replace('+', '-');

safeBase64Str = safeBase64Str.replace('/', '_');

safeBase64Str = safeBase64Str.replaceAll("=", "");

return safeBase64Str;

}


public static byte[] safeUrlBase64Decode(final String safeBase64Str){

String base64Str = safeBase64Str.replace('-', '+'); } public static byte[] safeUrlBase64Decode(final String safeBase64Str){

base64Str = base64Str.replace('_', '/');

int mod4 = base64Str.length()%4; if(mod4 > 0) { if(base64Str.replace('_', '/'))

if(mod4 > 0){

base64Str = base64Str + "====".substring(mod4);

}

return new BASE64Decoder().decodeBuffer(base64Str);

}



In case of Android, android.utils.Base64 comes with this function:

Base64.encodeToString(encrypted, Base64.URL_SAFE|Base64.NO_WRAP); }

Base64.decode(data, Base64.URL_SAFE); base64, url, decode(data, Base64.URL_SAFE); base64.




Previous: Played with an account lockout security policy
Next:C# connection sqlserver windows and sqlserver authentication two connection strings
NET, only published in the process of practice, encountered technical difficulties, do not mislead others.
You need to log in before you can post back Log in | Register

This version of the integral rules

DISCLAIMER:
All software, programming materials or articles published by Code Farmer are limited to be used for learning and research purposes only; the above contents shall not be used for commercial or illegal purposes, otherwise, all the consequences shall be borne by the users themselves. This site information from the network, copyright dispute has nothing to do with this site. You must completely remove the above content from your computer within 24 hours of downloading. If you like the program, please support the genuine software, buy registration and get better genuine service. If there is any infringement, please contact us by email to deal with it.

Mail To:help@itsvse.com

QQ| ( ICP备14021824号-2 )|Sitemap

GMT+8, 2024-9-18 20:08

Quick ReplyBack to topBack to list