溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中怎么實(shí)現(xiàn)數(shù)據(jù)的加密解密

發(fā)布時(shí)間:2021-06-26 16:33:44 來(lái)源:億速云 閱讀:187 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章為大家展示了Android中怎么實(shí)現(xiàn)數(shù)據(jù)的加密解密,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

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ù)傳輸過程中需要加密設(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彈出加載失敗;                     }                 }             });         }     }); }

上述內(nèi)容就是Android中怎么實(shí)現(xiàn)數(shù)據(jù)的加密解密,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI