Android中数据的加密解密
2023-03-31 11:07:17 时间
开发中我们经常会和服务器打交道:最终的目的就是和数据打交道,但是这往往出现一个问题就是,数据的安全性问题,比如说我们把数据发送给服务器,服务器返回数据给我们,这其中牵涉到很重要的安全性问题:分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:大功告成!
相关文章
- Flask中的请求上下文和应用上下文
- 腾讯入资东方金信数亿元,共建大数据生态体系
- 浅谈大数据开发工程师的两年工作经验总结
- 大数据应用案例:Dealer Tire通过大数据预测消费者何时需要轮胎
- 医疗大数据:如何实现商业化变现?
- 大数据分析建模及其应用建议
- 招聘行业大数据应用场景分析
- 大数据入门学习必读好书推荐,请收藏!
- 区块链与大数据的整合,未来必然趋势
- 如今的大数据究竟发展到了什么阶段
- InterSystems IRIS医疗版助力东华医为推动中国医疗行业数字化转型
- 大数据产业进入提质增效关键期
- 社交网络大数据的应用有多大的价值
- 大数据安全防护方法研究与建议
- InterSystems IRIS 数据平台通过AWS云应用商店认证
- 大数据在医疗行业中的5种应用
- 中国大地保险数据管理应用中心大数据应用平台案例分析
- 大数据在应急管理中的应用
- 大数据:产业链条将更为完备
- 2019年大数据发展将走向何方