歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android或Java用DES加密解密文件

Android或Java用DES加密解密文件

日期:2017/3/1 10:23:04   编辑:Linux編程
最近做項目,需要加密Android客戶端的一些sql語句,我當時使用的是DES加密的,結果加密出現了
  1. javax.crypto.BadPaddingException: Given final block not properly padded

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

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

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

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved