Android/java AES文件加密解密
2017-06-17 15:24:53    315    0    0
akiragatsu

package com.xy.jx.util;

  1. import android.util.Log;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.spec.IvParameterSpec;
  10. import javax.crypto.spec.SecretKeySpec;
  11. /**
  12. * Created by xuxiang on 2017/6/10.
  13. */
  14. public class FileAESUtil {
  15. /**
  16. * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
  17. **/
  18. private static String sKey = "写入您的加密key";//key,可自行修改
  19. private static String ivParameter = "写入您的偏移量";//偏移量,可自行修改
  20. /**
  21. * 算法/模式/填充
  22. **/
  23. private static final String CipherMode = "AES/CBC/PKCS5Padding";
  24. /**
  25. * 加密文件
  26. * @param readPath 源文件路径
  27. * @param writePath 加密后文件路径
  28. */
  29. public static void setAESFile(String readPath, String writePath) {
  30. File file = new File(readPath);
  31. if(!file.exists()) return;
  32. File writeFile = new File(writePath);
  33. InputStream inputStream = null;
  34. OutputStream outputStream = null;
  35. byte bt[] = new byte[(int) file.length()];
  36. try {
  37. if(!writeFile.exists()) writeFile.createNewFile();
  38. inputStream = new FileInputStream(file);
  39. outputStream = new FileOutputStream(writeFile);
  40. int byteread = 0;
  41. // 读入多个字节到字节数组中,byteread为一次读入的字节数
  42. while ((byteread = inputStream.read(bt)) != -1) {
  43. outputStream.write(encrypt(bt));
  44. outputStream.flush();
  45. }
  46. file.delete();
  47. }catch (IOException e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. if(outputStream != null)
  52. outputStream.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. try {
  57. if (inputStream != null)
  58. inputStream.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }
  64. /**
  65. * 解密文件
  66. * @param readPath 源文件路径
  67. * @param writePath 解密后文件路径
  68. */
  69. public static File readAESFile(String readPath, String writePath) {
  70. File file = new File(readPath);
  71. if(!file.exists()) return null;
  72. File writeFile = new File(writePath);
  73. InputStream inputStream = null;
  74. OutputStream outputStream = null;
  75. byte bt[] = new byte[(int) file.length()];
  76. try {
  77. if(!writeFile.exists()) writeFile.createNewFile();
  78. inputStream = new FileInputStream(file);
  79. outputStream = new FileOutputStream(writeFile);
  80. int byteread = 0;
  81. // 读入多个字节到字节数组中,byteread为一次读入的字节数
  82. while ((byteread = inputStream.read(bt)) != -1) {
  83. byte[] decrypt = decrypt(bt);
  84. Log.e("length", "decrypt ======" + decrypt.length);
  85. outputStream.write(decrypt);
  86. outputStream.flush();
  87. }
  88. return writeFile;
  89. }catch (IOException e) {
  90. e.printStackTrace();
  91. return null;
  92. } finally {
  93. try {
  94. if(outputStream != null)
  95. outputStream.close();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. try {
  100. if (inputStream != null)
  101. inputStream.close();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107. // 加密
  108. public static byte[] encrypt(byte[] bytes) {
  109. try {
  110. Cipher cipher = Cipher.getInstance(CipherMode);
  111. byte[] raw = sKey.getBytes();
  112. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  113. IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
  114. cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
  115. byte[] encrypted = cipher.doFinal(bytes);
  116. return encrypted;
  117. } catch (Exception ex) {
  118. Log.e("AES", "加密异常:" + ex.getMessage());
  119. return null;
  120. }
  121. }
  122. // 解密
  123. public static byte[] decrypt(byte[] bytes) {
  124. try {
  125. byte[] raw = sKey.getBytes("ASCII");
  126. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  127. Cipher cipher = Cipher.getInstance(CipherMode);
  128. IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
  129. cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  130. //byte[] encrypted1 = new BASE64Decoder().decodeBuffer(new String(bytes, "utf-8"));// 先用base64解密
  131. byte[] original = cipher.doFinal(bytes);
  132. return original;
  133. } catch (Exception ex) {
  134. Log.e("AES", "解密异常:" + ex.getMessage());
  135. return null;
  136. }
  137. }
  138. }

Pre: Objective-C AES文件加密解密

Next: 使用 微软自带语音合成类库

315
Sign in to leave a comment.
No Leanote account? Sign up now.
0 comments
Table of content