Android中數據的加密解密
作者:佚名
開發中我們經常會和服務器打交道:最終的目的就是和數據打交道,但是這往往出現一個問題就是,數據的安全性問題,比如說我們把數據發送給服務器,服務器返回數據給我們,這其中牽涉到很重要的安全性問題:分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:大功告成!
責任編輯:未麗燕
來源:
Android開發中文站