iOS Base64URL編碼 等同于java中的Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING

一. iOS Base64URL編碼:
java中,在進(jìn)行base64編碼時(shí)會(huì)看到類似如下代碼:

 String result = Base64.encodeToString(bytedata, Base64.URL_SAFE | Base64.NO_WRAP |  Base64.NO_PADDING);

其中參數(shù)的含義是:

 URL_SAFE:安全的URL編碼,base64轉(zhuǎn)碼過程中會(huì)生成“+”,“/”,“=”這些會(huì)被URL進(jìn)行轉(zhuǎn)碼的特殊字符,導(dǎo)致前后臺(tái)數(shù)據(jù)不同,所以需要將這些字符替代為URL不會(huì)進(jìn)行轉(zhuǎn)碼的字符,保證數(shù)據(jù)同步;
        "-" -> "+"
        "_" -> "/"
    NO_WRAP:不換行
    NO_PADDING:"="號(hào)補(bǔ)齊去除,base64會(huì)對(duì)字符進(jìn)行串長(zhǎng)度余4的"="的補(bǔ)位,需去除"="。

這些功能都是android.utils.Base64中自帶的
在iOS中沒有,所以我們只能自己動(dòng)手了~

Swift代碼:

//MARK: Data轉(zhuǎn)Base64URL加密
internal class func convertByte(bytedata:Data?) -> String {
    guard let bytedata = bytedata else {
        return ""
    }
    // data數(shù)據(jù)進(jìn)行base64加密
    guard let result = bytedata.base64EncodedString() else {
        return ""
    }
    // %替換為_
    let result4 = result.replacingOccurrences(of: "%", with: "_")
    // =替換為空
    let result1 = result4.replacingOccurrences(of: "=", with: "")
    // +替換為—
    let result2 = result1.replacingOccurrences(of: "+", with: "-")
    // /替換為_
    let result3 = result2.replacingOccurrences(of: "/", with: "_")
    return result3
}
//MARK: 后臺(tái)反回的base64字符串轉(zhuǎn)為data
internal class func convertBase64URL(base64:String?) -> Data? {
    guard let base64 = base64 else {
        return nil
    }
    // -替換為+
    let base64Str = base64.replacingOccurrences(of: "-", with: "+")
    // _替換為/
    let base64Str2 = base64Str.replacingOccurrences(of: "_", with: "/")
    var base64Str3 = ""
    // =號(hào)補(bǔ)全
    let mod4 = base64Str.count % 4
    if mod4 > 0 {
        let appStr = ("====" as NSString).substring(to: (4 - mod4))
        base64Str3 = base64Str2 + appStr
    }
    // base64Str轉(zhuǎn)data并進(jìn)行Base64Decoding
    return Data(base64Encoded: base64Str3, options: Data.Base64DecodingOptions.init(rawValue: 0))
}

OC:

//MARK: 將saveBase64編碼中的"-","_"字符串轉(zhuǎn)換成"+","/",字符串長(zhǎng)度余4倍的位補(bǔ)"="
+(NSData*)safeUrlBase64Decode:(NSString*)safeUrlbase64Str
{
            // '-' -> '+'
            // '_' -> '/'
            // 不足4倍長(zhǎng)度,補(bǔ)'='
            NSMutableString * base64Str = [[NSMutableString alloc]initWithString:safeUrlbase64Str];
            base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
            base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
            NSInteger mod4 = base64Str.length % 4;
            if(mod4 > 0)
                    [base64Str appendString:[@"====" substringToIndex:(4-mod4)]];
            NSLog(@"Base64原文:%@", base64Str);
            return [GTMBase64 decodeData:[base64Str dataUsingEncoding:NSUTF8StringEncoding]];
            
}

//MARK: 因?yàn)锽ase64編碼中包含有+,/,=這些不安全的URL字符串,所以要進(jìn)行換字符
+(NSString*)safeUrlBase64Encode:(NSData*)data
{
            // '+' -> '-'
            // '/' -> '_'
            // '=' -> ''
            NSString * base64Str = [GTMBase64 stringByEncodingData:data];
            NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];
            safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
            safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
            safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"=" withString:@""];
            NSLog(@"safeBase64編碼:%@", safeBase64Str);
            return safeBase64Str;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容