Category - Java

2017-06-17 15:24:53    267    0    0
  1. package com.xy.jx.util;
  2. import android.util.Log;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import javax.crypto.Cipher;
  10. import javax.crypto.spec.IvParameterSpec;
  11. import javax.crypto.spec.SecretKeySpec;
  12. /**
  13. * Created by xuxiang on 2017/6/10.
  14. */
  15. public class FileAESUtil {
  16. /**
  17. * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
  18. **/
  19. private static String sKey = "写入您的加密key";//key,可自行修改
  20. private static String ivParameter = "写入您的偏移量";//偏移量,可自行修改
  21. /**
  22. * 算法/模式/填充
  23. **/
  24. private static final String CipherMode = "AES/CBC/PKCS5Padding";
  25. /**
  26. * 加密文件
  27. * @param readPath 源文件路径
  28. * @param writePath 加密后文件路径
  29. */
  30. public static void setAESFile(String readPath, String writePath) {
  31. File file = new File(readPath);
  32. if(!file.exists()) return;
  33. File writeFile = new File(wri
2017-01-11 16:35:45    183    0    0
  1. /**
  2. * 解密
  3. *
  4. * @param sSrc
  5. * @param key
  6. * @param ivs
  7. * @return
  8. */
  9. public static String decrypt(String sSrc, String key, String ivs) {
  10. try {
  11. byte[] raw = key.getBytes("ASCII");
  12. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  13. Cipher cipher = Cipher.getInstance(CipherMode);
  14. IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
  15. cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  16. byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
  17. byte[] original = cipher.doFinal(encrypted1);
  18. String originalString = new String(original, "utf-8");
  19. return originalString;
  20. } catch (Exception ex) {
  21. Log.e("AES", "解密异常:" + ex.getMessage());
  22. return null;
  23. }
  24. }
  25. /**
  26. * 加密
  27. *
  28. * @param encData
  29. * @param secretKey
  30. * @param vector
  31. * @return
  32. */
  33. public static String Encrypt(String encData, String secretKey, String vector) {
  34. try {
  35. if (secretKey == null)