AES加密解密在JAVA和ANDROID下互連

來源:互聯網
上載者:User

標籤:調試   seh   .com   密碼   tostring   statistic   其他   cat   font   

<span style="font-family: Arial, Helvetica, sans-serif;">昨天外包安卓的那個人說AES的加解密結果不一樣。於是百度搜尋發現還真是!</span>

貼上AES加密核心:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

CBC是工作模式,AES一共同擁有電子password本模式(ECB)、加密分組連結模式(CBC)、加密反饋模式(CFB)和輸出反饋模式(OFB)四種模式。PKCS5Padding是填充模式,還有其他的填充模式:然後,cipher.init()一共同擁有三個參數:Cipher.ENCRYPT_MODE, key, zeroIv,zeroIv就是初始化向量,一個8為字元數組。工作模式、填充模式、初始化向量這三種因素一個都不能少。否則,假設你不指定的話。那麼就要程式就要調用預設實現。

知道原因就好辦,各種調試測試之後完畢AES在JAVA和安桌互連。

現貼上核心代碼:

/** 填充模式 */private static final String transformation = "AES/CBC/PKCS5Padding";/** * 加密 *  * @param content 須要加密的內容 * @param password 加密密碼 * @return */public static String encrypt(String content, String password) {try {IvParameterSpec zeroIv = new IvParameterSpec(password.getBytes());SecretKeySpec key1 = new SecretKeySpec(password.getBytes(),"AES");Cipher cipher = Cipher.getInstance(transformation);cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv);byte[] encryptedData = cipher.doFinal(content.getBytes());        String encryptResultStr = parseByte2HexStr(encryptedData);        return encryptResultStr;} catch (Exception e) {e.printStackTrace();}return null;}/** * 解密 *  * @param content 待解密內容 * @param password 解密密鑰 * @return */public static String decrypt(String content, String password) {try {byte[] decryptFrom = parseHexStr2Byte(content);IvParameterSpec zeroIv = new IvParameterSpec(password.getBytes());SecretKeySpec key1 = new SecretKeySpec(password.getBytes(),"AES");Cipher cipher = Cipher.getInstance(transformation);cipher.init(Cipher.DECRYPT_MODE, key1, zeroIv);byte decryptedData[] = cipher.doFinal(decryptFrom); return new String(decryptedData);} catch (Exception e) {e.printStackTrace();}return null;}/**將二進位轉換成16進位  * @param buf  * @return  */  public static String parseByte2HexStr(byte buf[]) {          StringBuffer sb = new StringBuffer();          for (int i = 0; i < buf.length; i++) {                  String hex = Integer.toHexString(buf[i] & 0xFF);                  if (hex.length() == 1) {                          hex = '0' + hex;                  }                  sb.append(hex.toUpperCase());          }          return sb.toString();  }  /**將16進位轉換為二進位  * @param hexStr  * @return  */  public static byte[] parseHexStr2Byte(String hexStr) {          if (hexStr.length() < 1)                  return null;          byte[] result = new byte[hexStr.length()/2];          for (int i = 0;i< hexStr.length()/2; i++) {                  int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);                  int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);                  result[i] = (byte) (high * 16 + low);          }          return result;  }  
最後我想說一下,花了積分最後還是解決不了互連。我僅僅想說不帶這樣騙積分的。



AES加密解密在JAVA和ANDROID下互連

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.