android或java用DES加密解密檔案

來源:互聯網
上載者:User

    最近做項目,需要加密android用戶端的一些sql語句,我當時使用的是DES加密的,結果加密出現了

javax.crypto.BadPaddingException: Given final block not properly padded

這樣的錯誤,要不就是出現亂碼的問題,很糾結!當時查了一些資料,就有可能是密鑰的問題或者編碼的問題,檢查了發現,密鑰正確的,就是在建立Key 的時候,得到的byte[]數組有一些處理的,具體完整的代碼如下:

 

package com.spring.sky.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.security.Key;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.spec.SecretKeySpec;import org.apache.http.entity.InputStreamEntity;/*** * DES檔案加密&解密  <br> * 可以實現android和window的檔案互連  * @author spring.sky * Email:vipa1888@163.com * QQ:840950105 * */public class FileDES {/**加密解密的key*/private Key mKey;/**解密的密碼*/private Cipher mDecryptCipher;/**加密的密碼*/private Cipher mEncryptCipher;public FileDES(String key) throws Exception{initKey(key);initCipher();}/** * 建立一個加密解密的key * @param keyRule  */public void initKey(String keyRule) {byte[] keyByte = keyRule.getBytes();// 建立一個空的八位元組,預設情況下為0byte[] byteTemp = new byte[8];// 將使用者指定的規則轉換成八位元組for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {byteTemp[i] = keyByte[i];}mKey = new SecretKeySpec(byteTemp, "DES");}/*** * 初始化載入密碼 * @throws Exception */private void initCipher() throws Exception{mEncryptCipher = Cipher.getInstance("DES");mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);mDecryptCipher = Cipher.getInstance("DES");mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);}/** * 加密檔案 * @param in * @param savePath 加密後儲存的位置 */public void doEncryptFile(InputStream in,String savePath){if(in==null){System.out.println("inputstream is null");return;}try {CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);OutputStream os = new FileOutputStream(savePath);byte[] bytes = new byte[1024];int len = -1;while((len=cin.read(bytes))>0){os.write(bytes, 0, len);os.flush();}os.close();cin.close();in.close();System.out.println("加密成功");} catch (Exception e) {System.out.println("加密失敗");e.printStackTrace();}}/** * 加密檔案 * @param filePath 需要加密的檔案路徑 * @param savePath 加密後儲存的位置 * @throws FileNotFoundException  */public void doEncryptFile(String filePath,String savePath) throws FileNotFoundException{doEncryptFile(new FileInputStream(filePath), savePath);}/** * 解密檔案 * @param in */public void doDecryptFile(InputStream in){if(in==null){System.out.println("inputstream is null");return;}try {CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);BufferedReader reader = new BufferedReader(new InputStreamReader(cin)) ;String line = null;while((line=reader.readLine())!=null){System.out.println(line);}reader.close();cin.close();in.close();System.out.println("解密成功");} catch (Exception e) {System.out.println("解密失敗");e.printStackTrace();}}/** * 解密檔案 * @param filePath  檔案路徑 * @throws Exception */public void doDecryptFile(String filePath) throws Exception{doDecryptFile(new FileInputStream(filePath));}public static void main(String[] args)throws Exception {FileDES fileDES = new FileDES("spring.sky");fileDES.doEncryptFile("d:/a.txt", "d:/b");  //加密fileDES.doDecryptFile("d:/b"); //解密}}

 

 

上面的代碼,我分別在android 1.6和java平台上面測試通過了,沒任何問題的,只是根據不同的需求做一下封裝,希望對大家有幫忙,讓大家少走彎路!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.