前言
由于接口请求需要将请求内容加密再传输,其中一部就是讲内容进行base64编码,然后请求回来的数据也进行同样的base64解码
方式:使用系统的dart:convert
库进行编码解码
// base64库
import 'dart:convert' as convert;
// 文件相关
import 'dart:io';
class Util {
/*
* Base64加密
*/
static String base64Encode(String data){
var content = convert.utf8.encode(data);
var digest = convert.base64Encode(content);
return digest;
}
/*
* Base64解密
*/
static String base64Decode(String data){
List<int> bytes = convert.base64Decode(data);
// 网上找的很多都是String.fromCharCodes,这个中文会乱码
//String txt1 = String.fromCharCodes(bytes);
String result = convert.utf8.decode(bytes);
return result;
}
/*
* 通过图片路径将图片转换成Base64字符串
*/
static Future image2Base64(String path) async {
File file = new File(path);
List<int> imageBytes = await file.readAsBytes();
return convert.base64Encode(imageBytes);
}
/*
* 将图片文件转换成Base64字符串
*/
static Future imageFile2Base64(File file) async {
List<int> imageBytes = await file.readAsBytes();
return convert.base64Encode(imageBytes);
}
/*
* 将Base64字符串的图片转换成图片
*/
static Image Future base642Image(String base64Txt) async {
String decodeTxt = convert.base64.decode(base64Txt);
return Image.memory(decodeTxt,
width:100,fit: BoxFit.fitWidth,
gaplessPlayback:true, //防止重绘
);
}
}
testBase64() {
String oriTxt = '[{"name":"小明","mail":"xiaoming@gamil.com","age":89,"money":9999.99}]';
String encodeTxt = Util.base64Encode(oriTxt);
String decodeTxt = Util.base64Decode(encodeTxt);
print(oriTxt);
print(encodeTxt);
print(decodeTxt);
}
使用String.fromCharCodes
测试结果:
[{"name":"小明","mail":"xiaoming@gamil.com","age":89,"money":9999.99}]
W3sibmFtZSI6IuWwj+aYjiIsIm1haWwiOiJ4aWFvbWluZ0BnYW1pbC5jb20iLCJhZ2UiOjg5LCJtb25leSI6OTk5OS45OX1d
[{"name":"��","mail":"xiaoming@gamil.com","age":89,"money":9999.99}]
使用convert.utf8.decode
测试结果:
[{"name":"小明","mail":"xiaoming@gamil.com","age":89,"money":9999.99}]
W3sibmFtZSI6IuWwj+aYjiIsIm1haWwiOiJ4aWFvbWluZ0BnYW1pbC5jb20iLCJhZ2UiOjg5LCJtb25leSI6OTk5OS45OX1d
[{"name":"小明","mail":"xiaoming@gamil.com","age":89,"money":9999.99}]
网友评论