Android中数据的加密解密

栏目: Android · 发布时间: 5年前

内容简介:开发中我们经常会和服务器打交道:最终的目的就是和数据打交道,但是这往往出现一个问题就是数据的安全性问题,比如说我们把数据发送给服务器,服务器返回数据给我们,这其中牵涉到很重要的安全性问题:分3步来解决这个问题

开发中我们经常会和服务器打交道:最终的目的就是和数据打交道,但是这往往出现一个问题就是

数据的安全性问题,比如说我们把数据发送给服务器,服务器返回数据给我们,

这其中牵涉到很重要的安全性问题:分3步来解决这个问题

1:首先我们新建一个类用来加密和解密如下所示:

*
 * Created by acer-pc on 2018/6/22.
 */

public class EncryptUtil {

 private static final String ALGORITHM = "AES/ECB/PKCS5Padding";

 // 加密秘钥
 private static final String AES_KEY = "XXX(我们自己设置)";

 private static SecretKeySpec secretKeySpec;

 /**
 * 前台传输数据解密
 *
 * @param rawJson 原始JSON
 * @return 解密后的Map
 */
 public static <T extends BaseResult> T decrypt(String rawJson, Class<T> 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 = new String(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();
 }

 return result;
 }

 /**
 * 数据传输过程中需要加密设置
 * @param rawMap
 * @return
 */

 public static String encrypt(Map<String, String> 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();
 }

 return result;
 }

 private static SecretKeySpec getAesKey() {
 if (secretKeySpec != null) {
 return secretKeySpec;
 }
 try {
 secretKeySpec = new SecretKeySpec(AES_KEY.getBytes("UTF-8"), "AES");
 } catch (UnsupportedEncodingException e) {
 e.printStackTrace();
 }

 return secretKeySpec;
 }
}

2:其中的BaseResult如下(要解析的数据的根类,放数据的类要继承这个类):

public class BaseResult {

 private int result;
 private String message;

 public int getResult() {
 return result;
 }

 public void setResult(int result) {
 this.result = result;
 }

 public String getMessage() {
 return message;
 }

 public void setMessage(String message) {
 this.message = message;
 }
}

3:当我们在主类中(或者Fragment中)使用的时候如下:

//加载数据
public void initData() {
 //这里利用线程池使得线程在线程池中运行防止程序卡死
 APIConfig.getDataIntoView(new Runnable() {
 @Override
 public void run() {
 Map<String, String> map = new HashMap<>();
 map.put("token", RuntimeConfig.user.getToken());
 String paramJson = EncryptUtil.encrypt(map);
 String url = "http://这里是我们的目标网址";
 String rs = HttpUtil.GetDataFromNetByPost(url,
 new ParamsBuilder().addParam("paramJson", paramJson).getParams());
 // rs判空
 final DiaryDetailResult result = EncryptUtil.decrypt(rs, DiaryDetailResult.class);

 UIUtils.runOnUIThread(new Runnable() {
 @Override
 public void run() {
 //这里禁用
 if (result != null && result.getResult() == APIConfig.CODE_SUCCESS) {
 Diary diaryData = result.getData().getContent();
 //接下来对解析出的数据进行自己的操作
。。。。。。。。。。。。

 } else {
 // Toast弹出加载失败;
 }
 }
 });
 }
 });
}

3:大功告成!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

区块链:定义未来金融与经济新格局

区块链:定义未来金融与经济新格局

张健 / 机械工业出版社 / 2016-6-18 / 49.00

从构建价值互联网的角度看,区块链的出现意味着从0到1。正因如此,本书章节结构与常见的体例不同,从第0章开始。第0章从文字与货币的起源出发,通过论述人类信息传递和价值传输手段的进步,说明区块链技术诞生的必然性。第1章用深入浅出的语言讲解区块链的本质、运行原理、颠覆性潜力以及区块链技术的现状与未来;第2章宏观讲述了区块链技术带来的新产品和新机遇,包括数字货币、互联网金融、物联网,以及新一代的基础设施;......一起来看看 《区块链:定义未来金融与经济新格局》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具