今天遇到一个Base64
解码的问题,因此在这里记录一下。
一. 问题
问题是这样,服务器下发的Base64
加密请求回复内容,客户端接到回复内容,调用系统解码,安卓可以解出来,iOS
解不出来。回复内容如下:
NSString *tmpStr = @"W3siYmFubmVyTGlzdCI6W3siaW1nVXJsIjoiaHR0cDovL3Nob3B4eC53ZWkyMC5jbi9Vc2VyRG9jdW1lbnQvaG1qczg4OC9QaWN0dXJlLzIwMTc4MTRfMTUxMjA4LmpwZyIsInR5cGUiOiJqYXZhc2NyaXB0OjsiLCJpZCI6IiJ9LHsiaW1nVXJsIjoiaHR0cDovL3Nob3B4eC53ZWkyMC5jbi9Vc2VyRG9jdW1lbnQvaG1qczg4OC9QaWN0dXJlLzIwMTc4MTRfMTUxMTQ2LmpwZyIsInR5cGUiOiJqYXZhc2NyaXB0OjsiLCJpZCI6IiJ9LHsiaW1nVXJsIjoiaHR0cDovL3Nob3B4eC53ZWkyMC5jbi9Vc2VyRG9jdW1lbnQvaG1qczg4OC9QaWN0dXJlLzIwMTc4MTRfMTUxMjI1LmpwZyIsInR5cGUiOiJqYXZhc2NyaXB0OjsiLCJpZCI6IiJ9XX0seyJkYXRhcyI6W3sic2hvcEltYWdlVXJsIjoiaHR0cDovL3Nob3Aud2VpMjAuY24vVXNlckRvY3VtZW50L2htanM4ODgvUGljdHVyZS8yMDE3ODE2XzE0NDIzMi5qcGciLCJzaG9wTGluayI6Imh0dHA6Ly93eDQud2VpMjAuY29tL2dvdXd1L3dpc2gzZC9ob21lLnNodG1sP2lkXHUwMDNkMzU2In0seyJzaG9wSW1hZ2VVcmwiOiJodHRwOi8vc2hvcC53ZWkyMC5jbi9Vc2VyRG9jdW1lbnQvaG1qczg4OC9QaWN0dXJlLzIwMTc4MTZfMTE1OTM3LmpwZyIsInNob3BMaW5rIjoiaHR0cDovL3d4NC53ZWkyMC5jb20vZ291d3Uvd2lzaDNkL2xpc3QvbGlzdD9pZFx1MDAzZDM1Nlx1MDAyNm5pZFx1MDAzZMnMs8e31s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fSx7ImRhdGFzIjpbeyJzaG9wSW1hZ2VVcmwiOiJodHRwOi8vMTM5LjIyNC41Ny4xMDU6ODA4MC9waWN0dXJlLy4uL3BpY3R1cmUvdGVtcGxhdGUvMl8xLzIwMTcwNzE4L2M2ZDY4N2U1LTdjMjMtNDBkYi1iOWVkLWJjNjY5N2Q0N2JhNC5wbmciLCJzaG9wTGluayI6IiJ9XX1d"; NSData *base64Data = [tmpStr dataUsingEncoding:NSASCIIStringEncoding]; NSData *data = [[NSData alloc] initWithBase64EncodedData:base64Data options:0]; NSString *outputString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"outputString = %@", outputString);
-
解出内容
outputString = [{"bannerList":[{"imgUrl":"http://shopxx.wei20.cn/UserDocument/hmjs888/Picture/2017814_151208.jpg","type":"javascript:;","id":""},{"imgUrl":"http://shopxx.wei20.cn/UserDocument/hmjs888/Picture/2017814_151146.jpg","type":"javascript:;","id":""},{"imgUrl":"http://shopxx.wei20.cn/UserDocument/hmjs888/Picture/2017814_151225.jpg","type":"javascript:;","id":""}]},{"datas":[{"shopImageUrl":"http://shop.wei20.cn/UserDocument/hmjs888/Picture/2017816_144232.jpg","shopLink":"http://wx4.wei20.com/gouwu/wish3d/home.shtml?id\u003d356"},{"shopImageUrl":"http://shop.wei20.cn/UserDocument/hmjs888/Picture/2017816_115937.jpg","shopLink":"http://wx4.wei20.com/gouwu/wish3d/list/list?id\u003d356\u0026nid\u003dÉ̳ǷÖÀà´úÂë"},{"shopImageUrl":"http://shop.wei20.cn/UserDocument/hmjs888/Picture/2017816_115947.jpg","shopLink":""},{"shopImageUrl":"http://shop.wei20.cn/UserDocument/hmjs888/Picture/2017816_115955.jpg","shopLink":""}]},{"datas":[{"shopImageUrl":"http://139.224.57.105:8080/picture/../picture/template/2_1/20170718/695e459c-7c01-459c-b153-0c66638b36e6.jpg","shopLink":"smore/smore?tid\u003d\u0026sid\u003d17864\u0026id\u003d618\u0026nid\u003d4100"}]},{"datas":[{"shopImageUrl":"http://139.224.57.105:8080/picture/../picture/template/2_1/20170718/b74f1bf9-00e5-40bd-9417-1d868f231973.jpg","shopLink":""}]},{"datas":[{"shopImageUrl":"http://139.224.57.105:8080/picture/../picture/template/2_1/20170718/4ee9287b-f568-4de5-875d-ee126b7023ec.jpg","shopLink":""}]},{"datas":[{"shopImageUrl":"http://139.224.57.105:8080/picture/../picture/template/2_1/20170718/c6d687e5-7c23-40db-b9ed-bc6697d47ba4.png","shopLink":""}]}]
三. 原因
可能原因就是服务器先进行ASCII
转码,然后在进行BASE64
编码。因此客户端也要进行相应的ASCII
解码,再进行BASE64
解码。
网友评论