每个人都有自己的知识体系。
Toggle navigation
Home
随笔
C#/.Net
树莓派 / Raspberry
皓月汉化组
Beego
Golang
OxideMod
apache
haproxy
windows
Java
Objective-C
日语/罗马音歌词/日语常识
MongoDB
python
电学
公告
Minecraft服务器-公告
NanoPi
C4D (CINEMA 4D)
生活
推流/m3u8/rtmp/rtsp
Unity3d
ffmpeg
数据结构
区块链
tarui
UnityForPSVita
About Me
Archives
Tags
Android/java AES文件加密解密
2017-06-17 15:24:53
285
0
0
akiragatsu
package com.xy.jx.util; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * Created by xuxiang on 2017/6/10. */ public class FileAESUtil { /** * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。 **/ private static String sKey = "写入您的加密key";//key,可自行修改 private static String ivParameter = "写入您的偏移量";//偏移量,可自行修改 /** * 算法/模式/填充 **/ private static final String CipherMode = "AES/CBC/PKCS5Padding"; /** * 加密文件 * @param readPath 源文件路径 * @param writePath 加密后文件路径 */ public static void setAESFile(String readPath, String writePath) { File file = new File(readPath); if(!file.exists()) return; File writeFile = new File(writePath); InputStream inputStream = null; OutputStream outputStream = null; byte bt[] = new byte[(int) file.length()]; try { if(!writeFile.exists()) writeFile.createNewFile(); inputStream = new FileInputStream(file); outputStream = new FileOutputStream(writeFile); int byteread = 0; // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = inputStream.read(bt)) != -1) { outputStream.write(encrypt(bt)); outputStream.flush(); } file.delete(); }catch (IOException e) { e.printStackTrace(); } finally { try { if(outputStream != null) outputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 解密文件 * @param readPath 源文件路径 * @param writePath 解密后文件路径 */ public static File readAESFile(String readPath, String writePath) { File file = new File(readPath); if(!file.exists()) return null; File writeFile = new File(writePath); InputStream inputStream = null; OutputStream outputStream = null; byte bt[] = new byte[(int) file.length()]; try { if(!writeFile.exists()) writeFile.createNewFile(); inputStream = new FileInputStream(file); outputStream = new FileOutputStream(writeFile); int byteread = 0; // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = inputStream.read(bt)) != -1) { byte[] decrypt = decrypt(bt); Log.e("length", "decrypt ======" + decrypt.length); outputStream.write(decrypt); outputStream.flush(); } return writeFile; }catch (IOException e) { e.printStackTrace(); return null; } finally { try { if(outputStream != null) outputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 加密 public static byte[] encrypt(byte[] bytes) { try { Cipher cipher = Cipher.getInstance(CipherMode); byte[] raw = sKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(bytes); return encrypted; } catch (Exception ex) { Log.e("AES", "加密异常:" + ex.getMessage()); return null; } } // 解密 public static byte[] decrypt(byte[] bytes) { try { byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance(CipherMode); IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); //byte[] encrypted1 = new BASE64Decoder().decodeBuffer(new String(bytes, "utf-8"));// 先用base64解密 byte[] original = cipher.doFinal(bytes); return original; } catch (Exception ex) { Log.e("AES", "解密异常:" + ex.getMessage()); return null; } } }
Pre:
Objective-C AES文件加密解密
Next:
使用 微软自带语音合成类库
0
likes
285
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.