原标题:Android中数据的加密解密
开发中我们经常会和服务器打交道:最终的目的就是和数据打交道,但是这往往出现一个问题就是
数据的安全性问题,比如说我们把数据发送给服务器,服务器返回数据给我们,
这其中牵涉到很重要的安全性问题:分3步来解决这个问题
1:首先我们新建一个类用来加密和解密如下所示:
*
* Created by acer-pc on 2018/6/22.
*/
publicclassEncryptUtil{
privatestaticfinalString ALGORITHM = "AES/ECB/PKCS5Padding";
// 加密秘钥
privatestaticfinalString AES_KEY = "XXX(我们自己设置)";
privatestaticSecretKeySpec secretKeySpec;
/**
* 前台传输数据解密
*
* @paramrawJson 原始JSON
* @return解密后的Map
*/
publicstatic T decrypt(String rawJson, Class tClass){
T result=null;
try{
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getAesKey());
byte[] paramBytes = cipher.doFinal(Base64.decode(rawJson.getBytes("UTF-8"), Base64.NO_WRAP));
String paramJson = newString(paramBytes);
result = GsonUtil.fromJson(paramJson, tClass);
} catch(NoSuchPaddingException e) {
e.printStackTrace();
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(InvalidKeyException e) {
e.printStackTrace();
} catch(BadPaddingException e) {
e.printStackTrace();
} catch(IllegalBlockSizeException e) {
e.printStackTrace();
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
returnresult;
}
/**
* 数据传输过程中需要加密设置
* @paramrawMap
* @return
*/
publicstaticString encrypt(Map rawMap){
String result = "";
try{
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, getAesKey());
String rawJson = GsonUtil.toJson(rawMap);
byte[] paramBytes = cipher.doFinal(rawJson.getBytes("UTF-8"));
result = Base64.encodeToString(paramBytes, Base64.NO_WRAP);
} catch(NoSuchPaddingException e) {
e.printStackTrace();
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(InvalidKeyException e) {
e.printStackTrace();
} catch(BadPaddingException e) {
e.printStackTrace();
} catch(IllegalBlockSizeException e) {
e.printStackTrace();
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
returnresult;
}
privatestaticSecretKeySpec getAesKey(){
if(secretKeySpec != null) {
returnsecretKeySpec;
}
try{
secretKeySpec = newSecretKeySpec(AES_KEY.getBytes("UTF-8"), "AES");
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
returnsecretKeySpec;
}
}
2:其中的BaseResult如下(要解析的数据的根类,放数据的类要继承这个类):
publicclassBaseResult{
privateintresult;
privateString message;
publicintgetResult(){
returnresult;
}
publicvoidsetResult(intresult){
this.result = result;
}
publicString getMessage(){
returnmessage;
}
publicvoidsetMessage(String message){
this.message = message;
}
}
3:当我们在主类中(或者Fragment中)使用的时候如下:
//加载数据
public voidinitData() {
//这里利用线程池使得线程在线程池中运行防止程序卡死
APIConfig.getDataIntoView(newRunnable() {
@Override
public voidrun() {
Map map = newHashMap<>();
map.put("token", RuntimeConfig.user.getToken());
StringparamJson = EncryptUtil.encrypt(map);
Stringurl = "http://这里是我们的目标网址";
Stringrs = HttpUtil.GetDataFromNetByPost(url,
newParamsBuilder().addParam("paramJson", paramJson).getParams());
// rs判空
finalDiaryDetailResult result = EncryptUtil.decrypt(rs, DiaryDetailResult.class);
UIUtils.runOnUIThread(newRunnable() {
@Override
public voidrun() {
//这里禁用
if(result != null&& result.getResult() == APIConfig.CODE_SUCCESS) {
Diary diaryData = result.getData().getContent();
//接下来对解析出的数据进行自己的操作
。。。。。。。。。。。。
} else{
// Toast弹出加载失败;
}
}
});
}
});
}
3:大功告成!
========分隔符=======返回搜狐,查看更多
责任编辑: