Android中數(shù)據(jù)的加密解密
作者:佚名 
  開發(fā)中我們經(jīng)常會和服務(wù)器打交道:最終的目的就是和數(shù)據(jù)打交道,但是這往往出現(xiàn)一個問題就是,數(shù)據(jù)的安全性問題,比如說我們把數(shù)據(jù)發(fā)送給服務(wù)器,服務(wù)器返回數(shù)據(jù)給我們,這其中牽涉到很重要的安全性問題:分3步來解決這個問題。
 開發(fā)中我們經(jīng)常會和服務(wù)器打交道:最終的目的就是和數(shù)據(jù)打交道,但是這往往出現(xiàn)一個問題就是,數(shù)據(jù)的安全性問題,比如說我們把數(shù)據(jù)發(fā)送給服務(wù)器,服務(wù)器返回數(shù)據(jù)給我們,這其中牽涉到很重要的安全性問題:分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(我們自己設(shè)置)";
 - private static SecretKeySpec secretKeySpec;
 - /**
 - * 前臺傳輸數(shù)據(jù)解密
 - *
 - * @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;
 - }
 - /**
 - * 數(shù)據(jù)傳輸過程中需要加密設(shè)置
 - * @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如下(要解析的數(shù)據(jù)的根類,放數(shù)據(jù)的類要繼承這個類):
- 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:當(dāng)我們在主類中(或者Fragment中)使用的時候如下:
- //加載數(shù)據(jù)
 - 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://這里是我們的目標網(wǎng)址";
 - 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();
 - //接下來對解析出的數(shù)據(jù)進行自己的操作
 - 。。。。。。。。。。。。
 - } else {
 - // Toast彈出加載失敗;
 - }
 - }
 - });
 - }
 - });
 - }
 
3:大功告成!
責(zé)任編輯:未麗燕 
                    來源:
                    Android開發(fā)中文站
 















 
 
 







 
 
 
 