Android中數(shù)據(jù)的加密解密
開(kāi)發(fā)中我們經(jīng)常會(huì)和服務(wù)器打交道:最終的目的就是和數(shù)據(jù)打交道,但是這往往出現(xiàn)一個(gè)問(wèn)題就是,數(shù)據(jù)的安全性問(wèn)題,比如說(shuō)我們把數(shù)據(jù)發(fā)送給服務(wù)器,服務(wù)器返回?cái)?shù)據(jù)給我們,這其中牽涉到很重要的安全性問(wèn)題:分3步來(lái)解決這個(gè)問(wèn)題。
1:首先我們新建一個(gè)類用來(lái)加密和解密如下所示:
- *
- * 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;
- /**
- * 前臺(tái)傳輸數(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ù)傳輸過(guò)程中需要加密設(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ù)的類要繼承這個(gè)類):
- 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)我們?cè)谥黝愔?或者Fragment中)使用的時(shí)候如下:
- //加載數(shù)據(jù)
- public void initData() {
- //這里利用線程池使得線程在線程池中運(yùn)行防止程序卡死
- 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://這里是我們的目標(biāo)網(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();
- //接下來(lái)對(duì)解析出的數(shù)據(jù)進(jìn)行自己的操作
- 。。。。。。。。。。。。
- } else {
- // Toast彈出加載失敗;
- }
- }
- });
- }
- });
- }
3:大功告成!