Android中数据的加密解密

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

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

Android中数据的加密解密

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

  1.  * Created by acer-pc on 2018/6/22. 
  2.  */ 
  3.  
  4. public class EncryptUtil { 
  5.  
  6.     private static final String ALGORITHM = "AES/ECB/PKCS5Padding"
  7.  
  8.     // 加密秘钥 
  9.     private static final String AES_KEY = "XXX(我们自己设置)"
  10.  
  11.     private static SecretKeySpec secretKeySpec; 
  12.  
  13.     /** 
  14.      * 前台传输数据解密 
  15.      * 
  16.      * @param rawJson 原始JSON 
  17.      * @return 解密后的Map 
  18.      */ 
  19.     public static <T extends BaseResult> T decrypt(String rawJson, Class<T> tClass) { 
  20.  
  21.         T result=null
  22.  
  23.         try { 
  24.             Cipher cipher = Cipher.getInstance(ALGORITHM); 
  25.             cipher.init(Cipher.DECRYPT_MODE, getAesKey()); 
  26.             byte[] paramBytes = cipher.doFinal(Base64.decode(rawJson.getBytes("UTF-8"), Base64.NO_WRAP)); 
  27.             String paramJson = new String(paramBytes); 
  28.             result = GsonUtil.fromJson(paramJson, tClass); 
  29.         } catch (NoSuchPaddingException e) { 
  30.             e.printStackTrace(); 
  31.         } catch (NoSuchAlgorithmException e) { 
  32.             e.printStackTrace(); 
  33.         } catch (InvalidKeyException e) { 
  34.             e.printStackTrace(); 
  35.         } catch (BadPaddingException e) { 
  36.             e.printStackTrace(); 
  37.         } catch (IllegalBlockSizeException e) { 
  38.             e.printStackTrace(); 
  39.         } catch (UnsupportedEncodingException e) { 
  40.             e.printStackTrace(); 
  41.         } 
  42.  
  43.         return result; 
  44.     } 
  45.  
  46.     /** 
  47.      * 数据传输过程中需要加密设置 
  48.      * @param rawMap 
  49.      * @return 
  50.      */ 
  51.  
  52.     public static String encrypt(Map<String, String> rawMap) { 
  53.         String result = ""
  54.  
  55.         try { 
  56.             Cipher cipher = Cipher.getInstance(ALGORITHM); 
  57.             cipher.init(Cipher.ENCRYPT_MODE, getAesKey()); 
  58.  
  59.             String rawJson = GsonUtil.toJson(rawMap); 
  60.             byte[] paramBytes = cipher.doFinal(rawJson.getBytes("UTF-8")); 
  61.             result = Base64.encodeToString(paramBytes, Base64.NO_WRAP); 
  62.         } catch (NoSuchPaddingException e) { 
  63.             e.printStackTrace(); 
  64.         } catch (NoSuchAlgorithmException e) { 
  65.             e.printStackTrace(); 
  66.         } catch (InvalidKeyException e) { 
  67.             e.printStackTrace(); 
  68.         } catch (BadPaddingException e) { 
  69.             e.printStackTrace(); 
  70.         } catch (IllegalBlockSizeException e) { 
  71.             e.printStackTrace(); 
  72.         } catch (UnsupportedEncodingException e) { 
  73.             e.printStackTrace(); 
  74.         } 
  75.  
  76.         return result; 
  77.     } 
  78.  
  79.     private static SecretKeySpec getAesKey() { 
  80.         if (secretKeySpec != null) { 
  81.             return secretKeySpec; 
  82.         } 
  83.         try { 
  84.             secretKeySpec = new SecretKeySpec(AES_KEY.getBytes("UTF-8"), "AES"); 
  85.         } catch (UnsupportedEncodingException e) { 
  86.             e.printStackTrace(); 
  87.         } 
  88.  
  89.         return secretKeySpec; 
  90.     } 

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

  1. public class BaseResult {  
  2.     private int result; 
  3.     private String message;  
  4.     public int getResult() { 
  5.         return result; 
  6.     } 
  7.  
  8.     public void setResult(int result) { 
  9.         this.result = result; 
  10.     } 
  11.  
  12.     public String getMessage() { 
  13.         return message; 
  14.     } 
  15.  
  16.     public void setMessage(String message) { 
  17.         this.message = message; 
  18.     } 

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

  1. //加载数据 
  2. public void initData() { 
  3.     //这里利用线程池使得线程在线程池中运行防止程序卡死 
  4.     APIConfig.getDataIntoView(new Runnable() { 
  5.         @Override 
  6.         public void run() { 
  7.             Map<String, String> map = new HashMap<>(); 
  8.             map.put("token", RuntimeConfig.user.getToken()); 
  9.             String paramJson = EncryptUtil.encrypt(map); 
  10.             String url = "http://这里是我们的目标网址"
  11.             String rs = HttpUtil.GetDataFromNetByPost(url, 
  12.                     new ParamsBuilder().addParam("paramJson", paramJson).getParams()); 
  13.             // rs判空 
  14.             final DiaryDetailResult result = EncryptUtil.decrypt(rs, DiaryDetailResult.class); 
  15.  
  16.             UIUtils.runOnUIThread(new Runnable() { 
  17.                 @Override 
  18.                 public void run() { 
  19.                     //这里禁用 
  20.                     if (result != null && result.getResult() == APIConfig.CODE_SUCCESS) { 
  21.                         Diary diaryData = result.getData().getContent(); 
  22.                         //接下来对解析出的数据进行自己的操作 
  23.                         。。。。。。。。。。。。 
  24.  
  25.                     } else { 
  26.                       // Toast弹出加载失败; 
  27.                     } 
  28.                 } 
  29.             }); 
  30.         } 
  31.     }); 

3:大功告成!

责任编辑:未丽燕 来源: Android开发中文站
相关推荐

2024-03-01 09:58:44

2023-06-26 00:30:51

2023-10-16 08:22:49

2011-08-01 14:14:36

加密技术

2021-01-07 14:17:31

Springboot数据安全加密

2023-03-06 08:49:02

加密和解密SpringBoot

2015-03-26 14:19:53

GPG加密解密

2020-09-24 10:50:53

加密解密语言hmac

2024-01-01 14:19:11

2016-07-12 09:40:30

恶意程序TLS加密恶意流量

2021-04-15 09:02:33

Python加密解密

2021-05-08 05:56:15

加密OpenSSL密钥

2014-01-03 09:13:39

JavaScriptthis

2016-06-21 10:40:54

云计算AWS

2018-08-28 10:40:08

Windows 10EFS加密文件

2011-04-12 14:58:23

加密解密类

2018-04-23 13:10:01

2011-06-28 14:30:48

Asp.net

2015-03-24 20:53:10

2011-03-03 13:13:51

DelphiSQLite加密
点赞
收藏

51CTO技术栈公众号