Android或Java用DES加密解密文件


最近做项目,需要加密Android客户端的一些sql语句,我当时使用的是DES加密的,结果加密出现了
  1. javax.crypto.BadPaddingException: Given final block not properly padded  

这样的错误,要不就是出现乱码的问题,很纠结!当时查了一些资料,就有可能是密钥的问题或者编码的问题,检查了发现,密钥正确的,就是在创建Key 的时候,得到的byte[]数组有一些处理的,具体完整的代码如下:

  1. package com.spring.sky.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.InputStreamReader;  
  11. import java.io.OutputStream;  
  12. import java.security.Key;  
  13.   
  14. import javax.crypto.Cipher;  
  15. import javax.crypto.CipherInputStream;  
  16. import javax.crypto.spec.SecretKeySpec;  
  17.   
  18. import org.apache.http.entity.InputStreamEntity;  
  19.   
  20. /*** 
  21.  * DES文件加密&解密  <br> 
  22.  * 可以实现android和window的文件互通  
  23.  * @author spring.sky 
  24.  * Email:vipa1888@163.com 
  25.  * QQ:840950105 
  26.  * 
  27.  */  
  28. public class FileDES {  
  29.     /**加密解密的key*/  
  30.     private Key mKey;  
  31.     /**解密的密码*/  
  32.     private Cipher mDecryptCipher;  
  33.     /**加密的密码*/  
  34.     private Cipher mEncryptCipher;  
  35.     public FileDES(String key) throws Exception  
  36.     {  
  37.         initKey(key);  
  38.         initCipher();  
  39.     }  
  40.       
  41.     /** 
  42.      * 创建一个加密解密的key 
  43.      * @param keyRule  
  44.      */  
  45.     public void initKey(String keyRule) {  
  46.         byte[] keyByte = keyRule.getBytes();  
  47.         // 创建一个空的八位数组,默认情况下为0   
  48.         byte[] byteTemp = new byte[8];  
  49.         // 将用户指定的规则转换成八位数组   
  50.         for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {  
  51.             byteTemp[i] = keyByte[i];  
  52.         }  
  53.         mKey = new SecretKeySpec(byteTemp, "DES");  
  54.     }  
  55.       
  56.     /*** 
  57.      * 初始化加载密码 
  58.      * @throws Exception 
  59.      */  
  60.     private void initCipher() throws Exception  
  61.     {  
  62.         mEncryptCipher = Cipher.getInstance("DES");  
  63.         mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);  
  64.           
  65.         mDecryptCipher = Cipher.getInstance("DES");  
  66.         mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);  
  67.     }  
  68.       
  69.     /** 
  70.      * 加密文件 
  71.      * @param in 
  72.      * @param savePath 加密后保存的位置 
  73.      */  
  74.     public void doEncryptFile(InputStream in,String savePath)  
  75.     {  
  76.         if(in==null)  
  77.         {  
  78.             System.out.println("inputstream is null");  
  79.             return;  
  80.         }  
  81.         try {  
  82.             CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);  
  83.             OutputStream os = new FileOutputStream(savePath);  
  84.             byte[] bytes = new byte[1024];  
  85.             int len = -1;  
  86.             while((len=cin.read(bytes))>0)  
  87.             {  
  88.                 os.write(bytes, 0, len);  
  89.                 os.flush();  
  90.             }  
  91.             os.close();  
  92.             cin.close();  
  93.             in.close();  
  94.             System.out.println("加密成功");  
  95.         } catch (Exception e) {  
  96.             System.out.println("加密失败");  
  97.             e.printStackTrace();  
  98.         }  
  99.     }  
  100.       
  101.     /** 
  102.      * 加密文件 
  103.      * @param filePath 需要加密的文件路径 
  104.      * @param savePath 加密后保存的位置 
  105.      * @throws FileNotFoundException  
  106.      */  
  107.     public void doEncryptFile(String filePath,String savePath) throws FileNotFoundException  
  108.     {  
  109.         doEncryptFile(new FileInputStream(filePath), savePath);  
  110.     }  
  111.       
  112.       
  113.     /** 
  114.      * 解密文件 
  115.      * @param in 
  116.      */  
  117.     public void doDecryptFile(InputStream in)  
  118.     {  
  119.         if(in==null)  
  120.         {  
  121.             System.out.println("inputstream is null");  
  122.             return;  
  123.         }  
  124.         try {  
  125.             CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);  
  126.             BufferedReader reader = new BufferedReader(new InputStreamReader(cin)) ;  
  127.             String line = null;  
  128.             while((line=reader.readLine())!=null)  
  129.             {  
  130.                 System.out.println(line);  
  131.             }  
  132.             reader.close();  
  133.             cin.close();  
  134.             in.close();  
  135.             System.out.println("解密成功");  
  136.         } catch (Exception e) {  
  137.             System.out.println("解密失败");  
  138.             e.printStackTrace();  
  139.         }  
  140.     }  
  141.     /** 
  142.      * 解密文件 
  143.      * @param filePath  文件路径 
  144.      * @throws Exception 
  145.      */  
  146.     public void doDecryptFile(String filePath) throws Exception  
  147.     {  
  148.         doDecryptFile(new FileInputStream(filePath));  
  149.     }  
  150.       
  151.       
  152.     public static void main(String[] args)throws Exception {  
  153.         FileDES fileDES = new FileDES("spring.sky");  
  154.         fileDES.doEncryptFile("d:/a.txt""d:/b");  //加密   
  155.         fileDES.doDecryptFile("d:/b"); //解密   
  156.     }  
  157.       
  158. }  

上面的代码,我分别在android 1.6和java平台上面测试通过了,没任何问题的,只是根据不同的需求做一下封装,希望对大家有帮忙,��大家少走弯路!

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容